Skip to content

Commit 14bb52f

Browse files
committed
Colorizer now uses 8-bit color palette
1 parent eb3317c commit 14bb52f

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

src/Internal/Console/Colorizer.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
final class Colorizer
1111
{
1212
/**
13-
* Color name => [foreground code, background code]
13+
* @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
1414
*/
1515
private const COLORMAP = [
16-
'black' => ['30', '40'],
17-
'red' => ['31', '41'],
18-
'green' => ['32', '42'],
19-
'yellow' => ['33', '43'],
20-
'blue' => ['34', '44'],
21-
'magenta' => ['35', '45'],
22-
'cyan' => ['36', '46'],
23-
'white' => ['37', '47'],
24-
'gray' => ['90', '100'],
16+
'black' => 0,
17+
'red' => 1,
18+
'green' => 2,
19+
'yellow' => 3,
20+
'blue' => 4,
21+
'magenta' => 5,
22+
'cyan' => 6,
23+
'white' => 7,
24+
'gray' => 8,
2525
];
2626

2727
private function __construct()
@@ -65,19 +65,9 @@ public static function colorize(string $string): string
6565
$pos = \strpos($string, $match[0]);
6666
$len = \strlen($match[0]);
6767
$text = \strip_tags($match[0]);
68-
$fg = $attr['fg'] ?? null;
69-
$bg = $attr['bg'] ?? null;
70-
$colors = [];
71-
if (isset(self::COLORMAP[$fg][0])) {
72-
$colors[] = self::COLORMAP[$fg][0];
73-
}
74-
if (isset(self::COLORMAP[$bg][1])) {
75-
$colors[] = self::COLORMAP[$bg][1];
76-
}
77-
if ($colors === []) {
78-
continue;
79-
}
80-
$formattedString = \sprintf("\e[%sm%s\e[0m", \implode(';', $colors), $text);
68+
$color = self::COLORMAP[$attr['fg'] ?? $attr['bg']] ?? $attr['fg'] ?? $attr['bg'];
69+
$isFg = isset($attr['fg']);
70+
$formattedString = \sprintf("\e[%s:5:%sm%s\e[0m", $isFg ? '38' : '48', $color, $text);
8171
$string = \substr_replace($string, $formattedString, $pos, $len);
8272
}
8373

0 commit comments

Comments
 (0)