fixed several bugs in the utils

This commit is contained in:
Jan Scheiper 2020-12-24 01:08:45 +01:00
parent 304c97bdcf
commit 37eb940fbd
3 changed files with 56 additions and 7 deletions

38
package-lock.json generated
View File

@ -1,8 +1,42 @@
{ {
"name": "homecontrol-control-base", "name": "homecontrol-control-base",
"version": "1.1.0", "version": "1.3.0",
"lockfileVersion": 1, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": {
"": {
"version": "1.3.0",
"license": "UNLICENSED",
"dependencies": {
"merge-options": "^1.0.1",
"node-object-hash": "^1.4.1"
}
},
"node_modules/is-plain-obj": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz",
"integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4=",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/merge-options": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/merge-options/-/merge-options-1.0.1.tgz",
"integrity": "sha512-iuPV41VWKWBIOpBsjoxjDZw8/GbSfZ2mk7N1453bwMrfzdrIk7EzBd+8UVR6rkw67th7xnk9Dytl3J+lHPdxvg==",
"dependencies": {
"is-plain-obj": "^1.1"
},
"engines": {
"node": ">=4"
}
},
"node_modules/node-object-hash": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/node-object-hash/-/node-object-hash-1.4.1.tgz",
"integrity": "sha512-JQVqSM5/mOaUoUhCYR0t1vgm8RFo7qpJtPvnoFCLeqQh1xrfmr3BCD3nGBnACzpIEF7F7EVgqGD3O4lao/BY/A=="
}
},
"dependencies": { "dependencies": {
"is-plain-obj": { "is-plain-obj": {
"version": "1.1.0", "version": "1.1.0",

View File

@ -1,6 +1,6 @@
{ {
"name": "homecontrol-control-base", "name": "homecontrol-control-base",
"version": "1.3.0", "version": "1.3.1",
"description": "Base class which all hc-controls inherit from", "description": "Base class which all hc-controls inherit from",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -11,6 +11,15 @@ function clamp(value, min, max) {
return Math.min(max, Math.max(min, value)); return Math.min(max, Math.max(min, value));
} }
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 HSL_to_RGB(hsl) { function HSL_to_RGB(hsl) {
let h = hsl.hue / 360; let h = hsl.hue / 360;
let s = hsl.sat / 100; let s = hsl.sat / 100;
@ -41,12 +50,17 @@ function RGB_to_HSL(rgb) {
let g = rgb.green / 255; let g = rgb.green / 255;
let b = rgb.blue / 255; let b = rgb.blue / 255;
let max = Math.max(r, g, b), min = Math.min(r, g, b); let max = Math.max(r, g, b);
let min = Math.min(r, g, b);
let h = 0;
let s = 0;
if (max == min || max - min < 0.01) { if (max == min || max - min < 0.01) {
h = s = 0; // achromatic h = 0; // achromatic
s = 0;
} else { } else {
var d = max - min; let d = max - min;
s = d / (2 - max - min); s = d / (2 - max - min);
switch (max) { switch (max) {
@ -71,5 +85,6 @@ module.exports = {
fillPartialHSL, fillPartialHSL,
clamp, clamp,
HSL_to_RGB, HSL_to_RGB,
RGB_to_HSL, RGB_to_HSL,
hue2rgb,
}; };