@@ -30,6 +30,17 @@ final class Color
30
30
'default ' => 9 ,
31
31
];
32
32
33
+ private const BRIGHT_COLORS = [
34
+ 'gray ' => 0 ,
35
+ 'bright-red ' => 1 ,
36
+ 'bright-green ' => 2 ,
37
+ 'bright-yellow ' => 3 ,
38
+ 'bright-blue ' => 4 ,
39
+ 'bright-magenta ' => 5 ,
40
+ 'bright-cyan ' => 6 ,
41
+ 'bright-white ' => 7 ,
42
+ ];
43
+
33
44
private const AVAILABLE_OPTIONS = [
34
45
'bold ' => ['set ' => 1 , 'unset ' => 22 ],
35
46
'underscore ' => ['set ' => 4 , 'unset ' => 24 ],
@@ -45,7 +56,7 @@ final class Color
45
56
public function __construct (string $ foreground = '' , string $ background = '' , array $ options = [])
46
57
{
47
58
$ this ->foreground = $ this ->parseColor ($ foreground );
48
- $ this ->background = $ this ->parseColor ($ background );
59
+ $ this ->background = $ this ->parseColor ($ background, true );
49
60
50
61
foreach ($ options as $ option ) {
51
62
if (!isset (self ::AVAILABLE_OPTIONS [$ option ])) {
@@ -65,10 +76,10 @@ public function set(): string
65
76
{
66
77
$ setCodes = [];
67
78
if ('' !== $ this ->foreground ) {
68
- $ setCodes [] = ' 3 ' . $ this ->foreground ;
79
+ $ setCodes [] = $ this ->foreground ;
69
80
}
70
81
if ('' !== $ this ->background ) {
71
- $ setCodes [] = ' 4 ' . $ this ->background ;
82
+ $ setCodes [] = $ this ->background ;
72
83
}
73
84
foreach ($ this ->options as $ option ) {
74
85
$ setCodes [] = $ option ['set ' ];
@@ -99,7 +110,7 @@ public function unset(): string
99
110
return sprintf ("\033[%sm " , implode ('; ' , $ unsetCodes ));
100
111
}
101
112
102
- private function parseColor (string $ color ): string
113
+ private function parseColor (string $ color, bool $ background = false ): string
103
114
{
104
115
if ('' === $ color ) {
105
116
return '' ;
@@ -116,14 +127,18 @@ private function parseColor(string $color): string
116
127
throw new InvalidArgumentException (sprintf ('Invalid "%s" color. ' , $ color ));
117
128
}
118
129
119
- return $ this ->convertHexColorToAnsi (hexdec ($ color ));
130
+ return ($ background ? '4 ' : '3 ' ).$ this ->convertHexColorToAnsi (hexdec ($ color ));
131
+ }
132
+
133
+ if (isset (self ::COLORS [$ color ])) {
134
+ return ($ background ? '4 ' : '3 ' ).self ::COLORS [$ color ];
120
135
}
121
136
122
- if (! isset (self ::COLORS [$ color ])) {
123
- throw new InvalidArgumentException ( sprintf ( ' Invalid "%s" color; expected one of (%s). ' , $ color , implode ( ' , ' , array_keys ( self ::COLORS )))) ;
137
+ if (isset (self ::BRIGHT_COLORS [$ color ])) {
138
+ return ( $ background ? ' 10 ' : ' 9 ' ). self ::BRIGHT_COLORS [ $ color ] ;
124
139
}
125
140
126
- return ( string ) self ::COLORS [ $ color ] ;
141
+ throw new InvalidArgumentException ( sprintf ( ' Invalid "%s" color; expected one of (%s). ' , $ color , implode ( ' , ' , array_merge ( array_keys ( self ::COLORS ), array_keys ( self :: BRIGHT_COLORS ))))) ;
127
142
}
128
143
129
144
private function convertHexColorToAnsi (int $ color ): string
0 commit comments