Convert HSL to RBG

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

I assume the first option is right, but it 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 saturation and ease.

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

That's why I'm starting to think that only Saturation should be 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) 

Then we convert it to RGB and crop all the values

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

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

What am I doing wrong?

+4
source share
2 answers

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

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

So, I realized that the values ​​of Saturation and Lightness should be in the range [0,1].

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

This makes sense because the value of (217,179,129)::RGB is (34,54,68)::HSL .

So, it might be useful to add these restrictions to the documents.

+1
source

Source: https://habr.com/ru/post/1396938/


All Articles