Skip to content

Commit

Permalink
Add support for CSS color function notation to Led.RGB.color() (#1320)
Browse files Browse the repository at this point in the history
* Add support for CSS functional notation to Led.RGB.color

Adds support for CSS functional notation (e.g. `"rgb(255, 255, 0)"`) to the `color()` method on the Led.RGB prototype.

Supports:

  - `rgb(r, g, b)`
  - `rgba(r, g, b, a)` where the alpha value in the range 0-1 modifies the color as an intensity function using `RGB.ToScaledRGB()`
  - `rgba(r, g, b)` and `rgb(r, g, b, a)` to comply with the [CSS Color Module Level 4 Draft](https://drafts.csswg.org/css-color/#rgb-functions)

* Add support for functional notation without commas

Also part of the CSS color level 4 spec.

* Fix lint errors
  • Loading branch information
islemaster authored and rwaldron committed Apr 13, 2017
1 parent 548c6f1 commit e335704
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/led/rgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ RGB.ToRGB = function(red, green, blue) {
green: parseInt(input.slice(2, 4), 16),
blue: parseInt(input.slice(4, 6), 16)
};
} else if (/^rgb/.test(input)) {
var args = input.match(/^rgba?\(([^)]+)\)$/)[1].split(/[\s,]+/);
update = {
red: parseInt(args[0], 10),
green: parseInt(args[1], 10),
blue: parseInt(args[2], 10)
};
if (args.length > 3) {
update = RGB.ToScaledRGB(100 * parseFloat(args[3]), update);
}
} else {
// color name
return RGB.ToRGB(converter.keyword.rgb(input.toLowerCase()));
Expand Down
55 changes: 54 additions & 1 deletion test/rgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ exports["RGB"] = {
color: function(test) {
var rgb = this.rgb;

test.expect(36);
test.expect(46);

// returns this
test.equal(this.rgb.color("#000000"), this.rgb);
Expand Down Expand Up @@ -229,6 +229,59 @@ exports["RGB"] = {
}));
this.write.reset();

// CSS functional notation
this.rgb.color("rgb(255, 100, 50)");
test.ok(this.write.calledOnce);
test.ok(this.write.calledWith({
red: 255,
green: 100,
blue: 50
}));
this.write.reset();

// CSS functional notation with alpha
// Subtle bug: Alpha is translated to intensity using linear scaling
// when it should probably use logarithmic scaling.
this.rgb.color("rgba(255, 100, 50, 0.5)");
test.ok(this.write.calledOnce);
test.ok(this.write.calledWith({
red: 128,
green: 50,
blue: 25
}));
this.write.reset();

// CSS4 functional notation (rgb and rgba become aliases)
this.rgb.color("rgba(255, 100, 50)");
test.ok(this.write.calledOnce);
test.ok(this.write.calledWith({
red: 255,
green: 100,
blue: 50
}));
this.write.reset();

// CSS4 functional notation with alpha (rgb and rgba become aliases)
// Also testing with no spaces to make sure that's supported.
this.rgb.color("rgb(255,100,50,0.5)");
test.ok(this.write.calledOnce);
test.ok(this.write.calledWith({
red: 128,
green: 50,
blue: 25
}));
this.write.reset();

// CSS4 functional notation without commas
this.rgb.color("rgb(255 100 50)");
test.ok(this.write.calledOnce);
test.ok(this.write.calledWith({
red: 255,
green: 100,
blue: 50
}));
this.write.reset();

// bad values
test.throws(function() {
rgb.color(null);
Expand Down

0 comments on commit e335704

Please sign in to comment.