splinter: the theme was unreadable, and here is why
Two real defects, both measurable: 1. COLORS was read before start_color(), so it was 0 and the colour mapper took the eight-basic-colour branch. Every palette collapsed to white on black - the theme was effectively ignored. 2. A CDE palette is eight mid-tones. Painted literally, Delphinium's accent sat 8% away from the background in luma and its secondary text 14%: readable on a 1995 desktop with a light backdrop, unreadable in a terminal. Fix: the background is darkened (same hue) until its luma is a terminal background, then each role is pushed away from it - foreground 120k, accent 100k, labels 60k in luma units. Delphinium now gives 14:1 for text, 7.7:1 for accent and 4.7:1 for labels on a slate background, with the palette's blues intact. Verified in the escape stream: 38;5;230 text, 38;5;68 and 38;5;74 accents, 48;5;235 surface. sp_color_darken and sp_color_separate live in theme.c so any future consumer gets the same rule, and the deltas are named in one place instead of spread over four init_pair calls.
This commit is contained in:
1 parent
8fa3e9d7bb
commit
dc35261ed2
5 files changed
+108
-11
No files matched your search
@@ -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
|
||||
|
||||
Binary file not shown.
+32
@@ -5,6 +5,7 @@
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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)
|
||||
{
|
||||
|
||||
+12
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user