Skip to content

Commit 5aa9abe

Browse files
committed
color keywords can be used in expresions
1 parent 390ecab commit 5aa9abe

File tree

2 files changed

+183
-4
lines changed

2 files changed

+183
-4
lines changed

lessc.inc.php

Lines changed: 176 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,8 @@ function colorArgs($args) {
12321232
return array(array('color', 0, 0, 0));
12331233
}
12341234
list($color, $delta) = $args[2];
1235-
if ($color[0] != 'color')
1235+
$color = $this->coerceColor($color);
1236+
if (is_null($color))
12361237
$color = array('color', 0, 0, 0);
12371238

12381239
$delta = floatval($delta[1]);
@@ -1363,9 +1364,9 @@ function lib_mix($args) {
13631364
}
13641365

13651366
function assertColor($value, $error = "expected color value") {
1366-
if ($value[0] != "color")
1367-
throw new exception($error);
1368-
return $value;
1367+
$color = $this->coerceColor($value);
1368+
if (is_null($color)) throw new exception($error);
1369+
return $color;
13691370
}
13701371

13711372
function toHSL($color) {
@@ -1548,11 +1549,32 @@ function reduce($var, $defaultValue = array('number', 0)) {
15481549
return $var;
15491550
}
15501551

1552+
function coerceColor($value) {
1553+
switch($value[0]) {
1554+
case 'color': return $value;
1555+
case 'keyword':
1556+
$name = $value[1];
1557+
if (isset(self::$cssColors[$name])) {
1558+
list($r, $g, $b) = explode(',', self::$cssColors[$name]);
1559+
return array('color', $r, $g, $b);
1560+
}
1561+
return null;
1562+
}
1563+
}
1564+
15511565
// evaluate an expression
15521566
function evaluate($op, $left, $right) {
15531567
$left = $this->reduce($left);
15541568
$right = $this->reduce($right);
15551569

1570+
if ($left_color = $this->coerceColor($left)) {
1571+
$left = $left_color;
1572+
}
1573+
1574+
if ($right_color = $this->coerceColor($right)) {
1575+
$right = $right_color;
1576+
}
1577+
15561578
if ($left[0] == 'color' && $right[0] == 'color') {
15571579
$out = $this->op_color_color($op, $left, $right);
15581580
return $out;
@@ -2063,5 +2085,155 @@ public static function cexecute($in, $force = false) {
20632085
}
20642086

20652087
}
2088+
2089+
static protected $cssColors = array(
2090+
'aliceblue' => '240,248,255',
2091+
'antiquewhite' => '250,235,215',
2092+
'aqua' => '0,255,255',
2093+
'aquamarine' => '127,255,212',
2094+
'azure' => '240,255,255',
2095+
'beige' => '245,245,220',
2096+
'bisque' => '255,228,196',
2097+
'black' => '0,0,0',
2098+
'blanchedalmond' => '255,235,205',
2099+
'blue' => '0,0,255',
2100+
'blueviolet' => '138,43,226',
2101+
'brown' => '165,42,42',
2102+
'burlywood' => '222,184,135',
2103+
'cadetblue' => '95,158,160',
2104+
'chartreuse' => '127,255,0',
2105+
'chocolate' => '210,105,30',
2106+
'coral' => '255,127,80',
2107+
'cornflowerblue' => '100,149,237',
2108+
'cornsilk' => '255,248,220',
2109+
'crimson' => '220,20,60',
2110+
'cyan' => '0,255,255',
2111+
'darkblue' => '0,0,139',
2112+
'darkcyan' => '0,139,139',
2113+
'darkgoldenrod' => '184,134,11',
2114+
'darkgray' => '169,169,169',
2115+
'darkgreen' => '0,100,0',
2116+
'darkgrey' => '169,169,169',
2117+
'darkkhaki' => '189,183,107',
2118+
'darkmagenta' => '139,0,139',
2119+
'darkolivegreen' => '85,107,47',
2120+
'darkorange' => '255,140,0',
2121+
'darkorchid' => '153,50,204',
2122+
'darkred' => '139,0,0',
2123+
'darksalmon' => '233,150,122',
2124+
'darkseagreen' => '143,188,143',
2125+
'darkslateblue' => '72,61,139',
2126+
'darkslategray' => '47,79,79',
2127+
'darkslategrey' => '47,79,79',
2128+
'darkturquoise' => '0,206,209',
2129+
'darkviolet' => '148,0,211',
2130+
'deeppink' => '255,20,147',
2131+
'deepskyblue' => '0,191,255',
2132+
'dimgray' => '105,105,105',
2133+
'dimgrey' => '105,105,105',
2134+
'dodgerblue' => '30,144,255',
2135+
'firebrick' => '178,34,34',
2136+
'floralwhite' => '255,250,240',
2137+
'forestgreen' => '34,139,34',
2138+
'fuchsia' => '255,0,255',
2139+
'gainsboro' => '220,220,220',
2140+
'ghostwhite' => '248,248,255',
2141+
'gold' => '255,215,0',
2142+
'goldenrod' => '218,165,32',
2143+
'gray' => '128,128,128',
2144+
'green' => '0,128,0',
2145+
'greenyellow' => '173,255,47',
2146+
'grey' => '128,128,128',
2147+
'honeydew' => '240,255,240',
2148+
'hotpink' => '255,105,180',
2149+
'indianred' => '205,92,92',
2150+
'indigo' => '75,0,130',
2151+
'ivory' => '255,255,240',
2152+
'khaki' => '240,230,140',
2153+
'lavender' => '230,230,250',
2154+
'lavenderblush' => '255,240,245',
2155+
'lawngreen' => '124,252,0',
2156+
'lemonchiffon' => '255,250,205',
2157+
'lightblue' => '173,216,230',
2158+
'lightcoral' => '240,128,128',
2159+
'lightcyan' => '224,255,255',
2160+
'lightgoldenrodyellow' => '250,250,210',
2161+
'lightgray' => '211,211,211',
2162+
'lightgreen' => '144,238,144',
2163+
'lightgrey' => '211,211,211',
2164+
'lightpink' => '255,182,193',
2165+
'lightsalmon' => '255,160,122',
2166+
'lightseagreen' => '32,178,170',
2167+
'lightskyblue' => '135,206,250',
2168+
'lightslategray' => '119,136,153',
2169+
'lightslategrey' => '119,136,153',
2170+
'lightsteelblue' => '176,196,222',
2171+
'lightyellow' => '255,255,224',
2172+
'lime' => '0,255,0',
2173+
'limegreen' => '50,205,50',
2174+
'linen' => '250,240,230',
2175+
'magenta' => '255,0,255',
2176+
'maroon' => '128,0,0',
2177+
'mediumaquamarine' => '102,205,170',
2178+
'mediumblue' => '0,0,205',
2179+
'mediumorchid' => '186,85,211',
2180+
'mediumpurple' => '147,112,219',
2181+
'mediumseagreen' => '60,179,113',
2182+
'mediumslateblue' => '123,104,238',
2183+
'mediumspringgreen' => '0,250,154',
2184+
'mediumturquoise' => '72,209,204',
2185+
'mediumvioletred' => '199,21,133',
2186+
'midnightblue' => '25,25,112',
2187+
'mintcream' => '245,255,250',
2188+
'mistyrose' => '255,228,225',
2189+
'moccasin' => '255,228,181',
2190+
'navajowhite' => '255,222,173',
2191+
'navy' => '0,0,128',
2192+
'oldlace' => '253,245,230',
2193+
'olive' => '128,128,0',
2194+
'olivedrab' => '107,142,35',
2195+
'orange' => '255,165,0',
2196+
'orangered' => '255,69,0',
2197+
'orchid' => '218,112,214',
2198+
'palegoldenrod' => '238,232,170',
2199+
'palegreen' => '152,251,152',
2200+
'paleturquoise' => '175,238,238',
2201+
'palevioletred' => '219,112,147',
2202+
'papayawhip' => '255,239,213',
2203+
'peachpuff' => '255,218,185',
2204+
'peru' => '205,133,63',
2205+
'pink' => '255,192,203',
2206+
'plum' => '221,160,221',
2207+
'powderblue' => '176,224,230',
2208+
'purple' => '128,0,128',
2209+
'red' => '255,0,0',
2210+
'rosybrown' => '188,143,143',
2211+
'royalblue' => '65,105,225',
2212+
'saddlebrown' => '139,69,19',
2213+
'salmon' => '250,128,114',
2214+
'sandybrown' => '244,164,96',
2215+
'seagreen' => '46,139,87',
2216+
'seashell' => '255,245,238',
2217+
'sienna' => '160,82,45',
2218+
'silver' => '192,192,192',
2219+
'skyblue' => '135,206,235',
2220+
'slateblue' => '106,90,205',
2221+
'slategray' => '112,128,144',
2222+
'slategrey' => '112,128,144',
2223+
'snow' => '255,250,250',
2224+
'springgreen' => '0,255,127',
2225+
'steelblue' => '70,130,180',
2226+
'tan' => '210,180,140',
2227+
'teal' => '0,128,128',
2228+
'thistle' => '216,191,216',
2229+
'tomato' => '255,99,71',
2230+
'turquoise' => '64,224,208',
2231+
'violet' => '238,130,238',
2232+
'wheat' => '245,222,179',
2233+
'white' => '255,255,255',
2234+
'whitesmoke' => '245,245,245',
2235+
'yellow' => '255,255,0',
2236+
'yellowgreen' => '154,205,50'
2237+
);
20662238
}
20672239

tests/inputs/colors.less

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,11 @@ fade {
8888
per: percentage(0.5);
8989
}
9090

91+
// color keywords
92+
93+
.colorz {
94+
color: whitesmoke - 10;
95+
color: spin(red, 34);
96+
}
97+
9198

0 commit comments

Comments
 (0)