167: def to_hsl
168: min = [ @r, @g, @b ].min
169: max = [ @r, @g, @b ].max
170: delta = (max - min).to_f
171:
172: lum = (max + min) / 2.0
173:
174: if delta <= 1e-5
175: hue = 0
176: sat = 0
177: else
178: if (lum - 0.5) <= 1e-5
179: sat = delta / (max + min).to_f
180: else
181: sat = delta / (2 - max - min).to_f
182: end
183:
184: if @r == max
185: hue = (@g - @b) / delta.to_f
186: elsif @g == max
187: hue = (2.0 + @b - @r) / delta.to_f
188: elsif (@b - max) <= 1e-5
189: hue = (4.0 + @r - @g) / delta.to_f
190: end
191: hue /= 6.0
192:
193: hue += 1 if hue < 0
194: hue -= 1 if hue > 1
195: end
196: Color::HSL.from_fraction(hue, sat, lum)
197: end