Module: UnderOs::Color::Converter
Constant Summary collapse
- ALIASES =
{ 'transparent' => 'clear', 'darkgray' => 'darkGray', 'lightgray' => 'lightGray' }
- IOS_COLORS =
%w[ black darkGray lightGray white gray red green blue cyan yellow magenta orange purple brown clear ].freeze
- HTML_COLORS =
{ maroon: '#800000', olive: '#808000', fuchsia: '#ff00ff', lime: '#00ff00', navy: '#000080', aqua: '#00ffff', teal: '#008080', silver: '#c0c0c0', }.freeze
Instance Method Summary collapse
- #color_2_rgba(color) ⇒ Object
- #color_from_array(r, g, b, a = 1.0) ⇒ Object
-
#color_from_param(param) ⇒ Object
0.0..1.0.
- #color_from_string(name) ⇒ Object
- #hex_2_rgb(hex) ⇒ Object
-
#param_2_rgb(a) ⇒ Object
0.0..1.0.
- #ui_color_from(*args) ⇒ Object
Instance Method Details
#color_2_rgba(color) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# 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) ⇒ Object
87 88 89 90 91 92 93 |
# 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) ⇒ Object
0.0..1.0
121 122 123 |
# 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) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# 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) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# 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) ⇒ Object
0.0..1.0
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# 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) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# 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 |