Skip to content

Commit dd0f723

Browse files
committed
ColorRepresentation: Support new color syntaxes
1 parent 3d08771 commit dd0f723

4 files changed

Lines changed: 100 additions & 8 deletions

File tree

build/kint.phar

594 Bytes
Binary file not shown.

src/Parser/ColorPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa
5757

5858
$trimmed = \strtolower(\trim($var));
5959

60-
if (!isset(ColorRepresentation::$color_map[$trimmed]) && !\preg_match('/^(?:(?:rgb|hsl)[^\\)]{6,}\\)|#[0-9a-fA-F]{3,8})$/', $trimmed)) {
60+
if (!isset(ColorRepresentation::$color_map[$trimmed]) && !\preg_match('/^(?:(?:rgb|hsl)a?[^\\)]{6,}\\)|#[0-9a-f]{3,8})$/', $trimmed)) {
6161
return $v;
6262
}
6363

src/Value/Representation/ColorRepresentation.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ protected function setValuesFromHex(string $hex): int
397397
/** @psalm-return self::COLOR_* */
398398
protected function setValuesFromFunction(string $value): int
399399
{
400+
// We're not even going to attempt to support other color functions or
401+
// angle values. If that's ever going to be a thing we'll depend on a
402+
// color library and use php scopes for the phar file.
400403
if (!\preg_match('/^((?:rgb|hsl)a?)\\s*\\(([0-9\\.%,\\s\\/\\-]+)\\)$/i', $value, $match)) {
401404
throw new InvalidArgumentException('Couldn\'t parse color function string');
402405
}
@@ -419,15 +422,30 @@ protected function setValuesFromFunction(string $value): int
419422
throw new InvalidArgumentException('Color functions must be one of rgb/rgba/hsl/hsla'); // @codeCoverageIgnore
420423
}
421424

422-
/** @psalm-var string $params */
423-
$params = \preg_replace('/[,\\s\\/]+/', ',', \trim($match[2]));
424-
$params = \explode(',', $params);
425-
$params = \array_map('trim', $params);
425+
\preg_match('/^\\s*([^,\\s\\/]+)([,\\s]+)((?1))((?2))((?1))(?:([,\\s\\/]+)((?1)))?\\s*$/', $match[2], $match);
426+
427+
if (!$match ||
428+
\trim($match[2]) !== \trim($match[4]) ||
429+
(isset($match[6]) && (
430+
('' === \trim($match[2]) && '/' !== \trim($match[6])) ||
431+
('' !== \trim($match[2]) && \trim($match[2]) !== \trim($match[6]))
432+
))
433+
) {
434+
throw new InvalidArgumentException('Couldn\'t parse color function string');
435+
}
426436

427-
if (\count($params) < 3 || \count($params) > 4) {
428-
throw new InvalidArgumentException('Color functions must have 3 or 4 arguments');
437+
$params = [
438+
$match[1],
439+
$match[3],
440+
$match[5],
441+
];
442+
443+
if (isset($match[7])) {
444+
$params[] = $match[7];
429445
}
430446

447+
$params = \array_map('trim', $params);
448+
431449
foreach ($params as $i => &$color) {
432450
if (false !== \strpos($color, '%')) {
433451
$color = (float) \str_replace('%', '', $color);

tests/Value/Representation/ColorRepresentationTest.php

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ public function colorProvider()
126126
0.5,
127127
ColorRepresentation::COLOR_RGB,
128128
],
129+
'rgb alpha truncated' => [
130+
'rgb(123, 45, 67, .5)',
131+
123,
132+
45,
133+
67,
134+
0.5,
135+
ColorRepresentation::COLOR_RGB,
136+
],
129137
'rgb alpha percent' => [
130138
'rgb(50%, 60%, 70%, 50%)',
131139
128,
@@ -135,7 +143,7 @@ public function colorProvider()
135143
ColorRepresentation::COLOR_RGB,
136144
],
137145
'rgb alpha whitespace' => [
138-
' rgb ( 123 45 67 / 0.5 ) ',
146+
' rgb ( 123 , 45 , 67 , 0.5 ) ',
139147
123,
140148
45,
141149
67,
@@ -166,6 +174,70 @@ public function colorProvider()
166174
1.0,
167175
ColorRepresentation::COLOR_RGBA,
168176
],
177+
'new rbg' => [
178+
'rgb(123 45 67)',
179+
123,
180+
45,
181+
67,
182+
1.0,
183+
ColorRepresentation::COLOR_RGB,
184+
],
185+
'new rgb alpha' => [
186+
'rgb(123 45 67 / 0.5)',
187+
123,
188+
45,
189+
67,
190+
0.5,
191+
ColorRepresentation::COLOR_RGB,
192+
],
193+
'new rgb alpha truncated' => [
194+
'rgb(123 45 67 / .5)',
195+
123,
196+
45,
197+
67,
198+
0.5,
199+
ColorRepresentation::COLOR_RGB,
200+
],
201+
'new rgb alpha percent' => [
202+
'rgb(50% 60% 70% / 50%)',
203+
128,
204+
153,
205+
179,
206+
0.5,
207+
ColorRepresentation::COLOR_RGB,
208+
],
209+
'new rgb alpha whitespace' => [
210+
' rgb ( 123 45 67 / 0.5 ) ',
211+
123,
212+
45,
213+
67,
214+
0.5,
215+
ColorRepresentation::COLOR_RGB,
216+
],
217+
'new rgb uppercase' => [
218+
'RGB(1 2 3)',
219+
1,
220+
2,
221+
3,
222+
1.0,
223+
ColorRepresentation::COLOR_RGB,
224+
],
225+
'new rgba' => [
226+
'rgba(123 45 67 / 0.5)',
227+
123,
228+
45,
229+
67,
230+
0.5,
231+
ColorRepresentation::COLOR_RGBA,
232+
],
233+
'new rgba without alpha' => [
234+
'rgba(123 45 67)',
235+
123,
236+
45,
237+
67,
238+
1.0,
239+
ColorRepresentation::COLOR_RGBA,
240+
],
169241
'hsl' => [
170242
'hsl(120, 100%, 50%)',
171243
0,
@@ -270,6 +342,8 @@ public function badColorProvider()
270342
'invalid hsl range' => ['hsl(0, 120%, 120%)'],
271343
'invalid hsl alpha range' => ['hsl(0, 0%, 0%, 2)'],
272344
'invalid name' => ['thatcolorbehindyoureyelids'],
345+
'invalid new without /' => ['rgba(1 2 3 4%)'],
346+
'invalid new mix' => ['rgb(1, 2 3)'],
273347
];
274348
}
275349

0 commit comments

Comments
 (0)