module UnderOs::Color::Converter
Constants
- ALIASES
- HTML_COLORS
- IOS_COLORS
Public Instance Methods
color_2_rgba(color)
click to toggle source
# File lib/under_os/color.rb, line 109 def color_2_rgba(color) if color =~ /^#[abcdef0-9]{6}$/ hex_2_rgb(color) elsif m = color.match(/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d*)\s*\)$/) [m[1].to_i, m[2].to_i, m[3].to_i] elsif m = color.match(/^rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d*)\s*,\s*([\d\.]+)\s*\)$/) [m[1].to_i, m[2].to_i, m[3].to_i, m[4].to_f] else raise "Malformed color spec: #{color.inspect}" end end
color_from_array(r,g,b,a=1.0)
click to toggle source
# File lib/under_os/color.rb, line 87 def color_from_array(r,g,b,a=1.0) r = r / 255.0 if r.is_a?(Integer) || r > 1.0 g = g / 255.0 if g.is_a?(Integer) || g > 1.0 b = b / 255.0 if b.is_a?(Integer) || b > 1.0 UIColor.colorWithRed(r, green: g, blue: b, alpha: a) end
color_from_param(param)
click to toggle source
# File lib/under_os/color.rb, line 121 def color_from_param(param) # 0.0..1.0 color_from_array *param_2_rgb(param) end
color_from_string(name)
click to toggle source
# File lib/under_os/color.rb, line 95 def color_from_string(name) name = name.downcase.strip name = ALIASES[name] || name return UIColor.send("#{name}Color") if IOS_COLORS.include?(name) name = HTML_COLORS[name] || name name = "#" + name[1]*2 + name[2]*2 + name[3]*2 if name =~ /^#[abcdef0-9]{3}$/ r, g, b, a = color_2_rgba(name) color_from_array(r, g, b, a || 1.0) end
hex_2_rgb(hex)
click to toggle source
# File lib/under_os/color.rb, line 125 def hex_2_rgb(hex) int = hex[1..-1].to_i(16) r = (int & 0xFF0000) >> 16 g = (int & 0xFF00) >> 8 b = (int & 0xFF) [r, g, b] end
param_2_rgb(a)
click to toggle source
# File lib/under_os/color.rb, line 135 def param_2_rgb(a) # 0.0..1.0 x = 1 / 6.0 s = a % x / x r = 1 - s if a < x then [1.0, s, 0.0] elsif a < x * 2 then [r, 1.0, 0.0] elsif a < x * 3 then [0.0, 1.0, s] elsif a < x * 4 then [0.0, r, 1.0] elsif a < x * 5 then [s, 0.0, 1.0] else [1.0, 0.0, r] end end
ui_color_from(*args)
click to toggle source
# File lib/under_os/color.rb, line 74 def ui_color_from(*args) origin = args.size == 1 ? args[0] : args if origin.is_a?(UIColor) then origin elsif origin.is_a?(Array) then color_from_array(*origin) elsif origin.is_a?(String) then color_from_string(origin) elsif origin.is_a?(Symbol) then color_from_string(origin.to_s) elsif origin.is_a?(Float) then color_from_param(origin) elsif origin.is_a?(Integer) then color_from_param(origin / 100.0) else UIColor.redColor # fallback end end