Problem
-------

i got a similar problem like this one:

[How to convert CIE color space into RGB or HEX color code in PHP](https://stackoverflow.com/questions/21576719/how-to-convert-cie-color-space-into-rgb-or-hex-color-code-in-php)

how to convert xy color to sRGB?
I can't get the formular working xyY. What should i enter for Y?

Setup of the environment
------------------------

i got an ikea light bulb which gives me a XY color value in the (CIE 1931 colorspace)
I would like to convert it into RGB,(sRGB) or HEX.

The Phosconn app is sending the following xy values when setting the colors by full brighness and saturation.

```
    RED   [0.735, 0.265]
    GREEN [0.115, 0.826]
    BLUE  [0.157, 0.018]

```

i figured out that the lamp shows deeper colors when i send following values:

```
    RED   [1.0, 0.0]
    GREEN [0.0, 1.0]
    BLUE  [0.0, 0.0]

```

To be more precise here is an illustration what i try to achieve:

1. Retrieve imformation from the bulb (xy color) via zigbee, convert it with javascript to RGB or HEX for the dashboard's color picker
2. The other way around does already work. Retrieving information from dashboard's color picker (RGB,brightness,saturation) convert it with JS into XY color, brightness and saturation and send it with zigbee to the bulb.
[![enter image description here](https://i.stack.imgur.com/DVM0N.png)](https://i.stack.imgur.com/DVM0N.png)

Current Implementation
----------------------

It's based on the suggested [cie-rgb-color-converter](https://www.npmjs.com/package/cie-rgb-color-converter) NPM module.

```js
function xyBriToRgb(x, y, bri){
   // bri = bri/254*100
    node.warn("XYBRI: "+x+ " | "+y+" | "+bri)
    function getReversedGammaCorrectedValue(value) {
        return value <= 0.0031308 ? 12.92 * value : (1.0 + 0.055) * Math.pow(value, (1.0 / 2.4)) - 0.055;
    }

    let xy = {
        x: x,
        y: y
    };

    let z = 1.0 - xy.x - xy.y;
    let Y = bri / 255;
    let X = (Y / xy.y) * xy.x;
    let Z = (Y / xy.y) * z;
    let r = X * 1.656492 - Y * 0.354851 - Z * 0.255038;
    let g = -X * 0.707196 + Y * 1.655397 + Z * 0.036152;
    let b = X * 0.051713 - Y * 0.121364 + Z * 1.011530;

    r = getReversedGammaCorrectedValue(r);
    g = getReversedGammaCorrectedValue(g);
    b = getReversedGammaCorrectedValue(b);

    // Bring all negative components to zero
    r = Math.max(r, 0);
    g = Math.max(g, 0);
    b = Math.max(b, 0);

    // If one component is greater than 1, weight components by that value
    let max = Math.max(r, g, b);
    if (max > 1) {
        r = r / max;
        g = g / max;
        b = b / max;
    }

    return {
        r: Math.floor(r * 255),
        g: Math.floor(g * 255),
        b: Math.floor(b * 255),
    };
}
msg.payload = xyBriToRgb(msg.payload.xy[0], msg.payload.xy[1], msg.payload.bri);
node.warn("RGB: "+ JSON.stringify(msg.payload))
return msg;

```

Results
-------

```
let rgb = ColorConverter.xyBriToRgb(0.157 ,0.018, 6);
// return {r: 64, g: 0, b: 255}

```

[![enter image description here](https://i.stack.imgur.com/YF4Tp.png)](https://i.stack.imgur.com/YF4Tp.png)

Research Material
-----------------

With the help of the fantastic guys here i found some explanations in this [Phillips HUE docs](https://gist.github.com/popcorn245/30afa0f98eea1c2fd34d)
which was leading me to a [Review of RGB color spaces](https://www.babelcolor.com/index_htm_files/A%20review%20of%20RGB%20color%20spaces.pdf)

Meanwhile i discovered some bugs inside the phosconn api or its the firmware of the bulb, that the saturation can not be set via api.

I found zigbee2mqtt page which could fix all my problems with a page fitting 100% to the model of the ikea bulb [Zigbee2MQTT IKEA LED1624G9](https://www.zigbee2mqtt.io/devices/LED1624G9.html)

Im trying to setup zigbee2mqtt for this, because i got some problems with phosconn and the api not setting correctly brightness and stuff.
Also the brightness is just the luminosity of the bulb and has here nothing to do with the color so i assume it's phosconn specific or bulb specific?