From 862d7ed0ab9d42995a268e07714868ac85e48366 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Sat, 14 Sep 2013 12:08:08 +0100 Subject: [PATCH] support transparent as a color but do not alter existing output of transparent and rgba() --- lib/less/colors.js | 1 - lib/less/parser.js | 9 ++++----- lib/less/tree/color.js | 18 ++++++++++++++++++ test/css/functions.css | 4 ++++ test/less/functions.less | 4 ++++ 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/less/colors.js b/lib/less/colors.js index b417af67..e509b602 100644 --- a/lib/less/colors.js +++ b/lib/less/colors.js @@ -140,7 +140,6 @@ 'teal':'#008080', 'thistle':'#d8bfd8', 'tomato':'#ff6347', - // 'transparent':'rgba(0,0,0,0)', 'turquoise':'#40e0d0', 'violet':'#ee82ee', 'wheat':'#f5deb3', diff --git a/lib/less/parser.js b/lib/less/parser.js index 1dd9a26e..61035dc7 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -657,12 +657,11 @@ less.Parser = function Parser(env) { var k; if (k = $(/^[_A-Za-z-][_A-Za-z0-9-]*/)) { - if (tree.colors.hasOwnProperty(k)) { - // detect named color - return new(tree.Color)(tree.colors[k].slice(1)); - } else { - return new(tree.Keyword)(k); + var color = tree.Color.fromKeyword(k); + if (color) { + return color; } + return new(tree.Keyword)(k); } }, diff --git a/lib/less/tree/color.js b/lib/less/tree/color.js index c5ad64fc..2e45db11 100644 --- a/lib/less/tree/color.js +++ b/lib/less/tree/color.js @@ -22,6 +22,9 @@ tree.Color = function (rgb, a) { } this.alpha = typeof(a) === 'number' ? a : 1; }; + +var transparentKeyword = "transparent"; + tree.Color.prototype = { type: "Color", eval: function () { return this; }, @@ -38,6 +41,9 @@ tree.Color.prototype = { // which has better compatibility with older browsers. // Values are capped between `0` and `255`, rounded and zero-padded. if (this.alpha < 1.0) { + if (this.alpha === 0 && this.isTransparentKeyword) { + return transparentKeyword; + } return "rgba(" + this.rgb.map(function (c) { return Math.round(c); }).concat(this.alpha).join(',' + (compress ? '' : ' ')) + ")"; @@ -156,5 +162,17 @@ tree.Color.prototype = { } }; +tree.Color.fromKeyword = function(keyword) { + if (tree.colors.hasOwnProperty(keyword)) { + // detect named color + return new(tree.Color)(tree.colors[keyword].slice(1)); + } + if (keyword === transparentKeyword) { + var transparent = new(tree.Color)([0, 0, 0], 0); + transparent.isTransparentKeyword = true; + return transparent; + } +}; + })(require('../tree')); diff --git a/test/css/functions.css b/test/css/functions.css index c1781b6c..b4ea3c86 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -102,11 +102,13 @@ mix-0: #ffff00; mix-100: #ff0000; mix-weightless: #ff8000; + mixt: rgba(255, 0, 0, 0.5); } #built-in .is-a { color: true; color1: true; color2: true; + color3: true; keyword: true; number: true; string: true; @@ -117,6 +119,8 @@ } #alpha { alpha: rgba(153, 94, 51, 0.6); + alpha2: 0.5; + alpha3: 0; } #blendmodes { multiply: #ed0000; diff --git a/test/less/functions.less b/test/less/functions.less index 32fe44b0..aa4c040b 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -111,11 +111,13 @@ mix-0: mix(#ff0000, #ffff00, 0); mix-100: mix(#ff0000, #ffff00, 100); mix-weightless: mix(#ff0000, #ffff00); + mixt: mix(#ff0000, transparent); .is-a { color: iscolor(#ddd); color1: iscolor(red); color2: iscolor(rgb(0, 0, 0)); + color3: iscolor(transparent); keyword: iskeyword(hello); number: isnumber(32); string: isstring("hello"); @@ -128,6 +130,8 @@ #alpha { alpha: darken(hsla(25, 50%, 50%, 0.6), 10%); + alpha2: alpha(rgba(3, 4, 5, 0.5)); + alpha3: alpha(transparent); } #blendmodes {