Skip to content

Commit

Permalink
Newer, simpler luminance formula
Browse files Browse the repository at this point in the history
  • Loading branch information
deadprogram committed Jan 28, 2016
1 parent c0385c4 commit bc1826d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
50 changes: 31 additions & 19 deletions examples/luminance.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,56 @@ orb.connect(function() {
orb.color({ red: 255, green: 0, blue: 255 });

setTimeout(function() {
console.log("color 1 - 50%");
// sets color to the provided hex value, at 50% luminance
orb.color(0xff0000, 50);
console.log("color 1 -50% luminance");
// sets color to the provided hex value, at -50% luminance
orb.color(0xcc0000, -0.5);
}, 1000);

setTimeout(function() {
console.log("color 1 - 100%");
// sets color to the same hex value, at 100% luminance
orb.color(0xff0000, 100);
console.log("color 1 normal% luminance");
// sets color to the same hex value, at +50% luminance
orb.color(0xcc0000, 0);
}, 2000);

setTimeout(function() {
console.log("color 2 - 33%");
// hex numbers can also be passed in strings
orb.color("00ff00", 33);
console.log("color 1 +50% luminance");
// sets color to the same hex value, at +50% luminance
orb.color(0xcc0000, 0.5);
}, 3000);

setTimeout(function() {
console.log("color 2 - 100%");
console.log("color 2 -50% luminance");
// hex numbers can also be passed in strings
orb.color("00ff00", 100);
orb.color("00cc00", -0.5);
}, 4000);

setTimeout(function() {
console.log("color 3 - 75%");
// sets color to the provided color name
orb.color("magenta", 75);
console.log("color 2 normal% luminance");
// hex numbers can also be passed in strings
orb.color("00cc00", 0);
}, 5000);

setTimeout(function() {
console.log("color 3 - 0%");
// sets color to the provided color name
orb.color("magenta", 0);
console.log("color 2 +50% luminance");
// hex numbers can also be passed in strings
orb.color("00cc00", 0.5);
}, 6000);

setTimeout(function() {
console.log("color 3 - 100%");
console.log("color 3 -50% luminance");
// sets color to the provided color name
orb.color("magenta", 100);
orb.color("magenta", -0.5);
}, 7000);

setTimeout(function() {
console.log("color 3 normal% luminance");
// sets color to the provided color name
orb.color("magenta", 0);
}, 8000);

setTimeout(function() {
console.log("color 3 +50% luminance");
// sets color to the provided color name
orb.color("magenta", 0.5);
}, 9000);
});
29 changes: 16 additions & 13 deletions lib/devices/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,30 @@ function hexToRgb(num) {
}

/**
* Converts a hex color number to RGB values,
* adjusted by the relative luminance
* Converts a hex color number to luminance adjusted value
*
* @private
* @param {Object} hex hex color value to convert
* @param {Number} lum percentage of luminance
* @return {Object} converted color value
*/
function calculateLuminance(hex, lum) {
return Math.round(Math.min(Math.max(0, hex + (hex * lum)), 255));
}

/**
* Adjusts an RGB color by the relative luminance
*
* @private
* @param {Object} rgb rgb color value to convert
* @param {Number} lum percentage of luminance
* @return {Object} RGB color values
*/
function adjustLuminance(rgb, lum) {
var lp = lum / 100;

// calculate the full luminance
var yr = rgb.red * 0.33;
var yg = rgb.green * 0.5;
var yb = rgb.blue * 0.16;

// adjust to lum %
var newRgb = {};
newRgb.red = Math.round(yr * lp / 0.33);
newRgb.green = Math.round(yg * lp / 0.5);
newRgb.blue = Math.round(yb * lp / 0.16);
newRgb.red = calculateLuminance(rgb.red, lum);
newRgb.green = calculateLuminance(rgb.green, lum);
newRgb.blue = calculateLuminance(rgb.blue, lum);

return newRgb;
}
Expand Down
18 changes: 9 additions & 9 deletions spec/lib/devices/custom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ describe("Custom Device Functions", function() {
});

context("with luminance", function() {
it("converts to an RGB object at 50%", function() {
device.color(0xc3FF00, 50);
var color = { red: 98, blue: 0, green: 128 };
it("converts to an RGB object at +20%", function() {
device.color(0x6699cc, .2);
var color = { red: 0x7a, green: 0xb8, blue: 0xf5 };
expect(rgb).to.be.calledWith(color);
});

it("converts to an RGB object at 33%", function() {
device.color(0xc3FF00, 33);
var color = { red: 64, blue: 0, green: 84 };
it("converts to an RGB object at -50%", function() {
device.color(0x6699cc, -0.5);
var color = { red: 0x33, green: 0x4d, blue: 0x66 };
expect(rgb).to.be.calledWith(color);
});

it("converts to an RGB object at 100%", function() {
device.color(0xc3FF00, 100);
var color = { red: 195, blue: 0, green: 255 };
it("converts to an RGB object at normal %", function() {
device.color(0x6699cc, 0);
var color = { red: 0x66, green: 0x99, blue: 0xcc };
expect(rgb).to.be.calledWith(color);
});
});
Expand Down

0 comments on commit bc1826d

Please sign in to comment.