diff --git a/lib/less/functions.js b/lib/less/functions.js index 6eb34bac8..98a4517f5 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -124,6 +124,22 @@ tree.functions = { greyscale: function (color) { return this.desaturate(color, new(tree.Dimension)(100)); }, + contrast: function (color, light, dark) { + var hsl = color.toHSL(); + if (arguments.length == 1) { + if (hsl.l > 0.5) { + return this.rgba(0, 0, 0, 1.0); + } else { + return this.rgba(255, 255, 255, 1.0); + } + } else if (arguments.length == 3) { + if (hsl.l > 0.5) { + return dark; + } else { + return light; + } + } + }, e: function (str) { return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str); }, diff --git a/test/css/functions.css b/test/css/functions.css index 823281457..1d3873746 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -14,6 +14,10 @@ greyscale: #2e2e2e; spin-p: #bf6a40; spin-n: #bf4055; + contrast-white: #000000; + contrast-black: #ffffff; + contrast-light: #111111; + contrast-dark: #eeeeee; format: "rgb(32, 128, 64)"; format-string: "hello world"; format-multiple: "hello earth 2"; diff --git a/test/less/functions.less b/test/less/functions.less index 535d2efde..097adcf70 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -17,6 +17,10 @@ greyscale: greyscale(#203c31); spin-p: spin(hsl(340, 50%, 50%), 40); spin-n: spin(hsl(30, 50%, 50%), -40); + contrast-white: contrast(#fff); + contrast-black: contrast(#000); + contrast-light: contrast(#fff, #eeeeee, #111111); + contrast-dark: contrast(#000, #eeeeee, #111111); format: %("rgb(%d, %d, %d)", @r, 128, 64); format-string: %("hello %s", "world"); format-multiple: %("hello %s %d", "earth", 2);