From cf3654b0efdddc808196c4d943faf0cb63b096ea Mon Sep 17 00:00:00 2001 From: Teodor Ivanov Date: Sun, 3 Feb 2019 11:24:31 +0200 Subject: [PATCH] Added support for xy inputs (#11). Include RGB color in the output. --- index.html | 4 ++++ index.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++- package-lock.json | 13 +++++++++++ package.json | 3 ++- 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 634f3de..8f2e5fb 100644 --- a/index.html +++ b/index.html @@ -92,6 +92,8 @@

Outputs

colormode string
Indicates the color mode (cr - Color Temperature | hs - Hue and Saturation)
+
rgb array
+
The light color in RGB format
@@ -152,5 +154,7 @@

Outputs

colormode string
Indicates the color mode (cr - Color Temperature | hs - Hue and Saturation)
+
rgb array
+
The light color in RGB format
diff --git a/index.js b/index.js index eaed8ca..03fb30a 100644 --- a/index.js +++ b/index.js @@ -324,6 +324,13 @@ module.exports = function(RED) { function setDeviceAttributes(id, attributes, context) { + if (attributes.xy !== undefined && attributes.xy !== null) { + var xy = attributes.xy; + var hsb = colorXYY2SHB(xy[0], xy[1], 100); + attributes.hue = hsb[0]; + attributes.sat = hsb[1]; + } + var currentAttributes = getDeviceAttributes(id, context); for (var key in currentAttributes) { @@ -350,11 +357,60 @@ module.exports = function(RED) { function payloadHandler(hubNode, deviceId) { var msg = getDeviceAttributes(deviceId, hubNode.context()); + msg.rgb = colorSHB2RGB(msg.hue, msg.sat, 254); msg.payload = msg.on ? "on" : "off"; msg.deviceid = deviceId; - msg.topic= ""; + msg.topic = ""; hubNode.send(msg); } + // + // Colors conversion + // + var colorConvert = require('color-convert'); + + function colorXYY2XYZ(xs, ys, yc){ + var xc = xs * yc / ys; + var zc = (1 - xs - ys) * yc / ys; + + return [xc, yc, zc]; + } + + function colorXYZ2SHV(x, y, z){ + var hsv = colorConvert.xyz.hsv(x, y, z); + + return hsv; + } + + function colorSHV2HSB(h, s, v){ + var hh = h / 360 * 65535; + var ss = s / 100 * 254; + var bb = v / 100 * 254; + + return [hh, ss, bb] + } + + function colorSHB2HSV(h, s, b){ + var hh = h * 360 / 65535; + var ss = s * 100 / 254; + var vv = b * 100 / 254; + + return [hh, ss, vv] + } + + function colorXYY2SHB(xs, ys, yc){ + var xyz = colorXYY2XYZ(xs, ys, yc); + var hsv = colorXYZ2SHV(xyz[0], xyz[1], xyz[2]); + var hsb = colorSHV2HSB(hsv[0], hsv[1], hsv[2]); + + return hsb; + } + + function colorSHB2RGB(h, s, b){ + var hsv = colorSHB2HSV(h, s, b); + var rgb = colorConvert.hsv.rgb(hsv[0], hsv[1], hsv[2]); + + return rgb; + } } diff --git a/package-lock.json b/package-lock.json index 4fbccb6..6dffa57 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53,6 +53,19 @@ "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=" }, + "color-convert": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.0.tgz", + "integrity": "sha512-hzTicsCJIHdxih9+2aLR1tNGZX5qSJGRHDPVwSY26tVrEf55XNajLOBWz2UuWSIergszA09/bqnOiHyqx9fxQg==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, "content-disposition": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.2.tgz", diff --git a/package.json b/package.json index 5a72e74..81b75b8 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "stoppable": "^1.0.4", "mustache": "^3.0.1", "express": "^4.16.4", - "body-parser": "^1.18.3" + "body-parser": "^1.18.3", + "color-convert": "^2.0.0" } }