From 5c6cb3b066f956606c2a0a8f65abff7ef9567d5a Mon Sep 17 00:00:00 2001 From: peter-murray Date: Mon, 12 Oct 2015 21:53:34 +0100 Subject: [PATCH] Closes issue #60 and #61 and adds tests to support it --- hue-api/lightstate.js | 23 +++++++----- test/lightstate-tests.js | 28 +++++++++++++- test/setLightState-tests.js | 73 ++++++++++++++++++++++++++++++++++++- 3 files changed, 111 insertions(+), 13 deletions(-) diff --git a/hue-api/lightstate.js b/hue-api/lightstate.js index cc10290..33afd70 100644 --- a/hue-api/lightstate.js +++ b/hue-api/lightstate.js @@ -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)) ; }; @@ -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)) ; }; @@ -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; diff --git a/test/lightstate-tests.js b/test/lightstate-tests.js index 550b333..40021e4 100644 --- a/test/lightstate-tests.js +++ b/test/lightstate-tests.js @@ -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) { @@ -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 diff --git a/test/setLightState-tests.js b/test/setLightState-tests.js index ea7f822..1c8d118 100644 --- a/test/setLightState-tests.js +++ b/test/setLightState-tests.js @@ -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") @@ -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) { @@ -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); + } + } }); \ No newline at end of file