Created
November 12, 2011 05:27
-
-
Save unknownbrackets/1360092 to your computer and use it in GitHub Desktop.
less/less.js#454 - Properly handle percentages for color channels.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 017e4e7a3fcbb3dfbd8d8341aaccfd1df947b5e2 Mon Sep 17 00:00:00 2001 | |
From: Unknown W. Brackets <checkins@unknownbrackets.org> | |
Date: Fri, 11 Nov 2011 21:22:25 -0800 | |
Subject: [PATCH] Properly handle percentages for color channels - issue 454. | |
--- | |
lib/less/functions.js | 10 +++++++++- | |
test/css/colors.css | 3 +++ | |
test/less/colors.less | 4 ++++ | |
3 files changed, 16 insertions(+), 1 deletions(-) | |
diff --git a/lib/less/functions.js b/lib/less/functions.js | |
index 96dcc8c..3df47bd 100644 | |
--- a/lib/less/functions.js | |
+++ b/lib/less/functions.js | |
@@ -5,7 +5,7 @@ tree.functions = { | |
return this.rgba(r, g, b, 1.0); | |
}, | |
rgba: function (r, g, b, a) { | |
- var rgb = [r, g, b].map(function (c) { return number(c) }), | |
+ var rgb = [r, g, b].map(function (c) { return scaled(c, 255) }), | |
a = number(a); | |
return new(tree.Color)(rgb, a); | |
}, | |
@@ -178,6 +178,14 @@ function number(n) { | |
} | |
} | |
+function scaled(n, size) { | |
+ if (n instanceof tree.Dimension && n.unit == '%') { | |
+ return parseFloat(n.value * size / 100); | |
+ } else { | |
+ return number(n); | |
+ } | |
+} | |
+ | |
function clamp(val) { | |
return Math.min(1, Math.max(0, val)); | |
} | |
diff --git a/test/css/colors.css b/test/css/colors.css | |
index 540f9b0..7b08e62 100644 | |
--- a/test/css/colors.css | |
+++ b/test/css/colors.css | |
@@ -40,6 +40,9 @@ | |
#grey { | |
color: #c8c8c8; | |
} | |
+#dark { | |
+ color: #333333; | |
+} | |
#808080 { | |
color: #808080; | |
} | |
diff --git a/test/less/colors.less b/test/less/colors.less | |
index 5744e16..1c2c18a 100644 | |
--- a/test/less/colors.less | |
+++ b/test/less/colors.less | |
@@ -43,6 +43,10 @@ | |
color: rgb(200, 200, 200); | |
} | |
+#dark { | |
+ color: rgb(20%, 20%, 20%); | |
+} | |
+ | |
#808080 { | |
color: hsl(50, 0%, 50%); | |
} | |
-- | |
1.7.3.1.msysgit.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment