Skip to content

Add support for 256 color indexed palette#9

Merged
the-eugen merged 7 commits intothe-eugen:masterfrom
pbutsykin:256_color_palette
Mar 3, 2026
Merged

Add support for 256 color indexed palette#9
the-eugen merged 7 commits intothe-eugen:masterfrom
pbutsykin:256_color_palette

Conversation

@pbutsykin
Copy link
Contributor

No description provided.

Signed-off-by: Pavel Butsykin <p.butsykin@gmail.com>
The cast to size_t is safe here since res == -1 is checked first.

Signed-off-by: Pavel Butsykin <p.butsykin@gmail.com>
Comparison is always true due to limited range of data type
[-Werror=type-limits].

Signed-off-by: Pavel Butsykin <p.butsykin@gmail.com>
Signed-off-by: Pavel Butsykin <p.butsykin@gmail.com>
Signed-off-by: Pavel Butsykin <p.butsykin@gmail.com>
Signed-off-by: Pavel Butsykin <p.butsykin@gmail.com>
Copy link
Owner

@the-eugen the-eugen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Just a couple minor questions.

vtrenderlib.c Outdated
Comment on lines +444 to +447
if (fgc < VTR_COLOR_SGR8_TOTAL) {
seq[3] = (fgc == VTR_COLOR_DEFAULT ? '9' : '0' + fgc - 1);
seq[4] = 'm';
return 5;
Copy link
Owner

@the-eugen the-eugen Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do this we substitute 8-bit 0-8 color values with the default terminal palette. Is that fine because the 8-bit space is backwards-compatible or are we losing some 8-bit colors that way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's fine, and we are not losing any colors because the first 8 entries in the 256 color palette are shared with ANSI colors 0-7.
https://github.com/ThomasDickey/xterm-snapshots/blob/5afc76e089167da6d07c49334d0bf350377f6c0a/terminfo#L2182

In the updated patch I removed the fallback to legacy ANSI colors to simplify set_foreground_color_s().

vtrenderlib.c Outdated
seq[4] = 'm';
return 5;
}
assert(seqcap >= 11);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will now consume more space in the preallocated sequence buffer. Which is correct, we will reallocate it if needed. However, to avoid reallocation during frame rendering we guess what is going to be a reasonable-enough preallocated seq buffer size (see VT_SEQLIST_BUFFER_SIZE). You could fiddle with it to account for extra space for the 8-bit color.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Use the 256-color escape sequence ESC[38;5;INDEXm for all foreground
colors, replacing the legacy SGR 30-37 path. The existing enum vtr_color
values remain valid because the standard ANSI colors 0-7 share the same
palette indices in the 256-color mode. VTR_COLOR_DEFAULT still emits
ESC[39m to reset to the terminal default color.

Palette indices are stored as idx+1 in vtr_fgcolor_t since value 0 is
reserved for VTR_COLOR_DEFAULT. This means index 255 would overflow
uint8_t, so it's mapped to VTR_COLOR_WHITE (the closest color match).

Signed-off-by: Pavel Butsykin <p.butsykin@gmail.com>
@pbutsykin pbutsykin force-pushed the 256_color_palette branch from 9c40690 to 4a90910 Compare March 3, 2026 13:11
Copy link
Owner

@the-eugen the-eugen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@the-eugen the-eugen merged commit 3ef8971 into the-eugen:master Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants