Skip to content

Commit

Permalink
Added support for xy inputs (#11). Include RGB color in the output.
Browse files Browse the repository at this point in the history
  • Loading branch information
datech committed Feb 3, 2019
1 parent 9d60486 commit cf3654b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ <h3>Outputs</h3>
<dt>colormode <span class="property-type">string</span></dt>
<dd>Indicates the color mode (cr - Color Temperature | hs - Hue and Saturation)</dd>

<dt>rgb <span class="property-type">array</span></dt>
<dd>The light color in RGB format</dd>
</dl>
</script>

Expand Down Expand Up @@ -152,5 +154,7 @@ <h3>Outputs</h3>
<dt>colormode <span class="property-type">string</span></dt>
<dd>Indicates the color mode (cr - Color Temperature | hs - Hue and Saturation)</dd>

<dt>rgb <span class="property-type">array</span></dt>
<dd>The light color in RGB format</dd>
</dl>
</script>
58 changes: 57 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit cf3654b

Please sign in to comment.