homebridge-hatch-baby-rest
Version:
Homebridge plugin for Hatch Rest/Restore WiFi sound machines
24 lines (23 loc) • 816 B
JavaScript
import rgb2hsv from 'pure-color/convert/rgb2hsv.js';
import hsv2rgb from 'pure-color/convert/hsv2rgb.js';
const HEX_MAX = 255;
// 0-maxValue in, 0-255 out
export function convertToHexRange(value, maxValue) {
return Math.floor((value / maxValue) * HEX_MAX);
}
export function convertFromHexRange(value, maxValue) {
return Math.floor((value * maxValue) / HEX_MAX);
}
// r,g,b
export function rgbToHsb({ r, g, b }, maxValue = HEX_MAX) {
const [h, s, v] = rgb2hsv([
convertToHexRange(r, maxValue),
convertToHexRange(g, maxValue),
convertToHexRange(b, maxValue),
]);
return { h, s, b: v };
}
export function hsbToRgb(hsb, maxValue = HEX_MAX) {
const [r, g, b] = hsv2rgb([hsb.h, hsb.s, hsb.b]).map((value) => convertFromHexRange(value, maxValue));
return { r, g, b };
}