Class: ColorMath
- Inherits:
-
Object
- Object
- ColorMath
- Defined in:
- lib/color_math.rb
Class Method Summary collapse
-
.from_hex(hex) ⇒ Object
creates a color object from hex code.
-
.from_hsl(h, s, l) ⇒ Object
h The hue (int) 0 - 360 s The saturation (int) 0 - 100 l The lightness (int) 0 - 100.
- .hex_to_rgb(hex) ⇒ Object
-
.hsl_to_rgb(h, s, l) ⇒ Object
Converts an HSL color value to RGB.
- .hue_to_rgb(p, q, t) ⇒ Object
- .rgb_to_hex(r, g, b) ⇒ Object
-
.rgb_to_hsl(r, g, b) ⇒ Object
r The red color value (int) g The green color value (int) b The blue color value (int) The HSL representation [hue, saturation, luminosity].
Instance Method Summary collapse
-
#initialize(r, g, b) ⇒ ColorMath
constructor
r The red color value (int) g The green color value (int) b The blue color value (int).
- #to_hex ⇒ Object
- #to_hsl ⇒ Object
- #to_rgb ⇒ Object
Constructor Details
#initialize(r, g, b) ⇒ ColorMath
r The red color value (int) g The green color value (int) b The blue color value (int)
6 7 8 9 10 |
# File 'lib/color_math.rb', line 6 def initialize(r, g, b) @r = r @g = g @b = b end |
Class Method Details
.from_hex(hex) ⇒ Object
creates a color object from hex code
27 28 29 30 |
# File 'lib/color_math.rb', line 27 def self.from_hex(hex) r, g, b = hex_to_rgb(hex) new(r, g, b) end |
.from_hsl(h, s, l) ⇒ Object
h The hue (int) 0 - 360 s The saturation (int) 0 - 100 l The lightness (int) 0 - 100
35 36 37 38 |
# File 'lib/color_math.rb', line 35 def self.from_hsl(h, s, l) r, g, b = hsl_to_rgb(h, s, l) new(r, g, b) end |
.hex_to_rgb(hex) ⇒ Object
54 55 56 57 58 |
# File 'lib/color_math.rb', line 54 def self.hex_to_rgb(hex) hex.gsub!("#", "") components = hex.scan(/.{2}/) components.collect { |component| component.to_i(16) } end |
.hsl_to_rgb(h, s, l) ⇒ Object
Converts an HSL color value to RGB. Conversion formula adapted from en.wikipedia.org/wiki/HSL_color_space. and
# http://stackoverflow.com/a/9493060/520008
returns r, g, and b in the set [0, 255]. h The hue (int) 0 - 360 s The saturation (int) 0 - 100 l The lightness (int) 0 - 100 returns The RGB representation [r, g, b]
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/color_math.rb', line 107 def self.hsl_to_rgb(h, s, l) h = h/360.0 s = s/100.0 l = l/100.0 r = 0.0 g = 0.0 b = 0.0 if(s == 0.0) r = l.to_f g = l.to_f b = l.to_f #achromatic else q = l < 0.5 ? l * (1 + s) : l + s - l * s p = 2 * l - q r = hue_to_rgb(p, q, h + 1/3.0) g = hue_to_rgb(p, q, h) b = hue_to_rgb(p, q, h - 1/3.0) end return [(r * 255).round, (g * 255).round, (b * 255).round] end |
.hue_to_rgb(p, q, t) ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/color_math.rb', line 131 def self.hue_to_rgb(p, q, t) t += 1 if(t < 0) t -= 1 if(t > 1) return (p + (q - p) * 6 * t) if(t < 1/6.0) return q if(t < 1/2.0) return (p + (q - p) * (2/3.0 - t) * 6) if(t < 2/3.0) return p end |
.rgb_to_hex(r, g, b) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/color_math.rb', line 40 def self.rgb_to_hex(r, g, b) rbg = [r,g,b] hex = "#" rbg.each do |component| _hex = component.to_s(16) if component < 16 hex << "0#{_hex}" else hex << _hex end end hex.upcase end |
.rgb_to_hsl(r, g, b) ⇒ Object
r The red color value (int) g The green color value (int) b The blue color value (int) The HSL representation [hue, saturation, luminosity]
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/color_math.rb', line 70 def self.rgb_to_hsl(r, g, b) r /= 255.0 g /= 255.0 b /= 255.0 max = [r, g, b].max min = [r, g, b].min h = (max + min) / 2.0 s = (max + min) / 2.0 l = (max + min) / 2.0 if(max == min) h = 0 s = 0 # achromatic else d = max - min; s = l >= 0.5 ? d / (2.0 - max - min) : d / (max + min) case max when r h = (g - b) / d + (g < b ? 6.0 : 0) when g h = (b - r) / d + 2.0 when b h = (r - g) / d + 4.0 end h /= 6.0 end return [(h*360).round, (s*100).round, (l*100).round] end |
Instance Method Details
#to_hex ⇒ Object
12 13 14 |
# File 'lib/color_math.rb', line 12 def to_hex self.class.rgb_to_hex(@r, @g, @b) end |
#to_hsl ⇒ Object
20 21 22 |
# File 'lib/color_math.rb', line 20 def to_hsl self.class.rgb_to_hsl(@r, @g, @b) end |
#to_rgb ⇒ Object
16 17 18 |
# File 'lib/color_math.rb', line 16 def to_rgb return @r, @g, @b end |