tmenu

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.inz.fi/tmenu
Log | Files | Refs | README | LICENSE

commit 7b9c7e3aa7bab3437e245be44c1cb2e7658dea67
parent 8fa055d5969aae5b7c6289a1029823ed94edb9bd
Author: Santtu Lakkala <santtu.lakkala@unikie.com>
Date:   Wed, 11 Mar 2026 14:22:11 +0200

Add 256 color support

Diffstat:
Mdrw.c | 86++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 72 insertions(+), 14 deletions(-)

diff --git a/drw.c b/drw.c @@ -20,6 +20,7 @@ static char *cap_setaf; static char *cap_setab; static char *cap_sc; static char *cap_rc; +static int termcolors = 8; static int ttyputc(int c) @@ -83,28 +84,82 @@ utf8decode(const char *s_in, long *u, int *err) static short rgb2color(int r, int g, int b) { - static const struct { short id; int r, g, b; } map[] = { + static const struct { short id; int r, g, b; } vga[] = { { 0, 0x00, 0x00, 0x00 }, - { 1, 0xcd, 0x00, 0x00 }, - { 2, 0x00, 0xcd, 0x00 }, - { 3, 0xcd, 0xcd, 0x00 }, - { 4, 0x00, 0x00, 0xee }, - { 5, 0xcd, 0x00, 0xcd }, - { 6, 0x00, 0xcd, 0xcd }, - { 7, 0xe5, 0xe5, 0xe5 }, + { 1, 0x80, 0x00, 0x00 }, + { 2, 0x00, 0x80, 0x00 }, + { 3, 0x80, 0x80, 0x00 }, + { 4, 0x00, 0x00, 0x80 }, + { 5, 0x80, 0x00, 0x80 }, + { 6, 0x00, 0x80, 0x80 }, + { 7, 0xc0, 0xc0, 0xc0 }, + { 8, 0x80, 0x80, 0x80 }, + { 9, 0xff, 0x00, 0x00 }, + { 10, 0x00, 0xff, 0x00 }, + { 11, 0xff, 0xff, 0x00 }, + { 12, 0x00, 0x00, 0xff }, + { 13, 0xff, 0x00, 0xff }, + { 14, 0x00, 0xff, 0xff }, + { 15, 0xff, 0xff, 0xff }, }; + static const int cube[] = { 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff }; int best = 0, bestdist = INT_MAX, i, dist; - for (i = 0; i < LENGTH(map); i++) { - dist = (r - map[i].r) * (r - map[i].r) - + (g - map[i].g) * (g - map[i].g) - + (b - map[i].b) * (b - map[i].b); + if (termcolors >= 256) { + int cr = 0, cg = 0, cb = 0, gray, grayv, idx; + + for (i = 1; i < LENGTH(cube); i++) { + if (abs(r - cube[i]) < abs(r - cube[cr])) + cr = i; + if (abs(g - cube[i]) < abs(g - cube[cg])) + cg = i; + if (abs(b - cube[i]) < abs(b - cube[cb])) + cb = i; + } + + for (i = 0; i < LENGTH(vga); i++) { + dist = (r - vga[i].r) * (r - vga[i].r) + + (g - vga[i].g) * (g - vga[i].g) + + (b - vga[i].b) * (b - vga[i].b); + if (dist < bestdist) { + best = vga[i].id; + bestdist = dist; + } + } + + idx = 16 + 36 * cr + 6 * cg + cb; + dist = (r - cube[cr]) * (r - cube[cr]) + + (g - cube[cg]) * (g - cube[cg]) + + (b - cube[cb]) * (b - cube[cb]); + if (dist < bestdist) + best = idx, bestdist = dist; + + gray = (r + g + b) / 3; + gray = (gray - 8 + 5) / 10; + if (gray < 0) + gray = 0; + if (gray > 23) + gray = 23; + idx = 232 + gray; + grayv = 8 + gray * 10; + dist = (r - grayv) * (r - grayv) + + (g - grayv) * (g - grayv) + + (b - grayv) * (b - grayv); + if (dist < bestdist) + best = idx; + return best; + } + + for (i = 0; i < 8; i++) { + dist = (r - vga[i].r) * (r - vga[i].r) + + (g - vga[i].g) * (g - vga[i].g) + + (b - vga[i].b) * (b - vga[i].b); if (dist < bestdist) { - best = i; + best = vga[i].id; bestdist = dist; } } - return map[best].id; + return best; } static short @@ -288,12 +343,15 @@ void drw_term_init(FILE *out) { ttyout = out; + termcolors = tigetnum("colors"); cap_cup = tigetstr("cup"); cap_sgr0 = tigetstr("sgr0"); cap_setaf = tigetstr("setaf"); cap_setab = tigetstr("setab"); cap_sc = tigetstr("sc"); cap_rc = tigetstr("rc"); + if (termcolors < 0) + termcolors = 8; if (cap_cup == (char *)-1) cap_cup = NULL; if (cap_sgr0 == (char *)-1)