javascriptphilips-hue

Philips hue, convert xy from api to HEX or RGB


I am making a web interface to manage my hue lamps, but i am struggling when it comes to color handling..

The api of the lamps provides me the x and y coordinates from http://en.wikipedia.org/wiki/CIE_1931_color_space

But not the z value.

I think i must calculate the z from the brightness value or saturation value (0 to 255).

but i am terrible at colors, and math :p.

I tried to use thoses functions https://github.com/eikeon/hue-color-converter/blob/master/colorconverter.ts

But as i saw in the comments, thoses functions do not provide correct values...

Could someone help me here please ? ☺

ps : i need a javascript function.


Solution

  • 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.