Question

I'm trying to convert some HSL value to RBG with Data.Colour module. Hackage doc said that Hue is always in the range 0-360. But there are now any ranges of Saturation and Lightness values. Are they in [0,100] or in [0,1] ranges?

I suppose that first variant is right, but seems like it is not.

λ> hsl 100 50 50
RGB {channelRed = 866.6666666666692, channelGreen = -2400.0, channelBlue = 2500.0}

Than I tried to use the range [0, 1] for both saturation and lightness.

λ> fmap truncate . (\(h,s,l) -> hsl h s l) $ (0,0,0)
RGB {channelRed = 0, channelGreen = 0, channelBlue = 0}
it :: RGB Integer

That why I'm start thinking that only Saturation should be a Double in [0,1].

For example we have some color value in HSL format.

λ> let c = (34.0,0.54,68.0)
c :: (Double, Double, Double)

Than we convert it to RGB and truncate all values

λ> fmap truncate . (\(h,s,l) -> hsl h s l) $ c
RGB {channelRed = 31, channelGreen = 63, channelBlue = 104}

But (31,63,104)::RGB is (214,54,26)::HSL like some online color-converters said.

What am I doing wrong?

Was it helpful?

Solution

It looks like the package uses the range [0, 1] for both lightness and saturation, but note that it also uses this range for RGB values, and not [0, 255] as you seem to be assuming. Taking this into account, I get (almost) the expected values:

> fmap (truncate . (* 255)) $ hsl 214 0.54 0.26
RGB {channelRed = 30, channelGreen = 61, channelBlue = 102}

OTHER TIPS

So finally I've figured out that both Saturation and Lightness value should be in the [0,1] range.

λ> fmap (round . (255*)). (\(h,s,l) -> hsl h s l) $ (34.0,0.54,0.68)
RGB {channelRed = 217, channelGreen = 179, channelBlue = 129}
it :: RGB Integer

It makes a sense, because (217,179,129)::RGB value is equal to (34,54,68)::HSL.

So, maybe it would be helpful to add that constrains in the docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top