Repository navigation
Colors and Attributes
Each cell has a foreground (fg) and background (bg) lt_attr (a uint32_t). The bits carry both color and attributes:
31 30 ........ 24 23 .................... 0
| |___________| |____________________|
| attribute bits color bits
HI_BLACK (fg only) (24-bit / palette / named)
- Bits 0–23 — the color value. How it's interpreted depends on the output mode (below).
-
Bits 24–30 — the 7 attribute flags. Attributes are read from
fgonly; attribute bits inbgare ignored. -
Bit 31 (
LT_HI_BLACK) — sentinel, see below.
Set with lt_set_output_mode(mode); query the terminal's capability with lt_detect_color_depth().
| Mode | Color bits mean |
|---|---|
LT_OUTPUT_NORMAL |
a named color, LT_BLACK…LT_WHITE (1–8) |
LT_OUTPUT_256 |
an 8-bit palette index (0–255) |
LT_OUTPUT_216 |
an index into the 6×6×6 color cube |
LT_OUTPUT_GRAYSCALE |
an index into the 24-step gray ramp |
LT_OUTPUT_TRUECOLOR |
24-bit RGB (use LT_RGB or LT_HEX) |
Changing the mode reinterprets every cell's color bits and forces the next present to re-emit colors.
int depth = lt_detect_color_depth(); // TRUECOLOR / 256 / NORMAL
lt_set_output_mode(depth);LT_DEFAULT (0), LT_BLACK, LT_RED, LT_GREEN, LT_YELLOW, LT_BLUE, LT_MAGENTA, LT_CYAN, LT_WHITE.
LT_DEFAULT (color bits = 0) means the terminal's default foreground/background, in any mode.
In LT_OUTPUT_TRUECOLOR, pack RGB into the color bits with LT_RGB:
lt_attr orange = LT_RGB(255, 128, 0);
lt_set_cell(0, 0, 'X', orange, LT_RGB(20, 20, 20));If you already hold a color as a single 0xRRGGBB hex value, LT_HEX packs it directly — LT_HEX(0xRRGGBB) packs the same bits as LT_RGB(0xRR, 0xGG, 0xBB):
lt_attr orange = LT_HEX(0xFF8000); // == LT_RGB(255, 128, 0)
lt_set_cell(0, 0, 'X', orange, LT_HEX(0x141414));LT_HEX masks off the high byte (bits 24–31), so a value with stray high bits — e.g. 0xFFFFFFFF — packs as pure white and can never accidentally set an attribute flag or the LT_HI_BLACK sentinel. The default-vs-black rule is the same as LT_RGB: LT_HEX(0x000000) is the terminal default; OR in LT_HI_BLACK for real black.
A color whose bits 0–23 are all zero means "terminal default". To select real black instead (palette index 0, or RGB 0,0,0), OR in LT_HI_BLACK:
lt_attr term_default = 0; // terminal default fg
lt_attr real_black = LT_HI_BLACK; // actual black
lt_attr black_rgb = LT_RGB(0,0,0) | LT_HI_BLACK;OR these into fg:
| Flag | |
|---|---|
LT_BOLD |
bold |
LT_DIM |
faint |
LT_ITALIC |
italic |
LT_UNDERLINE |
underline |
LT_BLINK |
blink |
LT_REVERSE |
reverse video |
LT_STRIKE |
strikethrough |
lt_set_cell(0, 0, 'A', LT_RED | LT_BOLD | LT_UNDERLINE, LT_DEFAULT);
lt_print(2, 1, LT_CYAN | LT_ITALIC, LT_DEFAULT, "italic cyan");Terminal support for individual attributes varies (blink and strike especially); libterm emits the SGR codes regardless.
lt_detect_color_depth tells you how many colors the terminal supports; these tell you which colors it is actually using:
uint32_t rgb;
int rc = lt_query_color(LT_COLOR_DEFAULT_BG, &rgb, 500); // 0x00RRGGBB
int dark = lt_is_dark_background(500); // 1 dark / 0 light / <0 error-
lt_query_color(what, &rgb, timeout_ms)— query the default foreground (LT_COLOR_DEFAULT_FG), default background (LT_COLOR_DEFAULT_BG), or a palette index0..255. On success*rgbholds a packed0x00RRGGBB— the same packing as truecolorlt_attr, so the result feeds straight back into a cell attribute viaLT_HEX(rgb). A negativetimeout_msselects a 1000 ms default. -
lt_is_dark_background(timeout_ms)— queries the default background and thresholds its ITU-R BT.709 relative luminance:1dark,0light, or a negativeLT_ERR_*passed through.
How it works. On POSIX this is a real round-trip: libterm emits the standard OSC 10 / 11 / 4 query and reads the terminal's reply off the input stream (both rgb: and URxvt's rgba: reply forms are accepted). Anything you type while the query is in flight is preserved and delivered by the next lt_poll_event / lt_peek_event — escape sequences included. On Windows the answer comes natively from the console color table (GetConsoleScreenBufferInfoEx): immediate, version-independent, but palette indexes above 15 return LT_ERR_UNSUPPORTED_TERM (the Win32 table has 16 slots).
The recommended pattern — treat errors as "unknown" and fall back to a default theme:
int dark = lt_is_dark_background(500);
if (dark < 0)
dark = 1; /* terminal didn't answer — assume dark and carry on */Caveats: under tmux the replies describe tmux's own colors (it answers OSC 10/11 itself); a terminal that never answers yields LT_ERR_NO_EVENT after the timeout. Both functions require lt_init first. See the theme example for the full pattern in action.