Class Color::HSL
In: lib/color/hsl.rb
Parent: Object
Enumerable Gimp AdobeColor CMYK\n[lib/color.rb\nlib/color/cmyk.rb] GrayScale\n[lib/color.rb\nlib/color/grayscale.rb] RGB\n[lib/color.rb\nlib/color/rgb-colors.rb\nlib/color/rgb.rb] YIQ\n[lib/color.rb\nlib/color/yiq.rb] HSL MonoContrast lib/color/cmyk.rb lib/color/grayscale.rb lib/color/rgb.rb lib/color/yiq.rb lib/color/hsl.rb CSS lib/color/palette/gimp.rb lib/color/palette/adobecolor.rb lib/color/palette/monocontrast.rb Palette Color dot/m_10_0.png

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.

Methods

==   brightness   css_hsl   css_hsla   css_rgb   css_rgba   from_fraction   h   h=   html   hue   hue=   inspect   l   l=   lightness   lightness=   luminosity   luminosity=   mix_with   new   s   s=   saturation   saturation=   to_cmyk   to_grayscale   to_greyscale   to_hsl   to_rgb   to_yiq  

Public Class methods

Creates an HSL colour object from fractional values 0..1.

[Source]

    # File lib/color/hsl.rb, line 21
21:     def from_fraction(h = 0.0, s = 0.0, l = 0.0)
22:       colour = Color::HSL.new
23:       colour.h = h
24:       colour.s = s
25:       colour.l = l
26:       colour
27:     end

Creates an HSL colour object from the standard values of degrees and percentages (e.g., 145 deg, 30%, 50%).

[Source]

    # File lib/color/hsl.rb, line 47
47:   def initialize(h = 0, s = 0, l = 0)
48:     @h = h / 360.0
49:     @s = s / 100.0
50:     @l = l / 100.0
51:   end

Public Instance methods

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 Color::COLOR_TOLERANCE of each other.

[Source]

    # File lib/color/hsl.rb, line 37
37:   def ==(other)
38:     other = other.to_hsl
39:     other.kind_of?(Color::HSL) and
40:     ((@h - other.h).abs <= Color::COLOR_TOLERANCE) and
41:     ((@s - other.s).abs <= Color::COLOR_TOLERANCE) and
42:     ((@l - other.l).abs <= Color::COLOR_TOLERANCE)
43:   end

Returns the luminosity (l) of the colour.

[Source]

     # File lib/color/hsl.rb, line 137
137:   def brightness
138:     @l
139:   end

Present the colour as an HSL HTML/CSS colour string (e.g., "hsl(180, 25%, 35%)").

[Source]

    # File lib/color/hsl.rb, line 74
74:   def css_hsl
75:     "hsl(%3.2f, %3.2f%%, %3.2f%%)" % [ hue, saturation, luminosity ]
76:   end

Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g., "hsla(180, 25%, 35%, 1)").

[Source]

    # File lib/color/hsl.rb, line 80
80:   def css_hsla
81:     "hsla(%3.2f, %3.2f%%, %3.2f%%, %3.2f)" % [ hue, saturation, luminosity, 1 ]
82:   end

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.

[Source]

    # File lib/color/hsl.rb, line 61
61:   def css_rgb
62:     to_rgb.css_rgb
63:   end

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.

[Source]

    # File lib/color/hsl.rb, line 68
68:   def css_rgba
69:     to_rgb.css_rgba
70:   end

Returns the hue of the colour in the range 0.0 .. 1.0.

[Source]

     # File lib/color/hsl.rb, line 150
150:   def h
151:     @h
152:   end

Sets the hue of the colour in the range 0.0 .. 1.0.

[Source]

     # File lib/color/hsl.rb, line 164
164:   def h=(hh)
165:     @h = Color.normalize(hh)
166:   end

Present the colour as an HTML/CSS colour string.

[Source]

    # File lib/color/hsl.rb, line 54
54:   def html
55:     to_rgb.html
56:   end

Returns the hue of the colour in degrees.

[Source]

     # File lib/color/hsl.rb, line 146
146:   def hue
147:     @h * 360.0
148:   end

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.

[Source]

     # File lib/color/hsl.rb, line 155
155:   def hue=(hh)
156:     hh = hh / 360.0
157: 
158:     hh += 1.0 if hh < 0.0
159:     hh -= 1.0 if hh > 1.0
160: 
161:     @h = Color.normalize(hh)
162:   end

[Source]

     # File lib/color/hsl.rb, line 207
207:   def inspect
208:     "HSL [%.2f deg, %.2f%%, %.2f%%]" % [ hue, saturation, luminosity ]
209:   end

Returns the luminosity of the colour in the range 0.0 .. 1.0.

[Source]

     # File lib/color/hsl.rb, line 190
190:   def l
191:     @l
192:   end

Sets the luminosity of the colour in the ragne 0.0 .. 1.0.

[Source]

     # File lib/color/hsl.rb, line 199
199:   def l=(ll)
200:     @l = Color.normalize(ll)
201:   end
lightness()

Alias for luminosity

lightness=(ll)

Alias for luminosity=

Returns the percentage of luminosity of the colour.

[Source]

     # File lib/color/hsl.rb, line 185
185:   def luminosity
186:     @l * 100.0
187:   end

Sets the percentage of luminosity of the colour.

[Source]

     # File lib/color/hsl.rb, line 194
194:   def luminosity=(ll)
195:     @l = Color.normalize(ll / 100.0)
196:   end

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 Color::RGB#mix_with.

[Source]

     # File lib/color/hsl.rb, line 215
215:   def mix_with(color, mix_percent = 0.5)
216:     color   = color.to_hsl
217:     _h = ((color.h - self.h) * mix_percent) + self.h
218:     _s = ((color.s - self.s) * mix_percent) + self.s
219:     _l = ((color.l - self.l) * mix_percent) + self.l
220: 
221:     self.class.from_fraction(_h, _s, _l)
222:   end

Returns the saturation of the colour in the range 0.0 .. 1.0.

[Source]

     # File lib/color/hsl.rb, line 172
172:   def s
173:     @s
174:   end

Sets the saturation of the colour in the ragne 0.0 .. 1.0.

[Source]

     # File lib/color/hsl.rb, line 180
180:   def s=(ss)
181:     @s = Color.normalize(ss)
182:   end

Returns the percentage of saturation of the colour.

[Source]

     # File lib/color/hsl.rb, line 168
168:   def saturation
169:     @s * 100.0
170:   end

Sets the percentage of saturation of the colour.

[Source]

     # File lib/color/hsl.rb, line 176
176:   def saturation=(ss)
177:     @s = Color.normalize(ss / 100.0)
178:   end

Converts to RGB then CMYK.

[Source]

     # File lib/color/hsl.rb, line 132
132:   def to_cmyk
133:     to_rgb.to_cmyk
134:   end
to_grayscale()

Alias for to_greyscale

[Source]

     # File lib/color/hsl.rb, line 140
140:   def to_greyscale
141:     Color::GrayScale.from_fraction(@l)
142:   end

[Source]

     # File lib/color/hsl.rb, line 203
203:   def to_hsl
204:     self
205:   end

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.

[Source]

     # File lib/color/hsl.rb, line 93
 93:   def to_rgb(ignored = nil)
 94:     return Color::RGB.new if Color.near_zero_or_less?(@l)
 95:     return Color::RGB.new(0xff, 0xff, 0xff) if Color.near_one_or_more?(@l)
 96:     return Color::RGB.from_fraction(@l, @l, @l) if Color.near_zero?(@s)
 97: 
 98:     # Is the value less than 0.5?
 99:     if Color.near_zero_or_less?(@l - 0.5)
100:       tmp2 = @l * (1.0 + @s.to_f)
101:     else
102:       tmp2 = @l + @s - (@l * @s.to_f)
103:     end
104:     tmp1 = 2.0 * @l - tmp2
105: 
106:     tmp3  = [ @h + (1.0 / 3.0), @h, @h - (1.0 / 3.0) ]
107: 
108:     rgb = tmp3.map { |hue|
109:       hue += 1.0 if Color.near_zero_or_less?(hue)
110:       hue -= 1.0 if Color.near_one_or_more?(hue)
111: 
112:       if Color.near_zero_or_less?((6.0 * hue) - 1.0)
113:         tmp1 + ((tmp2 - tmp1) * hue * 6.0)
114:       elsif Color.near_zero_or_less?((2.0 * hue) - 1.0)
115:         tmp2
116:       elsif Color.near_zero_or_less?((3.0 * hue) - 2.0)
117:         tmp1 + (tmp2 - tmp1) * ((2 / 3.0) - hue) * 6.0
118:       else
119:         tmp1
120:       end
121:     }
122: 
123:      Color::RGB.from_fraction(*rgb)
124:   end

Converts to RGB then YIQ.

[Source]

     # File lib/color/hsl.rb, line 127
127:   def to_yiq
128:     to_rgb.to_yiq
129:   end

[Validate]