Files
splinter/src/theme.c
T
lotherkandClaude Opus 5.5 a77641b699 ui: one text field that only grows; themes are Xresources or a CDE palette
The chat is now the whole screen and the input line the row below it: no
sidebar, no status bar. Entries are append only -- a growing answer stays in
its entry while it is the last one and continues in a new entry once a tool
line follows, so nothing that was on screen ever moves. The prompt carries
the agent name ("build> "), /widgets shows what plugins published, and
/status still prints the status segments.

The default theme is Xresources: *foreground, *background and *color0..15
from xrdb -query (or ~/.Xresources), falling back to the terminal's own
colour indices. Every CDE palette is a theme by its own name, painted the
way the desktop paints it (set 5 windows, set 4 text areas, set 6 dialogs,
set 1 active frame, set 2 inactive frame) with CDE's black-or-white rule.
"Default" is therefore the CDE Default palette again, which is what the
config asked for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 20:59:13 +00:00

411 lines
10 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 <strings.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];
}
#define LIGHT 127500 /* luma above this: a light surface, black text */
/* CDE's rule for the text on a colour set: black on light, white on dark */
static void
cde_fg(const unsigned char bg[3], unsigned char fg[3])
{
unsigned char v = luma(bg) > LIGHT ? 0 : 255;
fg[0] = fg[1] = fg[2] = v;
}
static const struct sp_palette *
find_palette(const char *name)
{
size_t i;
for (i = 0; i < sp_palette_count; i++) {
if (strcasecmp(sp_palettes[i].name, name) == 0)
return &sp_palettes[i];
}
return NULL;
}
/* the roles of a CDE palette, by the colour set the desktop uses for each */
static void
load_cde(const struct sp_palette *p, struct sp_theme *out)
{
memset(out, 0, sizeof(*out));
out->name = p->name;
memcpy(out->bg, p->c[4], 3); /* set 5: main windows */
cde_fg(out->bg, out->fg);
memcpy(out->accent, p->c[0], 3); /* set 1: active frame */
memcpy(out->border, p->c[1], 3); /* set 2: inactive frame */
memcpy(out->input_bg, p->c[3], 3); /* set 4: text areas */
cde_fg(out->input_bg, out->input_fg);
memcpy(out->status, p->c[5], 3); /* set 6: dialogs, menus */
cde_fg(out->status, out->status_fg);
if (luma(out->bg) > LIGHT) {
out->error[0] = 0xa0; out->error[1] = 0x10; out->error[2] = 0x10;
} else {
out->error[0] = 0xff; out->error[1] = 0x70; out->error[2] = 0x70;
}
/* a frame colour as text on the window colour: legible, or pushed */
sp_color_separate(out->accent, out->bg, 70000);
sp_color_separate(out->border, out->bg, 50000);
sp_color_separate(out->error, out->bg, 60000);
}
/* "#rgb", "#rrggbb", "#rrrrggggbbbb" or "rgb:r/g/b"; 0 when it is none */
static int
parse_color(const char *v, unsigned char c[3])
{
unsigned int x[3];
size_t n, i, w;
char part[8];
while (*v == ' ' || *v == '\t')
v++;
if (strncmp(v, "rgb:", 4) == 0) {
const char *q = v + 4;
for (i = 0; i < 3; i++) {
n = strspn(q, "0123456789abcdefABCDEF");
if (n == 0 || n > 4)
return 0;
memcpy(part, q, n);
part[n] = '\0';
x[i] = (unsigned int)strtoul(part, NULL, 16);
/* scale 1..4 hex digits to 8 bits */
x[i] = (unsigned int)(x[i] * 255 / ((1u << (4 * n)) - 1));
q += n;
if (i < 2 && *q++ != '/')
return 0;
}
} else if (*v == '#') {
v++;
n = strspn(v, "0123456789abcdefABCDEF");
if (n != 3 && n != 6 && n != 12)
return 0;
w = n / 3;
for (i = 0; i < 3; i++) {
memcpy(part, v + i * w, w);
part[w] = '\0';
x[i] = (unsigned int)strtoul(part, NULL, 16);
x[i] = (unsigned int)(x[i] * 255 / ((1u << (4 * w)) - 1));
}
} else
return 0;
for (i = 0; i < 3; i++)
c[i] = (unsigned char)x[i];
return 1;
}
/*
* One resource line: "*background:\t#1d1f21". Only the generic names count
* ("*name" or "*.name"); "URxvt.background" belongs to one program.
*/
static int
xrdb_line(const char *line, char *name, size_t ncap, const char **val,
int *tight)
{
const char *colon = strchr(line, ':');
size_t n;
if (line[0] != '*' || colon == NULL)
return 0;
line++;
*tight = *line == '.';
if (*tight)
line++;
n = (size_t)(colon - line);
while (n > 0 && (line[n - 1] == ' ' || line[n - 1] == '\t'))
n--;
if (n == 0 || n >= ncap || memchr(line, '.', n) != NULL ||
memchr(line, '*', n) != NULL)
return 0;
memcpy(name, line, n);
name[n] = '\0';
*val = colon + 1;
return 1;
}
int
sp_theme_from_xrdb(const char *text, struct sp_theme *out)
{
unsigned char col[16][3], fg[3], bg[3];
int have[16], hfg = 0, hbg = 0, any = 0, i, dark, tight;
/*
* "*.background" beats "*background" (a tight binding is the closer
* match), whatever order the lines come in -- xrdb -query sorts them.
* have/hfg/hbg: 0 none, 1 from a loose line, 2 from a tight one.
*/
const char *p = text;
memset(out, 0, sizeof(*out));
memset(have, 0, sizeof(have));
out->name = SP_THEME_XRESOURCES;
while (p != NULL && *p != '\0') {
const char *nl = strchr(p, '\n'), *val;
char line[512], name[64];
size_t n = nl != NULL ? (size_t)(nl - p) : strlen(p);
if (n >= sizeof(line))
n = sizeof(line) - 1;
memcpy(line, p, n);
line[n] = '\0';
p = nl != NULL ? nl + 1 : NULL;
if (!xrdb_line(line, name, sizeof(name), &val, &tight))
continue;
if (strcasecmp(name, "foreground") == 0) {
if (hfg < 1 + tight && parse_color(val, fg))
hfg = 1 + tight;
} else if (strcasecmp(name, "background") == 0) {
if (hbg < 1 + tight && parse_color(val, bg))
hbg = 1 + tight;
} else if (strncasecmp(name, "color", 5) == 0 &&
name[5] >= '0' && name[5] <= '9') {
int k = atoi(name + 5);
unsigned char c[3];
if (k >= 0 && k < 16 && have[k] < 1 + tight &&
parse_color(val, c)) {
memcpy(col[k], c, 3);
have[k] = 1 + tight;
any = 1;
}
}
if (hfg || hbg)
any = 1;
}
if (!any) {
/* the terminal's own colours, by index: its palette shows */
out->indexed = 1;
out->idx_fg = 7;
out->idx_bg = 0;
out->idx_border = 8;
out->idx_accent = 12;
out->idx_error = 9;
return 0;
}
if (!hbg) {
if (have[0])
memcpy(bg, col[0], 3);
else
memset(bg, 0, 3);
}
if (!hfg) {
if (have[7])
memcpy(fg, col[7], 3);
else
memset(fg, 0xc0, 3);
}
dark = luma(bg) <= LIGHT;
memcpy(out->bg, bg, 3);
memcpy(out->fg, fg, 3);
memcpy(out->input_bg, bg, 3);
memcpy(out->input_fg, fg, 3);
/* the bright blue on a dark background, the plain one on a light one */
i = dark ? (have[12] ? 12 : 4) : (have[4] ? 4 : 12);
if (have[i])
memcpy(out->accent, col[i], 3);
else
memcpy(out->accent, fg, 3);
i = have[8] ? 8 : 7;
if (have[i])
memcpy(out->border, col[i], 3);
else
memcpy(out->border, fg, 3);
i = dark ? (have[9] ? 9 : 1) : (have[1] ? 1 : 9);
if (have[i])
memcpy(out->error, col[i], 3);
else {
out->error[0] = 0xff; out->error[1] = 0x50; out->error[2] = 0x50;
}
i = dark ? (have[8] ? 8 : 0) : (have[7] ? 7 : 15);
if (have[i])
memcpy(out->status, col[i], 3);
else
memcpy(out->status, fg, 3);
memcpy(out->status_fg, fg, 3);
sp_color_separate(out->status_fg, out->status, 90000);
sp_color_separate(out->accent, out->bg, 50000);
sp_color_separate(out->border, out->bg, 40000);
sp_color_separate(out->error, out->bg, 50000);
return 0;
}
/* the resource text: the X server's database, or the file it is loaded from */
static char *
xrdb_text(void)
{
struct sp_buf b;
char chunk[4096], path[700];
const char *home = getenv("HOME");
FILE *f = NULL;
size_t n;
int k;
if (sp_buf_init(&b) != 0)
return NULL;
if (getenv("DISPLAY") != NULL && *getenv("DISPLAY") != '\0') {
f = popen("xrdb -query 2>/dev/null </dev/null", "r");
if (f != NULL) {
while ((n = fread(chunk, 1, sizeof(chunk), f)) > 0)
(void)sp_buf_add(&b, chunk, n);
(void)pclose(f);
}
}
for (k = 0; b.len == 0 && home != NULL && k < 2; k++) {
snprintf(path, sizeof(path), "%s/%s", home,
k == 0 ? ".Xresources" : ".Xdefaults");
if ((f = fopen(path, "r")) == NULL)
continue;
while ((n = fread(chunk, 1, sizeof(chunk), f)) > 0)
(void)sp_buf_add(&b, chunk, n);
fclose(f);
}
if (b.len == 0) {
sp_buf_free(&b);
return NULL;
}
return b.p; /* the caller frees it */
}
int
sp_theme_load(const char *name, struct sp_theme *out, struct sp_buf *err)
{
const struct sp_palette *p;
if (name == NULL || *name == '\0')
name = SP_THEME_DEFAULT;
if (strcasecmp(name, SP_THEME_XRESOURCES) == 0) {
char *text = xrdb_text();
int rc = sp_theme_from_xrdb(text, out);
free(text);
return rc;
}
p = find_palette(name);
if (p == NULL) {
sp_buf_printf(err, "theme: unknown theme '%s' (Xresources or "
"one of %zu CDE palettes; --list-themes)", name,
sp_palette_count);
return -1;
}
load_cde(p, out);
return 0;
}
size_t
sp_theme_list(const char ***names, size_t *count)
{
static const char *list[257];
size_t i, n = 0;
list[n++] = SP_THEME_XRESOURCES;
for (i = 0; i < sp_palette_count && n < 257; 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;
}