diff --git a/lib/less/functions.js b/lib/less/functions.js index 2c269a98e..eb7309c2f 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -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 diff --git a/lib/less/tree/color.js b/lib/less/tree/color.js index 8ea3fe020..eac71f7e0 100644 --- a/lib/less/tree/color.js +++ b/lib/less/tree/color.js @@ -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)); diff --git a/test/css/functions.css b/test/css/functions.css index 17509c18d..1220bd02b 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -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; diff --git a/test/less/functions.less b/test/less/functions.less index 87686beee..8a45ef9f2 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -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); @@ -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");