iobroker.signifylights
Version:
Signify Device Adapter
203 lines (173 loc) • 5.31 kB
JavaScript
/**
* Converts an RGB color value to HSL. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes r, g, and b are contained in the set [0, 255] and
* returns h, s, and l in the set [0, 1].
* https://gist.github.com/mjackson/5311256
*
* @param Number r The red color value
* @param Number g The green color value
* @param Number b The blue color value
* @return Array The HSL representation
*/
function RGB2HSL(r, g, b) {
r /= 255, g /= 255, b /= 255;
const max = Math.max(r, g, b), min = Math.min(r, g, b);
// eslint-disable-next-line prefer-const
let h, s, l = (max + min) / 2;
if (max == min) {
h = s = 0; // achromatic
} else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
// return [h, s, l];
// return [h * 360, s * 100, l * 100];
return [Math.round(h * 360), Math.round(s * 100), Math.round(l * 100)];
}
/**
* Converts an HSL color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes h, s, and l are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param Number h The hue
* @param Number s The saturation
* @param Number l The lightness
* @return Array The RGB representation
*/
function HSL2RGB(h, s, l) {
h = h / 360;
s = s / 100;
l = l / 100;
let r, g, b;
if (s == 0) {
r = g = b = l; // achromatic
} else {
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = HUE2RGB(p, q, h + 1 / 3);
g = HUE2RGB(p, q, h);
b = HUE2RGB(p, q, h - 1 / 3);
}
// return [r * 255, g * 255, b * 255];
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
/**
* Converts an RGB color value to HSV. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes r, g, and b are contained in the set [0, 255] and
* returns h, s, and v in the set [0, 1].
*
* @param Number r The red color value
* @param Number g The green color value
* @param Number b The blue color value
* @return Array The HSV representation
*/
function RGB2HSV(r, g, b) {
r /= 255, g /= 255, b /= 255;
const max = Math.max(r, g, b), min = Math.min(r, g, b);
// eslint-disable-next-line prefer-const
let h, s, v = max;
const d = max - min;
// eslint-disable-next-line prefer-const
s = max == 0 ? 0 : d / max;
if (max == min) {
h = 0; // achromatic
} else {
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
// return [h, s, v];
// return [h * 360, s * 100, v * 100];
return [Math.round(h * 360), Math.round(s * 100), Math.round(v * 100)];
}
/**
* Converts an HSV color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes h, s, and v are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param Number h The hue
* @param Number s The saturation
* @param Number v The value
* @return Array The RGB representation
*/
function HSV2RGB(h, s, v) {
h = h / 360;
s = s / 100;
v = v / 100;
let r, g, b;
const i = Math.floor(h * 6);
const f = h * 6 - i;
const p = v * (1 - s);
const q = v * (1 - f * s);
const t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
// return [r * 255, g * 255, b * 255];
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
// eslint-disable-next-line no-unused-vars
function RGB2HUE(r, g, b) {
return 0;
}
/**
*
* @param {*} p
* @param {*} q
* @param {*} t
*/
function HUE2RGB(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
}
function componentToHex(c) {
const hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function RGB2HEX(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function HEX2RGB(hex) {
if(hex.length == 7){
hex = hex.substring(1,6);
}
const aRgbHex = hex.match(/.{1,2}/g);
const aRgb = [
parseInt(aRgbHex[0], 16),
parseInt(aRgbHex[1], 16),
parseInt(aRgbHex[2], 16)
];
return aRgb;
}
module.exports = {
RGB2HEX,
RGB2HUE,
RGB2HSL,
RGB2HSV,
HSL2RGB,
HSV2RGB,
HUE2RGB,
HEX2RGB
};