将 HSL 转换为 RBG
我正在尝试使用 Data.Colour 模块将一些 HSL
值转换为 RBG
。 Hackage 文档说 Hue
始终在 0-360 范围内。但现在有任意范围的饱和度
和亮度
值。它们是在 [0,100] 还是 [0,1] 范围内?
我认为第一个变体是正确的,但似乎不是。
λ> hsl 100 50 50
RGB {channelRed = 866.6666666666692, channelGreen = -2400.0, channelBlue = 2500.0}
我尝试使用范围 [0, 1] 来表示饱和度和亮度。
λ> fmap truncate . (\(h,s,l) -> hsl h s l) $ (0,0,0)
RGB {channelRed = 0, channelGreen = 0, channelBlue = 0}
it :: RGB Integer
这就是为什么我开始认为只有 Saturation
应该是 [0,1]
中的 Double
。
例如,我们有一些 HSL
格式的颜色值。
λ> let c = (34.0,0.54,68.0)
c :: (Double, Double, Double)
然后我们将其转换为 RGB 并截断所有值
λ> fmap truncate . (\(h,s,l) -> hsl h s l) $ c
RGB {channelRed = 31, channelGreen = 63, channelBlue = 104}
,但是 (31,63,104)::RGB
是 (214,54,26)::HSL
就像一些在线颜色转换器所说。
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来该包使用范围
[0, 1]
来表示亮度和饱和度,但请注意,它也使用此范围来表示 RGB 值,而不是[0, 255]
代码> 正如您似乎假设的那样。考虑到这一点,我得到(几乎)预期值: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:所以最后我发现
Saturation
和Lightness
值都应该在 [0,1] 范围内。这是有道理的,因为
(217,179,129)::RGB
值等于(34,54,68)::HSL
。因此,也许在文档中添加该约束会有所帮助。
So finally I've figured out that both
Saturation
andLightness
value should be in the [0,1] range.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.