def to_hsl
min = [ @r, @g, @b ].min
max = [ @r, @g, @b ].max
delta = (max - min).to_f
lum = (max + min) / 2.0
if Color.near_zero?(delta)
hue = 0
sat = 0
else
if Color.near_zero_or_less?(lum - 0.5)
sat = delta / (max + min).to_f
else
sat = delta / (2 - max - min).to_f
end
sixth = 1 / 6.0
if @r == max
hue = (sixth * ((@g - @b) / delta))
hue += 1.0 if @g < @b
elsif @g == max
hue = (sixth * ((@b - @r) / delta)) + (1.0 / 3.0)
elsif @b == max
hue = (sixth * ((@r - @g) / delta)) + (2.0 / 3.0)
end
hue += 1 if hue < 0
hue -= 1 if hue > 1
end
Color::HSL.from_fraction(hue, sat, lum)
end