Skip to content

Commit 7450034

Browse files
committed
Use colorblind friendly TTY colors in whisperfile
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. This change was upstreamed in ggml-org/whisper.cpp#2360
1 parent b5748f3 commit 7450034

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

whisper.cpp/common.h

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,43 @@ void sam_print_usage(int argc, char ** argv, const sam_params & params);
167167
// Terminal utils
168168
//
169169

170-
171-
// Terminal color map. 10 colors grouped in ranges [0.0, 0.1, ..., 0.9]
172-
// Lowest is red, middle is yellow, highest is green.
170+
#define SQR(X) ((X) * (X))
171+
#define UNCUBE(x) x < 48 ? 0 : x < 115 ? 1 : (x - 35) / 40
172+
173+
/**
174+
* Quantizes 24-bit RGB to xterm256 code range [16,256).
175+
*/
176+
static int rgb2xterm256(int r, int g, int b) {
177+
unsigned char cube[] = {0, 0137, 0207, 0257, 0327, 0377};
178+
int av, ir, ig, ib, il, qr, qg, qb, ql;
179+
av = r * .299 + g * .587 + b * .114 + .5;
180+
ql = (il = av > 238 ? 23 : (av - 3) / 10) * 10 + 8;
181+
qr = cube[(ir = UNCUBE(r))];
182+
qg = cube[(ig = UNCUBE(g))];
183+
qb = cube[(ib = UNCUBE(b))];
184+
if (SQR(qr - r) + SQR(qg - g) + SQR(qb - b) <=
185+
SQR(ql - r) + SQR(ql - g) + SQR(ql - b))
186+
return ir * 36 + ig * 6 + ib + 020;
187+
return il + 0350;
188+
}
189+
190+
static std::string set_xterm256_foreground(int r, int g, int b) {
191+
int x = rgb2xterm256(r, g, b);
192+
std::ostringstream oss;
193+
oss << "\033[38;5;" << x << "m";
194+
return oss.str();
195+
}
196+
197+
// Lowest is red, middle is yellow, highest is green. Color scheme from
198+
// Paul Tol; it is colorblind friendly https://personal.sron.nl/~pault/
173199
const std::vector<std::string> k_colors = {
174-
"\033[38;5;196m", "\033[38;5;202m", "\033[38;5;208m", "\033[38;5;214m", "\033[38;5;220m",
175-
"\033[38;5;226m", "\033[38;5;190m", "\033[38;5;154m", "\033[38;5;118m", "\033[38;5;82m",
200+
set_xterm256_foreground(220, 5, 12),
201+
set_xterm256_foreground(232, 96, 28),
202+
set_xterm256_foreground(241, 147, 45),
203+
set_xterm256_foreground(246, 193, 65),
204+
set_xterm256_foreground(247, 240, 86),
205+
set_xterm256_foreground(144, 201, 135),
206+
set_xterm256_foreground( 78, 178, 101),
176207
};
177208

178209
//

0 commit comments

Comments
 (0)