Skip to content

Commit

Permalink
Merge pull request #1890 from roelvanduijnhoven/feature/luma-definition
Browse files Browse the repository at this point in the history
Let `luma` follow spec
  • Loading branch information
lukeapage committed Feb 27, 2014
2 parents ccd8ebb + e7389a0 commit 17a92e3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/less/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ tree.functions = {
luma: function (color) {
return new(tree.Dimension)(Math.round(color.luma() * color.alpha * 100), '%');
},
luminance: function (color) {
var luminance =
(0.2126 * color.rgb[0] / 255)
+ (0.7152 * color.rgb[1] / 255)
+ (0.0722 * color.rgb[2] / 255);

return new(tree.Dimension)(Math.round(luminance * color.alpha * 100), '%');
},
saturate: function (color, amount) {
// filter: saturate(3.2);
// should be kept as is, so check for color
Expand Down
12 changes: 11 additions & 1 deletion lib/less/tree/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ var transparentKeyword = "transparent";
tree.Color.prototype = {
type: "Color",
eval: function () { return this; },
luma: function () { return (0.2126 * this.rgb[0] / 255) + (0.7152 * this.rgb[1] / 255) + (0.0722 * this.rgb[2] / 255); },
luma: function () {
var r = this.rgb[0] / 255,
g = this.rgb[1] / 255,
b = this.rgb[2] / 255;

r = (r <= 0.03928) ? r / 12.92 : Math.pow(((r + 0.055) / 1.055), 2.4);
g = (g <= 0.03928) ? g / 12.92 : Math.pow(((g + 0.055) / 1.055), 2.4);
b = (b <= 0.03928) ? b / 12.92 : Math.pow(((b + 0.055) / 1.055), 2.4);

return 0.2126 * r + 0.7152 * g + 0.0722 * b;
},

genCSS: function (env, output) {
output.add(this.toCSS(env));
Expand Down
7 changes: 6 additions & 1 deletion test/css/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
luma-blue: 7%;
luma-yellow: 93%;
luma-cyan: 79%;
luma-white-alpha: 50%;
luma-differs-from-luminance: 24%;
luminance-white: 100%;
luminance-black: 0%;
luminance-black-alpha: 0%;
luminance-red: 21%;
luminance-differs-from-luma: 36%;
contrast-filter: contrast(30%);
saturate-filter: saturate(5%);
contrast-white: #000000;
Expand Down
11 changes: 8 additions & 3 deletions test/less/functions.less
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
luma-blue: luma(#0000ff);
luma-yellow: luma(#ffff00);
luma-cyan: luma(#00ffff);
luma-white-alpha: luma(rgba(255,255,255,0.5));
luma-differs-from-luminance: luma(#ff3600);
luminance-white: luma(#fff);
luminance-black: luma(#000);
luminance-black-alpha: luma(rgba(0,0,0,0.5));
luminance-red: luma(#ff0000);
luminance-differs-from-luma: luminance(#ff3600);
contrast-filter: contrast(30%);
saturate-filter: saturate(5%);
contrast-white: contrast(#fff);
Expand All @@ -44,11 +49,11 @@
contrast-light-thresh: contrast(#fff, #111111, #eeeeee, 0.5);
contrast-dark-thresh: contrast(#000, #111111, #eeeeee, 0.5);
contrast-high-thresh: contrast(#555, #111111, #eeeeee, 0.6);
contrast-low-thresh: contrast(#555, #111111, #eeeeee, 0.1);
contrast-low-thresh: contrast(#555, #111111, #eeeeee, 0.09);
contrast-light-thresh-per: contrast(#fff, #111111, #eeeeee, 50%);
contrast-dark-thresh-per: contrast(#000, #111111, #eeeeee, 50%);
contrast-high-thresh-per: contrast(#555, #111111, #eeeeee, 60%);
contrast-low-thresh-per: contrast(#555, #111111, #eeeeee, 10%);
contrast-low-thresh-per: contrast(#555, #111111, #eeeeee, 9%);
replace: replace("Hello, Mars.", "Mars\.", "World!");
replace-captured: replace("This is a string.", "(string)\.$", "new $1.");
replace-with-flags: replace("One + one = 4", "one", "2", "gi");
Expand Down

0 comments on commit 17a92e3

Please sign in to comment.