@@ -62,25 +62,28 @@ static int enableConsoleColor(void)
6262#endif /* COLORS_ON_WINDOWS */
6363}
6464
65- const unsigned kColors [] = {
66- 0xffffff , 0xff0000 , 0x00ff00 , 0x7f0000 ,
67- 0x007f00 , 0x00ffff , 0xff00ff , 0xffff00 ,
68- 0x007fff , 0x3fff3f , 0x007f3f , 0x7f7f00 ,
65+ const const char * kColors [] = {
66+ #define FORMAT_COLOR (r , g , b ) "\033[38;2;" #r ";"#g";"#b"m"
67+ FORMAT_COLOR (255 , 255 , 255 ),
68+ FORMAT_COLOR (255 , 0 , 0 ),
69+ FORMAT_COLOR (0 , 255 , 0 ),
70+ FORMAT_COLOR (127 , 0 , 0 ),
71+ FORMAT_COLOR (0 , 127 , 0 ),
72+ FORMAT_COLOR (0 , 255 , 255 ),
73+ FORMAT_COLOR (255 , 0 , 255 ),
74+ FORMAT_COLOR (255 , 255 , 0 ),
75+ FORMAT_COLOR (0 , 127 , 255 ),
76+ FORMAT_COLOR (63 , 255 , 63 ),
77+ FORMAT_COLOR (0 , 127 , 63 ),
78+ FORMAT_COLOR (127 , 127 , 0 ),
79+ #undef FORMAT_COLOR
6980};
7081
7182const int kColorCount = sizeof (kColors ) / sizeof (kColors [0 ]);
7283
73- static void setColor (unsigned rgb )
74- {
75- const int r = (rgb >> 16 ) & 0xff ;
76- const int g = (rgb >> 8 ) & 0xff ;
77- const int b = (rgb >> 0 ) & 0xff ;
78- printf ("\033[38;2;%d;%d;%dm" , r , g , b );
79- }
80-
8184static void resetColor (void )
8285{
83- printf ("\033[0m" ); /* is this correct? */
86+ fputs ("\033[0m" , stdout ); /* is this correct? */
8487}
8588
8689/* 32-bit fnv1, not 1a */
@@ -100,7 +103,7 @@ static unsigned fnv(const char * str)
100103static void printColoredByHash (const char * str )
101104{
102105 const int idx = fnv (str ) % kColorCount ;
103- setColor (kColors [idx ]);
106+ fputs (kColors [idx ], stdout );
104107 fputs (str , stdout ); /* not puts to not get a newline */
105108 resetColor ();
106109}
@@ -185,17 +188,14 @@ static int printhelp(const char * argv0)
185188
186189 /* print colors in their color, if possible, else in default color */
187190 ok = enableConsoleColor ();
188- printf ("Available colors (%s):\n" , ok ? "in that color each" : "values only" );
191+ printf ("Available color format strings (%s):\n" , ok ? "in that color each" : "values only" );
189192 for (i = 0 ; i < kColorCount ; ++ i )
190193 {
191- const int c = kColors [i ];
194+ const char * c = kColors [i ];
192195 if (ok )
193- setColor ( c );
196+ fputs ( c , stdout );
194197
195- printf ("#%06x rgb(%d, %d, %d)\n" ,
196- kColors [i ],
197- (c >> 16 ) & 0xff , (c >> 8 ) & 0xff , (c >> 0 ) & 0xff
198- );
198+ puts (c + 1 ); /* skip the ESC char to not interpret this as control sequence */
199199 } /* for each color */
200200
201201 resetColor ();
0 commit comments