Skip to content

Commit

Permalink
Closes issue #60 and #61 and adds tests to support it
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-murray authored and peter-murray committed Oct 12, 2015
1 parent 6f1ceff commit 5c6cb3b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 13 deletions.
23 changes: 13 additions & 10 deletions hue-api/lightstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,17 @@ State.prototype.white = function (colorTemp, brightPercentage) {
* @return {State}
*/
State.prototype.hsl = function (hue, saturation, luminosity) {
var t = saturation * (luminosity<50 ? luminosity : 100-luminosity) / 100; // Temp value
saturation = Math.round( 200 * t / (luminosity+t) ) |0;
luminosity = Math.round( t + luminosity );
var temp = saturation * (luminosity < 50 ? luminosity : 100 - luminosity) / 100
, satValue = Math.round(200 * temp / (luminosity + temp)) | 0
, luminosityValue = Math.round(temp + luminosity)
;

var hueValue = _getBoundedValue(hue, 0, 360) * 182.5487; // degrees upscaled to 0-65535 range
console.log("temp: %s, satValue: %s, luminosity: %s", temp, satValue, luminosityValue)

return this
.brightness(luminosity)
.hue(hueValue)
.sat(_convertSaturationPercentToHueValue(saturation))
.brightness(luminosityValue)
.hue(_convertHueToHueValue(hue))
.sat(_convertSaturationPercentToHueValue(satValue))
;
};

Expand All @@ -353,11 +354,9 @@ State.prototype.hsl = function (hue, saturation, luminosity) {
* @return {State}
*/
State.prototype.hsb = function (hue, saturation, brightness) {
var hueValue = _getBoundedValue(hue, 0, 360) * 182.5487; // degrees upscaled to 0-65535 range

return this
.brightness(brightness)
.hue(hueValue)
.hue(_convertHueToHueValue(hue))
.sat(_convertSaturationPercentToHueValue(saturation))
;
};
Expand Down Expand Up @@ -552,6 +551,10 @@ function _getWhiteState(colorTemp, brightness) {
/////////////////////////////////
// Value Functions

function _convertHueToHueValue(hue) {
return _getBoundedValue(hue, 0, 360) * 182.5487
}

function _convertMilliSecondsToTransitionTime(value) {
var result = 0;

Expand Down
28 changes: 26 additions & 2 deletions test/lightstate-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,30 @@ describe("#LightState", function () {
});


describe("#hsb", function() {

function test(h, s, b, expectedHue, expectedSat, expectedBri) {
state.hsb(h, s, b);
validateHueState(expectedHue);
validateSatState(expectedSat);
validateBriState(expectedBri);
}

it("should set (0, 0, 0)", function() {
test(0, 0, 0, 0, 0, 0);
});

it("should set (360, 100, 100)", function() {
test(360, 100, 100, 65535, 255, 255);
});

it("should set (180, 50, 25)", function() {
test(180, 50, 25, 32858, 127, 63);
});

//TODO validate limits on each parameter
});

describe("#hsl", function() {

function test(h, s, l, expectedHue, expectedSat, expectedBri) {
Expand All @@ -624,11 +648,11 @@ describe("#LightState", function () {
});

it("should set (360, 100, 100)", function() {
test(360, 100, 100, 65535, 255, 255);
test(360, 100, 100, 65535, 0, 255);
});

it("should set (180, 50, 25)", function() {
test(180, 50, 25, 32858, 127, 63);
test(180, 50, 25, 32858, 170, 96);
});

//TODO validate limits on each parameter
Expand Down
73 changes: 72 additions & 1 deletion test/setLightState-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

var expect = require("chai").expect
, assert = require("chai").assert
, HueApi = require("..").HueApi
, lightState = require("..").lightState
, testValues = require("./support/testValues.js")
Expand Down Expand Up @@ -147,6 +146,65 @@ describe("Hue API", function () {
});
});

describe("set hsb(0, 100, 100)", function() {

it("using #promise", function(done) {
state.on().hsb(0, 100, 100);
hue.setLightState(lightId, state)
.then(function(result) {
expect(result).to.be.true;
return hue.getLightStatus(lightId);
})
.then(function(light) {
var state;

expect(light).to.have.property("state");
state = light.state;

expect(state).to.have.property("hue", 0);
expect(state).to.have.property("sat", 254);
expect(state).to.have.property("bri", 254);
done();
})
.done();
});
});

describe("set hsl(0, 100, 100)", function() {

it("using #promise", function(done) {
state.on().hsl(0, 100, 100);
hue.setLightState(lightId, state)
.then(function(result) {
expect(result).to.be.true;
return hue.getLightStatus(lightId);
})
.then(function(light) {
validateHSBState(0, 0, 254)(light);
done();
})
.done();
});
});

describe("set hsl(0, 100, 50)", function() {

it("using #promise", function(done) {
state.on().hsl(0, 100, 50);
hue.setLightState(lightId, state)
.then(function(result) {
expect(result).to.be.true;
return hue.getLightStatus(lightId);
})
.then(function(light) {
validateHSBState(0, 254, 254)(light);
done();
})
.done();
});

});

//TODO need to put this back in and cater for callbacks
// it("should report error on an invalid state", function (done) {
// function checkError(error) {
Expand Down Expand Up @@ -236,4 +294,17 @@ describe("Hue API", function () {
});
});
});

function validateHSBState(hue, sat, bri) {
return function(light) {
var state;

expect(light).to.have.property("state");
state = light.state;

expect(state).to.have.property("hue", hue);
expect(state).to.have.property("sat", sat);
expect(state).to.have.property("bri", bri);
}
}
});

0 comments on commit 5c6cb3b

Please sign in to comment.