Class: ColorLib::HSL
- Inherits:
-
Object
- Object
- ColorLib::HSL
- Defined in:
- lib/color_lib/hsl.rb
Overview
An HSL colour object. Internally, the hue (#h), saturation (#s), and luminosity/lightness (#l) values are dealt with as fractional values in the range 0..1.
Class Method Summary collapse
-
.from_fraction(h = 0.0, s = 0.0, l = 0.0) ⇒ Object
Creates an HSL colour object from fractional values 0..1.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compares the other colour to this one.
-
#brightness ⇒ Object
Returns the luminosity (#l) of the colour.
-
#css_hsl ⇒ Object
Present the colour as an HSL HTML/CSS colour string (e.g., “hsl(180, 25%, 35%)”).
-
#css_hsla ⇒ Object
Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g., “hsla(180, 25%, 35%, 1)”).
-
#css_rgb ⇒ Object
Present the colour as an RGB HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%)”).
-
#css_rgba ⇒ Object
Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%, 1)”).
-
#h ⇒ Object
Returns the hue of the colour in the range 0.0 ..
-
#h=(hh) ⇒ Object
Sets the hue of the colour in the range 0.0 ..
-
#html ⇒ Object
Present the colour as an HTML/CSS colour string.
-
#hue ⇒ Object
Returns the hue of the colour in degrees.
-
#hue=(hh) ⇒ Object
Sets the hue of the colour in degrees.
-
#initialize(h = 0, s = 0, l = 0) ⇒ HSL
constructor
Creates an HSL colour object from the standard values of degrees and percentages (e.g., 145 deg, 30%, 50%).
- #inspect ⇒ Object
-
#l ⇒ Object
Returns the luminosity of the colour in the range 0.0 ..
-
#l=(ll) ⇒ Object
Sets the luminosity of the colour in the ragne 0.0 ..
-
#luminosity ⇒ Object
(also: #lightness)
Returns the percentage of luminosity of the colour.
-
#luminosity=(ll) ⇒ Object
(also: #lightness=)
Sets the percentage of luminosity of the colour.
-
#mix_with(color, mix_percent = 0.5) ⇒ Object
Mix the mask colour (which will be converted to an HSL colour) with the current colour at the stated mix percentage as a decimal value.
-
#s ⇒ Object
Returns the saturation of the colour in the range 0.0 ..
-
#s=(ss) ⇒ Object
Sets the saturation of the colour in the ragne 0.0 ..
-
#saturation ⇒ Object
Returns the percentage of saturation of the colour.
-
#saturation=(ss) ⇒ Object
Sets the percentage of saturation of the colour.
-
#to_cmyk ⇒ Object
Converts to RGB then CMYK.
- #to_greyscale ⇒ Object (also: #to_grayscale)
- #to_hsl ⇒ Object
-
#to_rgb(ignored = nil) ⇒ Object
Converting to HSL as adapted from Foley and Van-Dam from www.bobpowell.net/RGBHSB.htm.
-
#to_yiq ⇒ Object
Converts to RGB then YIQ.
Constructor Details
#initialize(h = 0, s = 0, l = 0) ⇒ HSL
Creates an HSL colour object from the standard values of degrees and percentages (e.g., 145 deg, 30%, 50%).
33 34 35 36 37 |
# File 'lib/color_lib/hsl.rb', line 33 def initialize(h = 0, s = 0, l = 0) @h = h / 360.0 @s = s / 100.0 @l = l / 100.0 end |
Class Method Details
Instance Method Details
#==(other) ⇒ Object
Compares the other colour to this one. The other colour will be converted to HSL before comparison, so the comparison between a HSL colour and a non-HSL colour will be approximate and based on the other colour’s #to_hsl conversion. If there is no #to_hsl conversion, this will raise an exception. This will report that two HSL values are equivalent if all component values are within ColorLib::COLOR_TOLERANCE of each other.
23 24 25 26 27 28 29 |
# File 'lib/color_lib/hsl.rb', line 23 def ==(other) other = other.to_hsl other.kind_of?(ColorLib::HSL) and ((@h - other.h).abs <= ColorLib::COLOR_TOLERANCE) and ((@s - other.s).abs <= ColorLib::COLOR_TOLERANCE) and ((@l - other.l).abs <= ColorLib::COLOR_TOLERANCE) end |
#brightness ⇒ Object
Returns the luminosity (#l) of the colour.
123 124 125 |
# File 'lib/color_lib/hsl.rb', line 123 def brightness @l end |
#css_hsl ⇒ Object
Present the colour as an HSL HTML/CSS colour string (e.g., “hsl(180, 25%, 35%)”).
60 61 62 |
# File 'lib/color_lib/hsl.rb', line 60 def css_hsl "hsl(%3.2f, %3.2f%%, %3.2f%%)" % [hue, saturation, luminosity] end |
#css_hsla ⇒ Object
Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g., “hsla(180, 25%, 35%, 1)”).
66 67 68 |
# File 'lib/color_lib/hsl.rb', line 66 def css_hsla "hsla(%3.2f, %3.2f%%, %3.2f%%, %3.2f)" % [hue, saturation, luminosity, 1] end |
#css_rgb ⇒ Object
Present the colour as an RGB HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%)”). Note that this will perform a #to_rgb operation using the default conversion formula.
47 48 49 |
# File 'lib/color_lib/hsl.rb', line 47 def css_rgb to_rgb.css_rgb end |
#css_rgba ⇒ Object
Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., “rgb(0%, 50%, 100%, 1)”). Note that this will perform a #to_rgb operation using the default conversion formula.
54 55 56 |
# File 'lib/color_lib/hsl.rb', line 54 def css_rgba to_rgb.css_rgba end |
#h ⇒ Object
Returns the hue of the colour in the range 0.0 .. 1.0.
139 140 141 |
# File 'lib/color_lib/hsl.rb', line 139 def h @h end |
#h=(hh) ⇒ Object
Sets the hue of the colour in the range 0.0 .. 1.0.
155 156 157 |
# File 'lib/color_lib/hsl.rb', line 155 def h=(hh) @h = ColorLib.normalize(hh) end |
#html ⇒ Object
Present the colour as an HTML/CSS colour string.
40 41 42 |
# File 'lib/color_lib/hsl.rb', line 40 def html to_rgb.html end |
#hue ⇒ Object
Returns the hue of the colour in degrees.
134 135 136 |
# File 'lib/color_lib/hsl.rb', line 134 def hue @h * 360.0 end |
#hue=(hh) ⇒ Object
Sets the hue of the colour in degrees. Colour is perceived as a wheel, so values should be set properly even with negative degree values.
145 146 147 148 149 150 151 152 |
# File 'lib/color_lib/hsl.rb', line 145 def hue=(hh) hh = hh / 360.0 hh += 1.0 if hh < 0.0 hh -= 1.0 if hh > 1.0 @h = ColorLib.normalize(hh) end |
#inspect ⇒ Object
205 206 207 |
# File 'lib/color_lib/hsl.rb', line 205 def inspect "HSL [%.2f deg, %.2f%%, %.2f%%]" % [hue, saturation, luminosity] end |
#l ⇒ Object
Returns the luminosity of the colour in the range 0.0 .. 1.0.
186 187 188 |
# File 'lib/color_lib/hsl.rb', line 186 def l @l end |
#l=(ll) ⇒ Object
Sets the luminosity of the colour in the ragne 0.0 .. 1.0.
197 198 199 |
# File 'lib/color_lib/hsl.rb', line 197 def l=(ll) @l = ColorLib.normalize(ll) end |
#luminosity ⇒ Object Also known as: lightness
Returns the percentage of luminosity of the colour.
180 181 182 |
# File 'lib/color_lib/hsl.rb', line 180 def luminosity @l * 100.0 end |
#luminosity=(ll) ⇒ Object Also known as: lightness=
Sets the percentage of luminosity of the colour.
191 192 193 |
# File 'lib/color_lib/hsl.rb', line 191 def luminosity=(ll) @l = ColorLib.normalize(ll / 100.0) end |
#mix_with(color, mix_percent = 0.5) ⇒ Object
Mix the mask colour (which will be converted to an HSL colour) with the current colour at the stated mix percentage as a decimal value.
- NOTE
-
This differs from ColorLib::RGB#mix_with.
213 214 215 216 217 218 219 220 |
# File 'lib/color_lib/hsl.rb', line 213 def mix_with(color, mix_percent = 0.5) color = color.to_hsl _h = ((color.h - self.h) * mix_percent) + self.h _s = ((color.s - self.s) * mix_percent) + self.s _l = ((color.l - self.l) * mix_percent) + self.l self.class.from_fraction(_h, _s, _l) end |
#s ⇒ Object
Returns the saturation of the colour in the range 0.0 .. 1.0.
165 166 167 |
# File 'lib/color_lib/hsl.rb', line 165 def s @s end |
#s=(ss) ⇒ Object
Sets the saturation of the colour in the ragne 0.0 .. 1.0.
175 176 177 |
# File 'lib/color_lib/hsl.rb', line 175 def s=(ss) @s = ColorLib.normalize(ss) end |
#saturation ⇒ Object
Returns the percentage of saturation of the colour.
160 161 162 |
# File 'lib/color_lib/hsl.rb', line 160 def saturation @s * 100.0 end |
#saturation=(ss) ⇒ Object
Sets the percentage of saturation of the colour.
170 171 172 |
# File 'lib/color_lib/hsl.rb', line 170 def saturation=(ss) @s = ColorLib.normalize(ss / 100.0) end |
#to_cmyk ⇒ Object
Converts to RGB then CMYK.
118 119 120 |
# File 'lib/color_lib/hsl.rb', line 118 def to_cmyk to_rgb.to_cmyk end |
#to_greyscale ⇒ Object Also known as: to_grayscale
127 128 129 |
# File 'lib/color_lib/hsl.rb', line 127 def to_greyscale ColorLib::GrayScale.from_fraction(@l) end |
#to_hsl ⇒ Object
201 202 203 |
# File 'lib/color_lib/hsl.rb', line 201 def to_hsl self end |
#to_rgb(ignored = nil) ⇒ Object
Converting to HSL as adapted from Foley and Van-Dam from www.bobpowell.net/RGBHSB.htm.
NOTE:
-
If the colour’s luminosity is near zero, the colour is always black.
-
If the colour’s luminosity is near one, the colour is always white.
-
If the colour’s saturation is near zero, the colour is always a shade of grey and is based only on the luminosity of the colour.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/color_lib/hsl.rb', line 79 def to_rgb(ignored = nil) return ColorLib::RGB.new if ColorLib.near_zero_or_less?(@l) return ColorLib::RGB.new(0xff, 0xff, 0xff) if ColorLib.near_one_or_more?(@l) return ColorLib::RGB.from_fraction(@l, @l, @l) if ColorLib.near_zero?(@s) # Is the value less than 0.5? if ColorLib.near_zero_or_less?(@l - 0.5) tmp2 = @l * (1.0 + @s.to_f) else tmp2 = @l + @s - (@l * @s.to_f) end tmp1 = 2.0 * @l - tmp2 tmp3 = [@h + (1.0 / 3.0), @h, @h - (1.0 / 3.0)] rgb = tmp3.map { |hue| hue += 1.0 if ColorLib.near_zero_or_less?(hue) hue -= 1.0 if ColorLib.near_one_or_more?(hue) if ColorLib.near_zero_or_less?((6.0 * hue) - 1.0) tmp1 + ((tmp2 - tmp1) * hue * 6.0) elsif ColorLib.near_zero_or_less?((2.0 * hue) - 1.0) tmp2 elsif ColorLib.near_zero_or_less?((3.0 * hue) - 2.0) tmp1 + (tmp2 - tmp1) * ((2 / 3.0) - hue) * 6.0 else tmp1 end } ColorLib::RGB.from_fraction(*rgb) end |
#to_yiq ⇒ Object
Converts to RGB then YIQ.
113 114 115 |
# File 'lib/color_lib/hsl.rb', line 113 def to_yiq to_rgb.to_yiq end |