Skip to content

[Console] Add support of RGB functional notation #43094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add `TesterTrait::assertCommandIsSuccessful()` to test command
* Deprecate `HelperSet::setCommand()` and `getCommand()` without replacement
* Add `rgb(r, g, b)` notation support for output colors

5.3
---
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Console/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ final class Color
'conceal' => ['set' => 8, 'unset' => 28],
];

private const RGB_FUNCTIONAL_NOTATION_REGEX = '/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/';

private $foreground;
private $background;
private $options = [];
Expand Down Expand Up @@ -116,6 +118,10 @@ private function parseColor(string $color, bool $background = false): string
return '';
}

if (str_starts_with($color, 'rgb(')) {
$color = $this->rgbToHex($color);
}

if ('#' === $color[0]) {
$color = substr($color, 1);

Expand Down Expand Up @@ -177,4 +183,23 @@ private function getSaturation(int $r, int $g, int $b): int

return (int) $diff * 100 / $v;
}

private function rgbToHex(string $color): string
{
if (!preg_match(self::RGB_FUNCTIONAL_NOTATION_REGEX, $color, $matches)) {
throw new InvalidArgumentException(sprintf('Invalid RGB functional notation; should be of the form "rgb(r, g, b)", got "%s".', $color));
}

$rgb = \array_slice($matches, 1);

$hexString = array_map(function ($element) {
if ($element > 255) {
throw new InvalidArgumentException(sprintf('Invalid color component; value should be between 0 and 255, got %d.', $element));
}

return str_pad(dechex((int) $element), 2, '0', \STR_PAD_LEFT);
}, $rgb);

return '#'.implode('', $hexString);
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/Console/Tests/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Color;
use Symfony\Component\Console\Exception\InvalidArgumentException;

class ColorTest extends TestCase
{
Expand Down Expand Up @@ -42,6 +43,9 @@ public function testTrueColors()

$color = new Color('#ffffff', '#000000');
$this->assertSame("\033[38;2;255;255;255;48;2;0;0;0m \033[39;49m", $color->apply(' '));

$color = new Color('rgb(255, 255, 255)', 'rgb(0, 0, 0)');
$this->assertSame("\033[38;2;255;255;255;48;2;0;0;0m \033[39;49m", $color->apply(' '));
}

public function testDegradedTrueColors()
Expand All @@ -59,4 +63,32 @@ public function testDegradedTrueColors()
putenv('COLORTERM='.$colorterm);
}
}

/**
* @dataProvider provideMalformedRgbStrings
*/
public function testMalformedRgbString(string $color, string $exceptionMessage)
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($exceptionMessage);

new Color($color);
}

public function provideMalformedRgbStrings(): \Generator
{
yield ['rgb()', 'Invalid RGB functional notation; should be of the form "rgb(r, g, b)", got "rgb()".'];

yield ['rgb(0, 0)', 'Invalid RGB functional notation; should be of the form "rgb(r, g, b)", got "rgb(0, 0)".'];

yield ['rgb(0, 0, 0, 0)', 'Invalid RGB functional notation; should be of the form "rgb(r, g, b)", got "rgb(0, 0, 0, 0)".'];

yield ['rgb(-1, 0, 0)', 'Invalid RGB functional notation; should be of the form "rgb(r, g, b)", got "rgb(-1, 0, 0)".'];

yield ['rgb(invalid, 0, 0)', 'Invalid RGB functional notation; should be of the form "rgb(r, g, b)", got "rgb(invalid, 0, 0)".'];

yield ['rgb(256, 0, 0)', 'Invalid color component; value should be between 0 and 255, got 256.'];

yield ['rgb(0, 0, 0', 'Invalid RGB functional notation; should be of the form "rgb(r, g, b)", got "rgb(0, 0, 0".'];
}
}