Skip to content

Commit 00aa3e1

Browse files
anny21Krinkle
authored andcommitted
Add support for native CSS color functions including with CSS variables
Preserve unsupported color values passed to Less functions, by outputting them as CSS functions instead. Bug: T405815 Change-Id: I0a088edc63b0a18406557b5c5c6212f361ea32dd
1 parent a359936 commit 00aa3e1

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
lines changed

lib/Less/Functions.php

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private static function _number( $n ) {
4242
}
4343
}
4444

45-
private static function _scaled( $n, $size = 255 ) {
45+
private static function _scaled( $n, $size ) {
4646
if ( $n instanceof Less_Tree_Dimension && $n->unit->is( '%' ) ) {
4747
return (float)$n->value * $size / 100;
4848
} else {
@@ -51,9 +51,6 @@ private static function _scaled( $n, $size = 255 ) {
5151
}
5252

5353
public function rgb( $r = null, $g = null, $b = null ) {
54-
if ( $r === null || $g === null || $b === null ) {
55-
throw new Less_Exception_Compiler( "rgb expects three parameters" );
56-
}
5754
$color = $this->rgba( $r, $g, $b, 1.0 );
5855
if ( $color ) {
5956
$color->value = 'rgb';
@@ -62,14 +59,26 @@ public function rgb( $r = null, $g = null, $b = null ) {
6259
}
6360

6461
public function rgba( $r = null, $g = null, $b = null, $a = null ) {
65-
$rgb = [
66-
self::_scaled( $r ),
67-
self::_scaled( $g ),
68-
self::_scaled( $b )
69-
];
62+
try {
63+
if ( $r instanceof Less_Tree_Color ) {
64+
if ( $g ) {
65+
$a = self::_number( $g );
66+
} else {
67+
$a = $r->alpha;
68+
}
69+
return new Less_Tree_Color( $r->rgb, $a, 'rgba' );
70+
}
71+
$rgb = [
72+
self::_scaled( $r, 255 ),
73+
self::_scaled( $g, 255 ),
74+
self::_scaled( $b, 255 )
75+
];
7076

71-
$a = self::_number( $a );
72-
return new Less_Tree_Color( $rgb, $a, 'rgba' );
77+
$a = self::_number( $a );
78+
return new Less_Tree_Color( $rgb, $a, 'rgba' );
79+
} catch ( Exception $e ) {
80+
81+
}
7382
}
7483

7584
public function hsl( $h, $s, $l ) {
@@ -80,23 +89,36 @@ public function hsl( $h, $s, $l ) {
8089
}
8190
}
8291

83-
public function hsla( $h, $s, $l, $a ) {
84-
$h = fmod( self::_number( $h ), 360 ) / 360; // Classic % operator will change float to int
85-
$s = self::_clamp( self::_number( $s ) );
86-
$l = self::_clamp( self::_number( $l ) );
87-
$a = self::_clamp( self::_number( $a ) );
92+
public function hsla( $h = null, $s = null, $l = null, $a = null ) {
93+
try {
94+
if ( $h instanceof Less_Tree_Color ) {
95+
if ( $s ) {
96+
$a = self::_number( $s );
97+
} else {
98+
$a = $h->alpha;
99+
}
100+
return new Less_Tree_Color( $h->rgb, $a, 'hsla' );
101+
}
102+
// NOTE: Avoid % operator which would change float to int
103+
$h = fmod( self::_number( $h ), 360 ) / 360;
104+
$s = self::_clamp( self::_number( $s ) );
105+
$l = self::_clamp( self::_number( $l ) );
106+
$a = self::_clamp( self::_number( $a ) );
88107

89-
$m2 = $l <= 0.5 ? $l * ( $s + 1 ) : $l + $s - $l * $s;
108+
$m2 = $l <= 0.5 ? $l * ( $s + 1 ) : $l + $s - $l * $s;
90109

91-
$m1 = $l * 2 - $m2;
110+
$m1 = $l * 2 - $m2;
92111

93-
$rgb = [
94-
self::hsla_hue( $h + 1 / 3, $m1, $m2 ) * 255,
95-
self::hsla_hue( $h, $m1, $m2 ) * 255,
96-
self::hsla_hue( $h - 1 / 3, $m1, $m2 ) * 255,
97-
];
98-
$a = self::_number( $a );
99-
return new Less_Tree_Color( $rgb, $a, 'hsla' );
112+
$rgb = [
113+
self::hsla_hue( $h + 1 / 3, $m1, $m2 ) * 255,
114+
self::hsla_hue( $h, $m1, $m2 ) * 255,
115+
self::hsla_hue( $h - 1 / 3, $m1, $m2 ) * 255,
116+
];
117+
$a = self::_number( $a );
118+
return new Less_Tree_Color( $rgb, $a, 'hsla' );
119+
} catch ( Exception $e ) {
120+
121+
}
100122
}
101123

102124
/**

test/phpunit/FixturesTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class FixturesTest extends LessTestCase {
1919
'import-module' => true,
2020
'extend-selector' => true,
2121
'merge' => true,
22-
'colors' => true,
2322
'urls' => true,
2423
'operations' => true,
2524
'import-remote' => true,

test/phpunit/FunctionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testException() {
3636
} catch ( Exception $e ) {
3737
$this->assertInstanceOf( Less_Exception_Parser::class, $e );
3838
$this->assertStringContainsString(
39-
'error evaluating function',
39+
'error evaluating function `max`',
4040
$e->getMessage()
4141
);
4242

@@ -45,7 +45,7 @@ public function testException() {
4545
$prev = $e->getPrevious();
4646
$this->assertInstanceOf( Less_Exception_Parser::class, $e );
4747
$this->assertStringContainsString(
48-
'rgb expects three parameters',
48+
'one or more arguments required in f1.less on line 2',
4949
$e->getMessage()
5050
);
5151
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.foo {
2-
content: rgb("x");
2+
content: max();
33
}

0 commit comments

Comments
 (0)