zigbee-shepherd-converters
Version:
Collection of device converters to be used with zigbee-shepherd
67 lines (55 loc) • 2.13 kB
JavaScript
;
/**
* From: https://github.com/usolved/cie-rgb-converter/blob/master/cie_rgb_converter.js
* Converts RGB color space to CIE color space
* @param {Number} red
* @param {Number} green
* @param {Number} blue
* @return {Array} Array that contains the CIE color values for x and y
*/
function rgbToXY(red, green, blue) {
// The RGB values should be between 0 and 1. So convert them.
// The RGB color (255, 0, 100) becomes (1.0, 0.0, 0.39)
red /= 255; green /= 255; blue /= 255;
// 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
red = (red > 0.04045) ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : (red / 12.92);
green = (green > 0.04045) ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : (green / 12.92);
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
const X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
const Y = red * 0.283881 + green * 0.668433 + blue * 0.047685;
const Z = red * 0.000088 + green * 0.072310 + blue * 0.986039;
// Calculate the xy values from the XYZ values
let x = (X / (X + Y + Z)).toFixed(4);
let y = (Y / (X + Y + Z)).toFixed(4);
if (isNaN(x)) {
x = 0;
}
if (isNaN(y)) {
y = 0;
}
return {x: Number.parseFloat(x), y: Number.parseFloat(y)};
}
function hexToXY(hex) {
const rgb = hexToRgb(hex);
return rgbToXY(rgb.r, rgb.g, rgb.b);
}
function hexToRgb(hex) {
hex = hex.replace('#', '');
const bigint = parseInt(hex, 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;
return {r: r, g: g, b: b};
}
function getKeyByValue(object, value, fallback) {
const key = Object.keys(object).find((k) => object[k] === value);
return key != null ? Number(key) : (fallback || 0);
}
module.exports = {
rgbToXY: rgbToXY,
hexToXY: hexToXY,
hexToRgb: hexToRgb,
getKeyByValue: getKeyByValue,
};