Skip to content

Commit 17a92e3

Browse files
committed
Merge pull request #1890 from roelvanduijnhoven/feature/luma-definition
Let `luma` follow spec
2 parents ccd8ebb + e7389a0 commit 17a92e3

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

lib/less/functions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ tree.functions = {
9595
luma: function (color) {
9696
return new(tree.Dimension)(Math.round(color.luma() * color.alpha * 100), '%');
9797
},
98+
luminance: function (color) {
99+
var luminance =
100+
(0.2126 * color.rgb[0] / 255)
101+
+ (0.7152 * color.rgb[1] / 255)
102+
+ (0.0722 * color.rgb[2] / 255);
103+
104+
return new(tree.Dimension)(Math.round(luminance * color.alpha * 100), '%');
105+
},
98106
saturate: function (color, amount) {
99107
// filter: saturate(3.2);
100108
// should be kept as is, so check for color

lib/less/tree/color.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@ var transparentKeyword = "transparent";
2828
tree.Color.prototype = {
2929
type: "Color",
3030
eval: function () { return this; },
31-
luma: function () { return (0.2126 * this.rgb[0] / 255) + (0.7152 * this.rgb[1] / 255) + (0.0722 * this.rgb[2] / 255); },
31+
luma: function () {
32+
var r = this.rgb[0] / 255,
33+
g = this.rgb[1] / 255,
34+
b = this.rgb[2] / 255;
35+
36+
r = (r <= 0.03928) ? r / 12.92 : Math.pow(((r + 0.055) / 1.055), 2.4);
37+
g = (g <= 0.03928) ? g / 12.92 : Math.pow(((g + 0.055) / 1.055), 2.4);
38+
b = (b <= 0.03928) ? b / 12.92 : Math.pow(((b + 0.055) / 1.055), 2.4);
39+
40+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
41+
},
3242

3343
genCSS: function (env, output) {
3444
output.add(this.toCSS(env));

test/css/functions.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
luma-blue: 7%;
2525
luma-yellow: 93%;
2626
luma-cyan: 79%;
27-
luma-white-alpha: 50%;
27+
luma-differs-from-luminance: 24%;
28+
luminance-white: 100%;
29+
luminance-black: 0%;
30+
luminance-black-alpha: 0%;
31+
luminance-red: 21%;
32+
luminance-differs-from-luma: 36%;
2833
contrast-filter: contrast(30%);
2934
saturate-filter: saturate(5%);
3035
contrast-white: #000000;

test/less/functions.less

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
luma-blue: luma(#0000ff);
2929
luma-yellow: luma(#ffff00);
3030
luma-cyan: luma(#00ffff);
31-
luma-white-alpha: luma(rgba(255,255,255,0.5));
31+
luma-differs-from-luminance: luma(#ff3600);
32+
luminance-white: luma(#fff);
33+
luminance-black: luma(#000);
34+
luminance-black-alpha: luma(rgba(0,0,0,0.5));
35+
luminance-red: luma(#ff0000);
36+
luminance-differs-from-luma: luminance(#ff3600);
3237
contrast-filter: contrast(30%);
3338
saturate-filter: saturate(5%);
3439
contrast-white: contrast(#fff);
@@ -44,11 +49,11 @@
4449
contrast-light-thresh: contrast(#fff, #111111, #eeeeee, 0.5);
4550
contrast-dark-thresh: contrast(#000, #111111, #eeeeee, 0.5);
4651
contrast-high-thresh: contrast(#555, #111111, #eeeeee, 0.6);
47-
contrast-low-thresh: contrast(#555, #111111, #eeeeee, 0.1);
52+
contrast-low-thresh: contrast(#555, #111111, #eeeeee, 0.09);
4853
contrast-light-thresh-per: contrast(#fff, #111111, #eeeeee, 50%);
4954
contrast-dark-thresh-per: contrast(#000, #111111, #eeeeee, 50%);
5055
contrast-high-thresh-per: contrast(#555, #111111, #eeeeee, 60%);
51-
contrast-low-thresh-per: contrast(#555, #111111, #eeeeee, 10%);
56+
contrast-low-thresh-per: contrast(#555, #111111, #eeeeee, 9%);
5257
replace: replace("Hello, Mars.", "Mars\.", "World!");
5358
replace-captured: replace("This is a string.", "(string)\.$", "new $1.");
5459
replace-with-flags: replace("One + one = 4", "one", "2", "gi");

0 commit comments

Comments
 (0)