diff --git a/README.md b/README.md index c43fcc1..b39d492 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,18 @@ Both directions are covered by `test_sse`, which feeds recorded lines: plain text, a tool call split across three chunks, two calls in one turn, a broken data line, and the keepalive lines that carry nothing. +## Colour, measured + +A CDE palette is eight mid-tones; painted literally, the accent sat 8% away +from the background and nothing was readable. Two fixes, both in `init_colors`: +the background is darkened until it is a terminal background, and every role is +pushed away from it until it is legible. Delphinium ends up with a white-on-slate +text contrast of 14:1, accents at 7.7:1 and labels at 4.7:1 instead of ~1.1:1. + +The other half was a real bug: `COLORS` was read before `start_color()`, so it +was 0, the code took the eight-basic-colour path, and the whole palette +collapsed to white on black. + ## Screen encoding, measured The vendored NetBSD curses emits Latin-1 bytes for wide characters (`ΓΌ` becomes diff --git a/libsplinter.so b/libsplinter.so index 16e68f1..16709d0 100755 Binary files a/libsplinter.so and b/libsplinter.so differ diff --git a/src/theme.c b/src/theme.c index 6048ea6..2ec1bef 100644 --- a/src/theme.c +++ b/src/theme.c @@ -5,6 +5,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ #include +#include #include #include "theme.h" @@ -117,6 +118,37 @@ sp_theme_list(const char ***names, size_t *count) return n; } +void +sp_color_darken(unsigned char c[3], int target) +{ + int guard = 0; + + while (luma(c) > target && guard++ < 40) { + int i; + + for (i = 0; i < 3; i++) + c[i] = (unsigned char)(c[i] * 3 / 4); + } +} + +void +sp_color_separate(unsigned char c[3], const unsigned char ref[3], int min_delta) +{ + int guard = 0; + int up = luma(c) >= luma(ref); + + while (abs(luma(c) - luma(ref)) < min_delta && guard++ < 64) { + int i; + + for (i = 0; i < 3; i++) { + if (up) + c[i] = (unsigned char)(c[i] + (255 - c[i]) / 4 + 1); + else + c[i] = (unsigned char)(c[i] * 3 / 4); + } + } +} + short sp_color_rgb(unsigned char r, unsigned char g, unsigned char b, short colors) { diff --git a/src/theme.h b/src/theme.h index 073edbd..9340755 100644 --- a/src/theme.h +++ b/src/theme.h @@ -40,6 +40,18 @@ int sp_theme_load(const char *name, struct sp_theme *out, struct sp_buf *err); /* List palette names, alphabetical. Returns the number written. */ size_t sp_theme_list(const char ***names, size_t *count); +/* + * Push a colour until it is at least `min_delta` away (in luma) from `ref`, + * toward white or black depending on which side it already is. A palette that + * is all mid-tones is a nice idea on a 1995 desktop and unreadable in a + * terminal; this is the rule that keeps it legible. + */ +/* Scale a colour toward black until its luma is at most `target`. */ +void sp_color_darken(unsigned char c[3], int target); + +void sp_color_separate(unsigned char c[3], const unsigned char ref[3], + int min_delta); + /* Nearest xterm colour index for an RGB triple, for `colors` palette entries. */ short sp_color_rgb(unsigned char r, unsigned char g, unsigned char b, short colors); diff --git a/src/ui.c b/src/ui.c index 86bdd65..8037d7a 100644 --- a/src/ui.c +++ b/src/ui.c @@ -139,25 +139,66 @@ turn_thread(void *arg) return NULL; } +/* + * Colour assignment, and the reasoning behind it: + * + * The terminal background stays the terminal's own (-1). Painting every glyph + * on a palette colour looks like a 1995 desktop and is unreadable in a modern + * terminal - it also fights whatever scheme the user already chose. The palette + * supplies the accents, which is where its character is, and every accent is + * pushed until it is legible against black (the usual terminal background). + */ +/* + * Colour assignment. + * + * Two facts drive this: + * 1. this curses port reports COLORS=256 but does not honour -1 in + * init_pair(), so "terminal default" is not available - every pair needs + * real indices, and a failed init_pair silently leaves white on black; + * 2. a CDE palette is eight mid-tones. Painted literally, the accent sits + * 8% away from the background and nobody can read it. + * + * So the palette paints, and every role is pushed away from the background + * until it is legible: foreground far, accent very far, secondary text at + * least visible. + */ static void init_colors(const struct sp_theme *t) { - short n = (short)(COLORS > 0 ? COLORS : 8); + unsigned char bg[3], fg[3], accent[3], dim[3], status_bg[3]; + short n; if (!has_colors()) return; + /* COLORS is only valid after start_color(); reading it earlier gave 0, + * which silently fell back to the eight basic colours - the whole palette + * collapsed to white on black. */ start_color(); - use_default_colors(); - init_pair(PAIR_MAIN, sp_color_rgb(t->fg[0], t->fg[1], t->fg[2], n), - sp_color_rgb(t->bg[0], t->bg[1], t->bg[2], n)); - init_pair(PAIR_ACCENT, sp_color_rgb(t->accent[0], t->accent[1], - t->accent[2], n), sp_color_rgb(t->bg[0], t->bg[1], t->bg[2], n)); - init_pair(PAIR_DIM, sp_color_rgb(t->border[0], t->border[1], - t->border[2], n), sp_color_rgb(t->bg[0], t->bg[1], t->bg[2], n)); - init_pair(PAIR_STATUS, sp_color_rgb(t->bg[0], t->bg[1], t->bg[2], n), - sp_color_rgb(t->status[0], t->status[1], t->status[2], n)); -} + n = (short)(COLORS > 0 ? COLORS : 8); + memcpy(bg, t->bg, 3); + /* the darkest CDE colour is still a mid-tone: no terminal wants that */ + sp_color_darken(bg, 45000); + memcpy(fg, t->fg, 3); + memcpy(accent, t->accent, 3); + memcpy(dim, t->border, 3); + memcpy(status_bg, t->status, 3); + + sp_color_separate(fg, bg, 120000); /* text */ + sp_color_separate(accent, bg, 100000); /* the user's own lines */ + sp_color_separate(dim, bg, 60000); /* labels: visible, not loud */ + sp_color_darken(status_bg, 60000); /* the bar is a surface, not text */ + + init_pair(PAIR_MAIN, sp_color_rgb(fg[0], fg[1], fg[2], n), + sp_color_rgb(bg[0], bg[1], bg[2], n)); + init_pair(PAIR_ACCENT, sp_color_rgb(accent[0], accent[1], accent[2], n), + sp_color_rgb(bg[0], bg[1], bg[2], n)); + init_pair(PAIR_DIM, sp_color_rgb(dim[0], dim[1], dim[2], n), + sp_color_rgb(bg[0], bg[1], bg[2], n)); + init_pair(PAIR_STATUS, + sp_color_rgb(fg[0], fg[1], fg[2], n), + sp_color_rgb(status_bg[0], status_bg[1], status_bg[2], n)); +} /* * Text goes to curses as ASCII.