Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use HSV for reading back RGB rather than HSL #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 4 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ HttpPushRgb.prototype = {
this._httpRequest(url, '', 'GET', function(error, response, responseBody) {
if (!this._handleHttpErrorResponse('getBrightness()', error, response, responseBody, callback)) {
var rgb = responseBody;
var levels = this._rgbToHsl(
var levels = convert.rgb.hsv(
parseInt(rgb.substr(0,2),16),
parseInt(rgb.substr(2,2),16),
parseInt(rgb.substr(4,2),16)
Expand Down Expand Up @@ -425,7 +425,7 @@ HttpPushRgb.prototype = {
this.log('Caching Brightness as %s ...', level);
this.cache.brightness = level;

// If achromatic or color.brightness is false, update brightness, otherwise, update HSL as RGB
// If achromatic or color.brightness is false, update brightness, otherwise, update HSV as RGB
if (!this.color || !this.color.brightness) {
var calculatedLevel = Math.ceil(this.brightness.max / 100 * level);

Expand Down Expand Up @@ -460,7 +460,7 @@ HttpPushRgb.prototype = {
this._httpRequest(url, '', 'GET', function(error, response, responseBody) {
if (!this._handleHttpErrorResponse('getHue()', error, response, responseBody, callback)) {
var rgb = responseBody;
var levels = this._rgbToHsl(
var levels = convert.rgb.hsv(
parseInt(rgb.substr(0,2),16),
parseInt(rgb.substr(2,2),16),
parseInt(rgb.substr(4,2),16)
Expand Down Expand Up @@ -512,7 +512,7 @@ HttpPushRgb.prototype = {
this._httpRequest(url, '', 'GET', function(error, response, responseBody) {
if (!this._handleHttpErrorResponse('getSaturation()', error, response, responseBody, callback)) {
var rgb = responseBody;
var levels = this._rgbToHsl(
var levels = convert.rgb.hsv(
parseInt(rgb.substr(0,2),16),
parseInt(rgb.substr(2,2),16),
parseInt(rgb.substr(4,2),16)
Expand Down Expand Up @@ -648,43 +648,6 @@ HttpPushRgb.prototype = {
return errorOccurred;
},

/**
* Converts an RGB color value to HSL. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes r, g, and b are in [0..255] and
* returns h in [0..360], and s and l in [0..100].
*
* @param {Number} r The red color value
* @param {Number} g The green color value
* @param {Number} b The blue color value
* @return {Array} The HSL representation
*/
_rgbToHsl: function(r, g, b){
r /= 255;
g /= 255;
b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;

if(max == min){
h = s = 0; // achromatic
}else{
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}

h *= 360; // return degrees [0..360]
s *= 100; // return percent [0..100]
l *= 100; // return percent [0..100]
return [parseInt(h), parseInt(s), parseInt(l)];
},

/**
* Converts a decimal number into a hexidecimal string, with optional
* padding (default 2 characters).
Expand Down