title: how to convert Ikea light bulb color XY ( CIE 1931 colorspace ) to RGB
body: <h2>Problem</h2>
  <p>i got a similar problem like this one:</p>
  <p><a href="https://stackoverflow.com/questions/21576719/how-to-convert-cie-color-space-into-rgb-or-hex-color-code-in-php">How to convert CIE color space into RGB or HEX color code in PHP</a></p>
  <p>how to convert xy color to sRGB?
  I can't get the formular working xyY. What should i enter for Y?</p>
  <h2>Setup of the environment</h2>
  <p>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.</p>
  <p>The Phosconn app is sending the following xy values when setting the colors by full brighness and saturation.</p>
  <pre><code>    RED   [0.735, 0.265]
      GREEN [0.115, 0.826]
      BLUE  [0.157, 0.018]
  </code></pre>
  <p>i figured out that the lamp shows deeper colors when i send following values:</p>
  <pre><code>    RED   [1.0, 0.0]
      GREEN [0.0, 1.0]
      BLUE  [0.0, 0.0]
  </code></pre>
  <p>To be more precise here is an illustration what i try to achieve:</p>
  <ol>
  <li><p>Retrieve imformation from the bulb (xy color) via zigbee, convert it with javascript to RGB or HEX for the dashboard's color picker</p>
  </li>
  <li><p>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.
  <a href="https://i.stack.imgur.com/DVM0N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DVM0N.png" alt="enter image description here" /></a></p>
  </li>
  </ol>
  <h2>Current Implementation</h2>
  <p>It's based on the suggested <a href="https://www.npmjs.com/package/cie-rgb-color-converter" rel="nofollow noreferrer">cie-rgb-color-converter</a> NPM module.</p>
  <pre class="lang-js prettyprint-override"><code>function xyBriToRgb(x, y, bri){
     // bri = bri/254*100
      node.warn(&quot;XYBRI: &quot;+x+ &quot; | &quot;+y+&quot; | &quot;+bri)
      function getReversedGammaCorrectedValue(value) {
          return value &lt;= 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 &gt; 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(&quot;RGB: &quot;+ JSON.stringify(msg.payload))
  return msg;
  </code></pre>
  <h2>Results</h2>
  <pre><code>let rgb = ColorConverter.xyBriToRgb(0.157 ,0.018, 6);
  // return {r: 64, g: 0, b: 255}
  </code></pre>
  <p><a href="https://i.stack.imgur.com/YF4Tp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YF4Tp.png" alt="enter image description here" /></a></p>
  <h2>Research Material</h2>
  <p>With the help of the fantastic guys here i found some explanations in this <a href="https://gist.github.com/popcorn245/30afa0f98eea1c2fd34d" rel="nofollow noreferrer">Phillips HUE docs</a>
  which was leading me to a <a href="https://www.babelcolor.com/index_htm_files/A%20review%20of%20RGB%20color%20spaces.pdf" rel="nofollow noreferrer">Review of RGB color spaces</a></p>
  <p>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.</p>
  <p>I found zigbee2mqtt page which could fix all my problems with a page fitting 100% to the model of the ikea bulb <a href="https://www.zigbee2mqtt.io/devices/LED1624G9.html" rel="nofollow noreferrer">Zigbee2MQTT IKEA LED1624G9</a></p>
  <p>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?</p>