Module: Color::Maker::Support
- Defined in:
- lib/color/maker/support.rb
Overview
Utilities methods
Class Method Summary collapse
-
.hex_to_color(hex) ⇒ Color::RGB
Convert color from HEX to Color::RGB.
-
.hsl_to_color(hsl) ⇒ Color::RGB
Convert color from HSL to Color::RGB.
-
.hsl_to_hsv(hsl) ⇒ Hash
Convert color from HSL to HSV.
-
.hsl_to_rgb(hsl) ⇒ Hash
Convert color from HSL to RGB.
-
.hsv_to_color(hsv) ⇒ Color::RGB
Convert color from HSV to Color::RGB.
-
.hsv_to_hsl(hsv) ⇒ Hash
Convert color from HSV to HSL.
-
.hsv_to_rgb(hsv) ⇒ Hash
Convert color from HSV to RGB format.
-
.normalize_color_keys(hash, options = {}) ⇒ Hash
Normalize color keys.
-
.rgb_to_color(rgb) ⇒ Color::RGB
Convert color from RGB to Color::RGB.
-
.rgb_to_hsl(rgb) ⇒ Hash
Convert color from RGB to HSL.
-
.rgb_to_hsv(rgb) ⇒ Hash
Convert color from RGB to HSV.
Class Method Details
.hex_to_color(hex) ⇒ Color::RGB
Convert color from HEX to Color::RGB
185 186 187 188 |
# File 'lib/color/maker/support.rb', line 185 def hex_to_color(hex) hex = hex.sub(/0x/, '') Color::RGB.by_hex(hex) end |
.hsl_to_color(hsl) ⇒ Color::RGB
Convert color from HSL to Color::RGB
155 156 157 158 |
# File 'lib/color/maker/support.rb', line 155 def hsl_to_color(hsl) rgb = hsl_to_rgb(hsl) Color::RGB.new(rgb[:r], rgb[:g], rgb[:b]) end |
.hsl_to_hsv(hsl) ⇒ Hash
Convert color from HSL to HSV
143 144 145 146 |
# File 'lib/color/maker/support.rb', line 143 def hsl_to_hsv(hsl) rgb = hsl_to_rgb(hsl) rgb_to_hsv(rgb) end |
.hsl_to_rgb(hsl) ⇒ Hash
Convert color from HSL to RGB
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/color/maker/support.rb', line 109 def hsl_to_rgb(hsl) hsl = normalize_color_keys(hsl, format: :hsl) h, s, l = hsl[:h].to_f / 360.0, hsl[:s].to_f, hsl[:l].to_f return { r: l, g: l, b: l } if s == 0 q = l < 0.5 ? l * (1 + s) : l + s - l * s p = 2 * l - q r = hue_to_rgb(p: p, q: q, t: h + 1 / 3.0) * 255 g = hue_to_rgb(p: p, q: q, t: h) * 255 b = hue_to_rgb(p: p, q: q, t: h - 1 / 3.0) * 255 { r: r.round(0), g: g.round(0), b: b.round(0) } end |
.hsv_to_color(hsv) ⇒ Color::RGB
Convert color from HSV to Color::RGB
176 177 178 179 |
# File 'lib/color/maker/support.rb', line 176 def hsv_to_color(hsv) rgb = hsv_to_rgb(hsv) rgb_to_color(rgb) end |
.hsv_to_hsl(hsv) ⇒ Hash
Convert color from HSV to HSL
131 132 133 134 |
# File 'lib/color/maker/support.rb', line 131 def hsv_to_hsl(hsv) rgb = hsv_to_rgb(hsv) rgb_to_hsl(rgb) end |
.hsv_to_rgb(hsv) ⇒ Hash
Convert color from HSV to RGB format
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/color/maker/support.rb', line 13 def hsv_to_rgb(hsv) hsv = normalize_color_keys(hsv, format: :hsv) h, s, v = hsv[:h].to_f / 360.0, hsv[:s].to_f, hsv[:v].to_f i = (h * 6).floor f = h * 6 - i p = v * (1 - s) q = v * (1 - f * s) t = v * (1 - (1 - f) * s) case i % 6 when 0 r, g, b = v, t, p when 1 r, g, b = q, v, p when 2 r, g, b = p, v, t when 3 r, g, b = p, q, v when 4 r, g, b = t, p, v else r, g, b = v, p, q end r, g, b = (r * 255).round(0), (g * 255).round(0), (b * 255).round(0) { r: r, g: g, b: b } end |
.normalize_color_keys(hash, options = {}) ⇒ Hash
Normalize color keys
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/color/maker/support.rb', line 198 def normalize_color_keys(hash, = {}) = { format: :rgb, short: true } = .merge() format = [:format].to_sym keys = { rgb: { [:r, :red] => [:r, :red], [:b, :blue] => [:b, :blue], [:g, :green] => [:g, :green] }, hsv: { [:h, :hue] => [:h, :hue], [:s, :saturation] => [:s, :saturation], [:v, :value] => [:v, :value] }, hsl: { [:h, :hue] => [:h, :hue], [:s, :saturation] => [:s, :saturation], [:l, :light] => [:l, :lightness, :luminosity, :light] } } return unless keys[format] normalized = hash.dup keys[format].each do |key, variants| default = [:short] ? key.first : key.last normalized.replace_key!(variants, default) end normalized end |
.rgb_to_color(rgb) ⇒ Color::RGB
Convert color from RGB to Color::RGB
167 168 169 170 |
# File 'lib/color/maker/support.rb', line 167 def rgb_to_color(rgb) rgb = normalize_color_keys(rgb, format: :rgb) Color::RGB.new(rgb[:r], rgb[:g], rgb[:b]) end |
.rgb_to_hsl(rgb) ⇒ Hash
Convert color from RGB to HSL
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/color/maker/support.rb', line 77 def rgb_to_hsl(rgb) rgb = normalize_color_keys(rgb, format: :rgb) r, g, b = rgb[:r].to_f / 255.0, rgb[:g].to_f / 255.0, rgb[:b].to_f / 255.0 min, max = [r, g, b].min, [r, g, b].max delta = max - min l = (min + max) / 2.0 return { h: 0, s: 0, l: l } if delta == 0 s = l < 0.5 ? delta / (max + min) : delta / (2 - max - min) case(max) when r h = (g - b) / delta + (g < b ? 6 : 0) when g h = (b - r) / delta + 2 when b h = (r - g) / delta + 4 end h *= 60 { h: h.round(1), s: s.round(3), l: l.round(3) } end |
.rgb_to_hsv(rgb) ⇒ Hash
Convert color from RGB to HSV
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/color/maker/support.rb', line 47 def rgb_to_hsv(rgb) rgb = normalize_color_keys(rgb, format: :rgb) r, g, b = rgb[:r].to_f / 255.0, rgb[:g].to_f / 255.0, rgb[:b].to_f / 255.0 min, max = [r, g, b].min, [r, g, b].max delta = max - min v = max return { h: 0, s: 0, v: v.round(3) } if max == 0 || delta == 0 s = delta / max h = 4 + (r - g) / delta if r == max h = (g - b) / delta elsif g == max h = 2 + (b - r) / delta end h *= 60 h += 360 if h < 0 { h: h.round(1), s: s.round(3), v: v.round(3) } end |