Skip to content

Commit 2cee7ab

Browse files
committed
added built in functions: alpha, fade, mix, percentage
1 parent 5120fcd commit 2cee7ab

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

lessc.inc.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,64 @@ function lib_lightness($color) {
13101310
return round($hsl[3]);
13111311
}
13121312

1313+
// get the alpha of a color
1314+
// defaults to 1 for non-colors or colors without an alpha
1315+
function lib_alpha($color) {
1316+
if ($color[0] != 'color') return 1;
1317+
return isset($color[4]) ? $color[4] : 1;
1318+
}
1319+
1320+
// set the alpha of the color
1321+
function lib_fade($args) {
1322+
list($color, $alpha) = $this->colorArgs($args);
1323+
$color[4] = $this->clamp($alpha / 100.0);
1324+
return $color;
1325+
}
1326+
1327+
function lib_percentage($number) {
1328+
return array('%', $number[1]*100);
1329+
}
1330+
1331+
// mixes two colors by weight
1332+
// mix(@color1, @color2, @weight);
1333+
// http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#mix-instance_method
1334+
function lib_mix($args) {
1335+
if ($args[0] != "list")
1336+
throw new exception("mix expects (color1, color2, weight)");
1337+
1338+
list($first, $second, $weight) = $args[2];
1339+
$first = $this->assertColor($first);
1340+
$second = $this->assertColor($second);
1341+
1342+
$first_a = $this->lib_alpha($first);
1343+
$second_a = $this->lib_alpha($second);
1344+
$weight = $weight[1] / 100.0;
1345+
1346+
$w = $weight * 2 - 1;
1347+
$a = $first_a - $second_a;
1348+
1349+
$w1 = (($w * $a == -1 ? $w : ($w + $a)/(1 + $w * $a)) + 1) / 2.0;
1350+
$w2 = 1.0 - $w1;
1351+
1352+
$new = array('color',
1353+
$w1 * $first[1] + $w2 * $second[1],
1354+
$w1 * $first[2] + $w2 * $second[2],
1355+
$w1 * $first[3] + $w2 * $second[3],
1356+
);
1357+
1358+
if ($first_a != 1.0 || $second_a != 1.0) {
1359+
$new[] = $first_a * $p + $second_a * ($p - 1);
1360+
}
1361+
1362+
return $new;
1363+
}
1364+
1365+
function assertColor($value, $error = "expected color value") {
1366+
if ($value[0] != "color")
1367+
throw new exception($error);
1368+
return $value;
1369+
}
1370+
13131371
function toHSL($color) {
13141372
if ($color[0] == 'hsl') return $color;
13151373

tests/inputs/colors.less

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,29 @@ body {
6363
zero: spin(#000000, 100);
6464
zero: spin(#ffffff, 100);
6565
}
66+
67+
68+
alpha {
69+
// g: alpha(red);
70+
g: alpha(rgba(0,0,0,0));
71+
g: alpha(rgb(155,55,0));
72+
}
73+
74+
fade {
75+
f: fade(red, 50%);
76+
f: fade(#fff, 20%);
77+
f: fade(rgba(34,23,64,0.4), 50%);
78+
}
79+
80+
@a: rgb(255,255,255);
81+
@b: rgb(0,0,0);
82+
83+
.mix {
84+
color: mix(@a, @b, 50%);
85+
}
86+
87+
.percent {
88+
per: percentage(0.5);
89+
}
90+
91+

tests/outputs/colors.css

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,15 @@ body {
4343
zero:#56124b;
4444
zero:#000000;
4545
zero:#ffffff;
46-
}
46+
}
47+
alpha {
48+
g:0;
49+
g:1;
50+
}
51+
fade {
52+
f:rgba(0,0,0,0.5);
53+
f:rgba(255,255,255,0.2);
54+
f:rgba(34,23,64,0.5);
55+
}
56+
.mix { color:#7f7f7f; }
57+
.percent { per:50%; }

0 commit comments

Comments
 (0)