From 1f513a97f7a9e4baf9805c091a592e385294b43b Mon Sep 17 00:00:00 2001 From: Marcus Bointon Date: Thu, 24 Nov 2011 16:46:23 +0100 Subject: [PATCH 1/4] Add contrast function --- lib/less/functions.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/less/functions.js b/lib/less/functions.js index 96dcc8c4c..2406eb514 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -124,6 +124,14 @@ tree.functions = { greyscale: function (color) { return this.desaturate(color, new(tree.Dimension)(100)); }, + contrast: function (color) { + var hsl = color.toHSL(); + if (hsl.l > 0.5) { + return this.rgba(0, 0, 0, 1.0); + } else { + return this.rgba(255, 255, 255, 1.0); + } + }, e: function (str) { return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str); }, From 4a081ad6fb358ac17f62ebe55e85c094bda49942 Mon Sep 17 00:00:00 2001 From: Marcus Bointon Date: Thu, 24 Nov 2011 23:14:15 +0100 Subject: [PATCH 2/4] Add LESS tests for contrast --- test/less/functions.less | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/less/functions.less b/test/less/functions.less index 1af78bc80..d37e47537 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -17,6 +17,8 @@ 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); format: %("rgb(%d, %d, %d)", @r, 128, 64); format-string: %("hello %s", "world"); format-multiple: %("hello %s %d", "earth", 2); From 5f593b5857dc533985681327f96e7891b58fed3b Mon Sep 17 00:00:00 2001 From: Marcus Bointon Date: Thu, 24 Nov 2011 23:16:12 +0100 Subject: [PATCH 3/4] Add CSS test results for contrast --- test/css/functions.css | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/css/functions.css b/test/css/functions.css index f33b9869b..1024dff98 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -14,6 +14,8 @@ greyscale: #2e2e2e; spin-p: #bf6a40; spin-n: #bf4055; + contrast-white: #000000; + contrast-black: #ffffff; format: "rgb(32, 128, 64)"; format-string: "hello world"; format-multiple: "hello earth 2"; From 32e41896a6c4a303d0608a9f87279d2e7b160559 Mon Sep 17 00:00:00 2001 From: Marcus Bointon Date: Tue, 10 Jan 2012 23:49:13 +0100 Subject: [PATCH 4/4] Add optional light and dark parameters Merged latest from upstream and updated tests too, all pass. --- lib/less/functions.js | 18 +++++++++++++----- test/css/functions.css | 2 ++ test/less/functions.less | 2 ++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index 83175378e..98a4517f5 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -124,12 +124,20 @@ tree.functions = { greyscale: function (color) { return this.desaturate(color, new(tree.Dimension)(100)); }, - contrast: function (color) { + contrast: function (color, light, dark) { var hsl = color.toHSL(); - if (hsl.l > 0.5) { - return this.rgba(0, 0, 0, 1.0); - } else { - return this.rgba(255, 255, 255, 1.0); + 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) { diff --git a/test/css/functions.css b/test/css/functions.css index 1a92bef63..1d3873746 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -16,6 +16,8 @@ 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 0ad85374c..097adcf70 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -19,6 +19,8 @@ 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);