cleaned up the code a bit

This commit is contained in:
jangxx 2019-03-02 13:33:58 +01:00
parent c650a60703
commit 7dfa195cd1
3 changed files with 31 additions and 32 deletions

View File

@ -236,24 +236,15 @@ class HCMagicHome extends HCColorLamp {
module.exports = HCMagicHome; module.exports = HCMagicHome;
function HSL_to_RGB(hsl) { function HSL_to_RGB(hsl) {
var h = hsl.hue / 360; let h = hsl.hue / 360;
var s = hsl.sat / 100; let s = hsl.sat / 100;
var l = hsl.l / 100; let l = hsl.l / 100;
var r, g, b; let r, g, b;
if (s == 0) { if (s == 0) {
r = g = b = l; // achromatic r = g = b = l; // achromatic
} else { } else {
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;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s; var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q; var p = 2 * l - q;
@ -269,12 +260,21 @@ function HSL_to_RGB(hsl) {
}; };
} }
function RGB_to_HSL(rgb) { function hue2rgb(p, q, t) {
var r = rgb.red / 255; if (t < 0) t += 1;
var g = rgb.green / 255; if (t > 1) t -= 1;
var b = rgb.blue / 255; 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;
}
var max = Math.max(r, g, b), min = Math.min(r, g, b); function RGB_to_HSL(rgb) {
let r = rgb.red / 255;
let g = rgb.green / 255;
let b = rgb.blue / 255;
let max = Math.max(r, g, b), min = Math.min(r, g, b);
if (max == min || max - min < 0.01) { if (max == min || max - min < 0.01) {
h = s = 0; // achromatic h = s = 0; // achromatic
@ -291,7 +291,7 @@ function RGB_to_HSL(rgb) {
h /= 6; h /= 6;
} }
var l = 1 - 0.5 * s; let l = 1 - 0.5 * s;
return { return {
hue: Math.round(h * 360), hue: Math.round(h * 360),
@ -301,13 +301,13 @@ function RGB_to_HSL(rgb) {
} }
function extractBrightness(rgb) { function extractBrightness(rgb) {
var max = Math.max(rgb.red, rgb.green, rgb.blue); let max = Math.max(rgb.red, rgb.green, rgb.blue);
return (max / 255) * 100; return (max / 255) * 100;
} }
function removeBrightness(rgb) { function removeBrightness(rgb) {
var max = Math.max(rgb.red, rgb.green, rgb.blue); let max = Math.max(rgb.red, rgb.green, rgb.blue);
var scale = (max > 0) ? 255 / max : 0; let scale = (max > 0) ? 255 / max : 0;
return { return {
red: rgb.red * scale, red: rgb.red * scale,
@ -322,9 +322,9 @@ function fillPartialHSL(newcolor, oldcolor) {
} }
//console.log(newcolor, oldcolor); //console.log(newcolor, oldcolor);
var color = {}; let color = {};
color["hue"] = (newcolor.hue != null) ? newcolor.hue : oldcolor.hue; color.hue = (newcolor.hue != null) ? newcolor.hue : oldcolor.hue;
color["sat"] = (newcolor.sat != null) ? newcolor.sat : oldcolor.sat; color.sat = (newcolor.sat != null) ? newcolor.sat : oldcolor.sat;
color["l"] = (newcolor.l != null) ? newcolor.l : oldcolor.l; color.l = (newcolor.l != null) ? newcolor.l : oldcolor.l;
return color; return color;
} }

9
package-lock.json generated
View File

@ -1,15 +1,14 @@
{ {
"name": "hc-magichome", "name": "hc-magichome",
"version": "1.0.1", "version": "1.0.2",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"homecontrol-control-base": { "homecontrol-control-base": {
"version": "git+https://git.literalchaos.de/jan/homecontrol-control-base.git#b7ed807affe888d61efd9dbff3d55bcc128215b7", "version": "git+https://git.literalchaos.de/jan/homecontrol-control-base.git#b7ed807affe888d61efd9dbff3d55bcc128215b7",
"from": "git+https://git.literalchaos.de/jan/homecontrol-control-base.git",
"requires": { "requires": {
"merge-options": "^1.0.1", "merge-options": "1.0.1",
"node-object-hash": "^1.4.1" "node-object-hash": "1.4.2"
} }
}, },
"is-plain-obj": { "is-plain-obj": {
@ -27,7 +26,7 @@
"resolved": "https://registry.npmjs.org/merge-options/-/merge-options-1.0.1.tgz", "resolved": "https://registry.npmjs.org/merge-options/-/merge-options-1.0.1.tgz",
"integrity": "sha512-iuPV41VWKWBIOpBsjoxjDZw8/GbSfZ2mk7N1453bwMrfzdrIk7EzBd+8UVR6rkw67th7xnk9Dytl3J+lHPdxvg==", "integrity": "sha512-iuPV41VWKWBIOpBsjoxjDZw8/GbSfZ2mk7N1453bwMrfzdrIk7EzBd+8UVR6rkw67th7xnk9Dytl3J+lHPdxvg==",
"requires": { "requires": {
"is-plain-obj": "^1.1" "is-plain-obj": "1.1.0"
} }
}, },
"node-object-hash": { "node-object-hash": {

View File

@ -1,6 +1,6 @@
{ {
"name": "hc-magichome", "name": "hc-magichome",
"version": "1.0.2", "version": "1.0.3",
"description": "Magic Home Plugin for Homecontrol", "description": "Magic Home Plugin for Homecontrol",
"main": "HCMagicHome.js", "main": "HCMagicHome.js",
"scripts": { "scripts": {