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.
207 lines
4.2 KiB
C
207 lines
4.2 KiB
C
/*
|
|
* splinter -- CDE palettes as terminal themes.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "theme.h"
|
|
#include "palettes.h"
|
|
|
|
static int
|
|
luma(const unsigned char *c)
|
|
{
|
|
/* integer Rec.601 luma, so the ordering is deterministic and testable */
|
|
return 299 * c[0] + 587 * c[1] + 114 * c[2];
|
|
}
|
|
|
|
static int
|
|
saturation(const unsigned char *c)
|
|
{
|
|
int hi = c[0], lo = c[0];
|
|
|
|
if (c[1] > hi)
|
|
hi = c[1];
|
|
if (c[2] > hi)
|
|
hi = c[2];
|
|
if (c[1] < lo)
|
|
lo = c[1];
|
|
if (c[2] < lo)
|
|
lo = c[2];
|
|
return hi - lo;
|
|
}
|
|
|
|
static const struct sp_palette *
|
|
find_palette(const char *name)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < sp_palette_count; i++) {
|
|
if (strcmp(sp_palettes[i].name, name) == 0)
|
|
return &sp_palettes[i];
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
sp_theme_load(const char *name, struct sp_theme *out, struct sp_buf *err)
|
|
{
|
|
const struct sp_palette *p;
|
|
int order[8], i, j;
|
|
int darkest, lightest, second, accent;
|
|
int best;
|
|
|
|
if (name == NULL || *name == '\0')
|
|
name = SP_THEME_DEFAULT;
|
|
p = find_palette(name);
|
|
if (p == NULL) {
|
|
sp_buf_printf(err, "theme: unknown palette '%s' (%zu known)",
|
|
name, sp_palette_count);
|
|
return -1;
|
|
}
|
|
|
|
for (i = 0; i < 8; i++)
|
|
order[i] = i;
|
|
for (i = 1; i < 8; i++) {
|
|
int v = order[i];
|
|
|
|
for (j = i - 1; j >= 0 && luma(p->c[order[j]]) > luma(p->c[v]);
|
|
j--)
|
|
order[j + 1] = order[j];
|
|
order[j + 1] = v;
|
|
}
|
|
|
|
darkest = order[0];
|
|
lightest = order[7];
|
|
second = order[1];
|
|
|
|
accent = -1;
|
|
best = -1;
|
|
for (i = 0; i < 8; i++) {
|
|
int idx = order[i], s;
|
|
|
|
if (idx == darkest || idx == lightest || idx == second)
|
|
continue;
|
|
s = saturation(p->c[idx]);
|
|
if (s > best) {
|
|
best = s;
|
|
accent = idx;
|
|
}
|
|
}
|
|
if (accent < 0)
|
|
accent = lightest; /* a fully grey palette has no accent */
|
|
|
|
memset(out, 0, sizeof(*out));
|
|
out->name = p->name;
|
|
memcpy(out->bg, p->c[darkest], 3);
|
|
memcpy(out->fg, p->c[lightest], 3);
|
|
memcpy(out->border, p->c[second], 3);
|
|
memcpy(out->accent, p->c[accent], 3);
|
|
for (i = 0; i < 3; i++)
|
|
out->status[i] = (unsigned char)(p->c[accent][i] * 2 / 3);
|
|
return 0;
|
|
}
|
|
|
|
size_t
|
|
sp_theme_list(const char ***names, size_t *count)
|
|
{
|
|
static const char *list[256];
|
|
size_t i, n = 0;
|
|
|
|
for (i = 0; i < sp_palette_count && n < 256; i++)
|
|
list[n++] = sp_palettes[i].name;
|
|
*names = list;
|
|
*count = n;
|
|
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)
|
|
{
|
|
static const unsigned char base[16][3] = {
|
|
{ 0, 0, 0}, {128, 0, 0}, { 0, 128, 0}, {128, 128, 0},
|
|
{ 0, 0, 128}, {128, 0, 128}, { 0, 128, 128}, {192, 192, 192},
|
|
{128, 128, 128}, {255, 0, 0}, { 0, 255, 0}, {255, 255, 0},
|
|
{ 0, 0, 255}, {255, 0, 255}, { 0, 255, 255}, {255, 255, 255}
|
|
};
|
|
static const int level[6] = { 0, 95, 135, 175, 215, 255 };
|
|
int i, best = 0, bestd = 1 << 30;
|
|
|
|
if (colors >= 256) {
|
|
int ri = 0, gi = 0, bi = 0, dr, dg, db;
|
|
|
|
for (i = 1; i < 6; i++) {
|
|
if (r >= level[i])
|
|
ri = i;
|
|
}
|
|
for (i = 1; i < 6; i++) {
|
|
if (g >= level[i])
|
|
gi = i;
|
|
}
|
|
for (i = 1; i < 6; i++) {
|
|
if (b >= level[i])
|
|
bi = i;
|
|
}
|
|
dr = r - level[ri];
|
|
dg = g - level[gi];
|
|
db = b - level[bi];
|
|
if (dr * dr + dg * dg + db * db > 3 * 32 * 32) {
|
|
/* closer to a grey than to a cube corner: use the ramp */
|
|
int grey = (r + g + b) / 3;
|
|
int gi2 = (grey - 8) / 10;
|
|
|
|
if (gi2 < 0)
|
|
gi2 = 0;
|
|
if (gi2 > 23)
|
|
gi2 = 23;
|
|
return (short)(232 + gi2);
|
|
}
|
|
return (short)(16 + 36 * ri + 6 * gi + bi);
|
|
}
|
|
|
|
for (i = 0; i < (colors >= 16 ? 16 : 8); i++) {
|
|
int dr = r - base[i][0], dg = g - base[i][1], db = b - base[i][2];
|
|
int d = dr * dr + dg * dg + db * db;
|
|
|
|
if (d < bestd) {
|
|
bestd = d;
|
|
best = i;
|
|
}
|
|
}
|
|
return (short)best;
|
|
}
|