【发布时间】:2019-07-06 22:06:33
【问题描述】:
我使用 RUCKUS API 来更改灯的颜色,我需要将颜色从 RGB 转换为 XY 以调用 API。
我试过这段代码:
1) 从颜色对象中获取 RGB 值并将它们转换为 0 到 1 之间的值
function rgb_to_cie(red, green, blue)
{
//Apply a gamma correction to the RGB values, which makes the color more vivid and more the like the color displayed on the screen of your device
var red = (red > 0.04045) ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : (red / 12.92);
var green = (green > 0.04045) ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : (green / 12.92);
var blue = (blue > 0.04045) ? Math.pow((blue + 0.055) / (1.0 + 0.055), 2.4) : (blue / 12.92);
//RGB values to XYZ using the Wide RGB D65 conversion formula
var X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
var Y = red * 0.283881 + green * 0.668433 + blue * 0.047685;
var Z = red * 0.000088 + green * 0.072310 + blue * 0.986039;
//Calculate the xy values from the XYZ values
var x = (X / (X + Y + Z)).toFixed(4);
var y = (Y / (X + Y + Z)).toFixed(4);
if (isNaN(x))
x = 0;
if (isNaN(y))
y = 0;
return [x, y];
}
但它没有提供适当的解决方案。
所以如果我通过 r:182 g: 255 B: 65 这样我得到 x 作为 22932 和 y 作为 35249 (根据 API 文档。)
我该怎么做?
【问题讨论】:
-
我假设 doc 会告诉你 X,Y 的范围。我会将值缩放到该范围。实际上 x 和 y 应该小于 1(通常),但 API 可能需要一个整数。但是您要的是 X,Y 还是 x,y?并且请不要称它为 CIE:CIE 是 CIE RGB、CIE XYZ、CIE xyz、CIE xyY、CIELAB、CIE LUV 等。
-
@GiacomoCatenazzi 它的 X 和 Y.. 对不起 CIE 我会改变的。
-
啊..我在考虑 xy 归一化(注意,你返回小的 x,y。但我没有注意到你的转换。计算假设红色、绿色和蓝色 en.wikipedia.org/wiki/SRGB#Specification_of_the_transformation 中的注释)您的 RGB(如果您使用的是网页颜色或屏幕值)已经经过伽马校正。您应该执行相反的功能:获取线性 R, G、B
-
抱歉,我没听懂你想说什么。
标签: javascript colors processing philips-hue ruckus-api