将 HSL 转换为 RBG

发布于 2025-01-06 09:03:41 字数 1099 浏览 7 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

享受孤独 2025-01-13 09:03:41

看起来该包使用范围 [0, 1] 来表示亮度和饱和度,但请注意,它也使用此范围来表示 RGB 值,而不是 [0, 255]代码> 正如您似乎假设的那样。考虑到这一点,我得到(几乎)预期值:

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

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}
浮萍、无处依 2025-01-13 09:03:41

所以最后我发现 SaturationLightness 值都应该在 [0,1] 范围内。

λ> 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

这是有道理的,因为 (217,179,129)::RGB 值等于 (34,54,68)::HSL

因此,也许在文档中添加该约束会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文