UNPKG

homebridge-hatch-baby-rest

Version:
25 lines (24 loc) 952 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertToHexRange = convertToHexRange; exports.convertFromHexRange = convertFromHexRange; exports.rgbToHsb = rgbToHsb; exports.hsbToRgb = hsbToRgb; const rgb2hsv = require('pure-color/convert/rgb2hsv'), hsv2rgb = require('pure-color/convert/hsv2rgb'); const HEX_MAX = 255; // 0-maxValue in, 0-255 out function convertToHexRange(value, maxValue) { return Math.floor((value / maxValue) * HEX_MAX); } function convertFromHexRange(value, maxValue) { return Math.floor((value * maxValue) / HEX_MAX); } // r,g,b function rgbToHsb({ r, g, b }, maxValue = HEX_MAX) { const [h, s, v] = rgb2hsv([r, g, b].map((value) => convertToHexRange(value, maxValue))); return { h, s, b: v }; } 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 }; }