将RGB转换为HLS并返回 [英] Converting RGB to HLS and back

查看:2495
本文介绍了将RGB转换为HLS并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python的colorsys库将RGB颜色值转换为HLS。只是为了验证,我试图转换回RGB,并得到一个不同的价值回来。



这是我的代码:

  import colorsys 
r = 192
g = 64
b = 1

hlsval = colorsys.rgb_to_hls(r,g,b)
rgbval = colorsys.hls_to_rgb(hlsval [0],hlsval [1],hlsval [2])

print hlsval,rgbval
pre>

输出:

 (0.16666666666666666,96.5,-1.0) (191.99999999999994,192.0,1.0)

绿色值已关闭超过200%



我尝试了其他一些值,每当一个组件关闭大量。

解决方案

您的价值观是方式,超出色彩空间的范围。



文档


所有这些颜色空间中的坐标是浮点值。在YIQ空间中,Y坐标在0和1之间,但I和Q坐标可以是正或负。在所有其他空格中,坐标都在0和1之间。


我猜你希望它们是字节值在0和255之间?只要先除以255:

  r,g,b = 192,64,1 
r,g,b = [x / 255.0 for x in r,g,b]
h,l,s = colorsys.rgb_to_hls(r,g,b)
r,g,b = colorsys.hls_to_rgb(h,l,s )
r,g,b = [x * 255.0 for r in r,g,b]
print r,g,b

这将给你:

  192.0 64.0 1.0 

如果你想了解为什么当你走出色彩空间的边界时,你会得到这么可笑的错误。

$首先,阅读从文档链接的两个文档,以了解为什么颜色空间之外的值是无意义的,以及为什么要求它生成值到不可能的约束。然后,要弄清楚为什么它以它的方式失败,而不是某种其他方式,你需要知道它使用哪个算法 - 这是很容易的,因为文档链接到源代码,这是纯Python和相当可读。



(PS,我可以想象9650%的亮度看起来像是什么样子,但我很想知道-100%的饱和度。

I'm using python's colorsys library to convert RGB color values to HLS. Just to verify, I tried converting back to RGB and got a different value back. I can understand minor differences because of precision issues, but these values are significantly different.

Here's my code:

import colorsys
r=192
g=64
b=1

hlsval = colorsys.rgb_to_hls(r,g,b)
rgbval=colorsys.hls_to_rgb(hlsval[0],hlsval[1],hlsval[2])

print hlsval, rgbval

Output:

(0.16666666666666666, 96.5, -1.0) (191.99999999999994, 192.0, 1.0)

The green value is off by over 200%

I tried some other values, and every time one of the components is off by a significant amount. Am I missing something?

解决方案

Your values are way, way outside the bounds of the colorspace.

From the docs:

Coordinates in all of these color spaces are floating point values. In the YIQ space, the Y coordinate is between 0 and 1, but the I and Q coordinates can be positive or negative. In all other spaces, the coordinates are all between 0 and 1.

I'm guessing you're expecting them to be byte values between 0 and 255? Just divide by 255 first:

r, g, b = 192, 64, 1
r, g, b = [x/255.0 for x in r, g, b]
h, l, s = colorsys.rgb_to_hls(r, g, b)
r, g, b = colorsys.hls_to_rgb(h, l, s)
r, g, b = [x*255.0 for x in r, g, b]
print r, g, b

This will give you:

192.0 64.0 1.0

If you want to understand why you get such ridiculous errors when you go way outside the bounds of the colorspace.

Well, first, read the two documents linked from the docs to understand why values outside the colorspace are meaningless, and why you're asking it to generate values to impossible constraints. Then, to figure out exactly why it fails in the way it does instead of some other way, you need to know which algorithm it's using—which is pretty easy, since the docs link to the source code, which is pure Python and pretty readable.

(PS, I can imagine what 9650% lightness might look like, but I'm curious about -100% saturation. Probably something Lovecraftian.)

这篇关于将RGB转换为HLS并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆