Skip to content

Commit 7f78675

Browse files
authored
examples : use colorblind friendly TTY color scheme (#2360)
This change updates the -pc flag, so that a new xterm256 color scheme is used. This color scheme is believed to be better for three reasons: 1. It should be friendlier to the colorblind. The scheme was designed by Paul Tol (see: https://personal.sron.nl/~pault/). TensorBoard uses it since 2017, so it's already popular in the machine learning community 2. It should appear to be the same colors as before to people who aren't i.e. it's still a red-green spectrum like before but lightly modified 3. It is readable in both white and black background terminals. The neon colors before were probably a bit too intense for white backgrounds.
1 parent 22fcd5f commit 7f78675

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

examples/common.h

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <thread>
1010
#include <ctime>
1111
#include <fstream>
12+
#include <sstream>
1213

1314
#define COMMON_SAMPLE_RATE 16000
1415

@@ -286,12 +287,43 @@ void sam_print_usage(int argc, char ** argv, const sam_params & params);
286287
// Terminal utils
287288
//
288289

289-
290-
// Terminal color map. 10 colors grouped in ranges [0.0, 0.1, ..., 0.9]
291-
// Lowest is red, middle is yellow, highest is green.
290+
#define SQR(X) ((X) * (X))
291+
#define UNCUBE(x) x < 48 ? 0 : x < 115 ? 1 : (x - 35) / 40
292+
293+
/**
294+
* Quantizes 24-bit RGB to xterm256 code range [16,256).
295+
*/
296+
static int rgb2xterm256(int r, int g, int b) {
297+
unsigned char cube[] = {0, 0137, 0207, 0257, 0327, 0377};
298+
int av, ir, ig, ib, il, qr, qg, qb, ql;
299+
av = r * .299 + g * .587 + b * .114 + .5;
300+
ql = (il = av > 238 ? 23 : (av - 3) / 10) * 10 + 8;
301+
qr = cube[(ir = UNCUBE(r))];
302+
qg = cube[(ig = UNCUBE(g))];
303+
qb = cube[(ib = UNCUBE(b))];
304+
if (SQR(qr - r) + SQR(qg - g) + SQR(qb - b) <=
305+
SQR(ql - r) + SQR(ql - g) + SQR(ql - b))
306+
return ir * 36 + ig * 6 + ib + 020;
307+
return il + 0350;
308+
}
309+
310+
static std::string set_xterm256_foreground(int r, int g, int b) {
311+
int x = rgb2xterm256(r, g, b);
312+
std::ostringstream oss;
313+
oss << "\033[38;5;" << x << "m";
314+
return oss.str();
315+
}
316+
317+
// Lowest is red, middle is yellow, highest is green. Color scheme from
318+
// Paul Tol; it is colorblind friendly https://personal.sron.nl/~pault/
292319
const std::vector<std::string> k_colors = {
293-
"\033[38;5;196m", "\033[38;5;202m", "\033[38;5;208m", "\033[38;5;214m", "\033[38;5;220m",
294-
"\033[38;5;226m", "\033[38;5;190m", "\033[38;5;154m", "\033[38;5;118m", "\033[38;5;82m",
320+
set_xterm256_foreground(220, 5, 12),
321+
set_xterm256_foreground(232, 96, 28),
322+
set_xterm256_foreground(241, 147, 45),
323+
set_xterm256_foreground(246, 193, 65),
324+
set_xterm256_foreground(247, 240, 86),
325+
set_xterm256_foreground(144, 201, 135),
326+
set_xterm256_foreground( 78, 178, 101),
295327
};
296328

297329
//

0 commit comments

Comments
 (0)