@@ -30,6 +30,17 @@ final class Color
3030 'default ' => 9 ,
3131 ];
3232
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+
3344 private const AVAILABLE_OPTIONS = [
3445 'bold ' => ['set ' => 1 , 'unset ' => 22 ],
3546 'underscore ' => ['set ' => 4 , 'unset ' => 24 ],
@@ -45,7 +56,7 @@ final class Color
4556 public function __construct (string $ foreground = '' , string $ background = '' , array $ options = [])
4657 {
4758 $ this ->foreground = $ this ->parseColor ($ foreground );
48- $ this ->background = $ this ->parseColor ($ background );
59+ $ this ->background = $ this ->parseColor ($ background, true );
4960
5061 foreach ($ options as $ option ) {
5162 if (!isset (self ::AVAILABLE_OPTIONS [$ option ])) {
@@ -65,10 +76,10 @@ public function set(): string
6576 {
6677 $ setCodes = [];
6778 if ('' !== $ this ->foreground ) {
68- $ setCodes [] = ' 3 ' . $ this ->foreground ;
79+ $ setCodes [] = $ this ->foreground ;
6980 }
7081 if ('' !== $ this ->background ) {
71- $ setCodes [] = ' 4 ' . $ this ->background ;
82+ $ setCodes [] = $ this ->background ;
7283 }
7384 foreach ($ this ->options as $ option ) {
7485 $ setCodes [] = $ option ['set ' ];
@@ -99,7 +110,7 @@ public function unset(): string
99110 return sprintf ("\033[%sm " , implode ('; ' , $ unsetCodes ));
100111 }
101112
102- private function parseColor (string $ color ): string
113+ private function parseColor (string $ color, bool $ background = false ): string
103114 {
104115 if ('' === $ color ) {
105116 return '' ;
@@ -116,14 +127,18 @@ private function parseColor(string $color): string
116127 throw new InvalidArgumentException (sprintf ('Invalid "%s" color. ' , $ color ));
117128 }
118129
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 ];
120135 }
121136
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 ] ;
124139 }
125140
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 ))))) ;
127142 }
128143
129144 private function convertHexColorToAnsi (int $ color ): string
0 commit comments