Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
use colorsys for color convertion
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Jul 24, 2016
1 parent 33f85c0 commit 6239661
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 40 deletions.
49 changes: 11 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var colorsys = require( 'colorsys' );
var Service, Characteristic;

module.exports = function( homebridge ) {
Expand All @@ -18,52 +19,24 @@ function RgbAccessory( log, config ) {
this.log( "Initialized '" + this.name + "'" );
}

function rgbFromHsv( h, s, v ) {
var h_ = h / 60;
var c = v * s;
var x = c * ( 1 - Math.abs( ( h_ % 2 ) - 1 ) );

var r = v - c;
var g = v - c;
var b = v - c;

if ( h_ >= 5 ) {
r += c;
b += x;
} else if ( h_ >= 4 ) {
r += x;
b += c;
} else if ( h_ >= 3 ) {
g += x;
b += c;
} else if ( h_ >= 2 ) {
g += c;
b += x;
} else if ( h_ >= 1 ) {
r += x;
g += c;
} else {
r += c;
g += x;
}

return {
r: r,
g: g,
b: b
};
}

RgbAccessory.prototype.setColor = function() {
var color = rgbFromHsv( this.hue, this.saturation / 100, this.brightness / 100 );
var color = colorsys.hsv_to_rgb( {
h: this.hue,
s: this.saturation,
v: this.brightness
} );

if ( !this.power ) {
color.r = 0;
color.g = 0;
color.b = 0;
}

this.log( "set color to", Math.round( color.r * 255 ), Math.round( color.g * 255 ), Math.round( color.b * 255 ) );
color.r = Math.round( color.r * 255 );
color.g = Math.round( color.g * 255 );
color.b = Math.round( color.b * 255 );

this.log( "set color to", color.r, color.g, color.b );
};

RgbAccessory.prototype.getServices = function() {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-fake-rgb",
"version": "0.0.1",
"version": "0.0.2",
"description": "Fake RGB Bulb plugin for homebridge: https://github.com/nfarina/homebridge",
"license": "MIT",
"keywords": [
Expand All @@ -17,5 +17,7 @@
"type": "git",
"url": "git://github.com/edjopato/homebridge-fake-rgb.git"
},
"dependencies": {}
"dependencies": {
"colorsys": "^1.0.9"
}
}

0 comments on commit 6239661

Please sign in to comment.