飞利浦色相,将xy从api转换为十六进制或RGB [英] Philips hue, convert xy from api to HEX or RGB
问题描述
我正在制作一个Web界面来管理我的色相灯,但是在色彩处理方面我很挣扎.
I am making a web interface to manage my hue lamps, but i am struggling when it comes to color handling..
灯的api为我提供了 http://en.wikipedia.org的x和y坐标/wiki/CIE_1931_color_space
The api of the lamps provides me the x and y coordinates from http://en.wikipedia.org/wiki/CIE_1931_color_space
但z值不是.
我认为我必须根据亮度值或饱和度值(0到255)计算z.
I think i must calculate the z from the brightness value or saturation value (0 to 255).
但是我对颜色和数学很不满意:p.
but i am terrible at colors, and math :p.
我尝试使用那些函数 https://github.com. com/eikeon/hue-color-converter/blob/master/colorconverter.ts
但是,正如我在评论中看到的那样,那些函数没有提供正确的值...
But as i saw in the comments, thoses functions do not provide correct values...
有人可以帮我吗? ☺
ps:我需要一个javascript函数.
ps : i need a javascript function.
推荐答案
Okay so i manage to make something working with the help of : How do I convert an RGB value to a XY value for the Phillips Hue Bulb
function xyBriToRgb(x, y, bri){
z = 1.0 - x - y;
Y = bri / 255.0; // Brightness of lamp
X = (Y / y) * x;
Z = (Y / y) * z;
r = X * 1.612 - Y * 0.203 - Z * 0.302;
g = -X * 0.509 + Y * 1.412 + Z * 0.066;
b = X * 0.026 - Y * 0.072 + Z * 0.962;
r = r <= 0.0031308 ? 12.92 * r : (1.0 + 0.055) * Math.pow(r, (1.0 / 2.4)) - 0.055;
g = g <= 0.0031308 ? 12.92 * g : (1.0 + 0.055) * Math.pow(g, (1.0 / 2.4)) - 0.055;
b = b <= 0.0031308 ? 12.92 * b : (1.0 + 0.055) * Math.pow(b, (1.0 / 2.4)) - 0.055;
maxValue = Math.max(r,g,b);
r /= maxValue;
g /= maxValue;
b /= maxValue;
r = r * 255; if (r < 0) { r = 255 };
g = g * 255; if (g < 0) { g = 255 };
b = b * 255; if (b < 0) { b = 255 };
return {
r :r,
g :g,
b :b
}
}
它不是很精确,但是效果很好.如果有人设法做得更好,请在这里发表,谢谢.
Its not very very precise, but it works quite well. If someone manage to make something better, please post it here, thanks.
这篇关于飞利浦色相,将xy从api转换为十六进制或RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!