![]() |
libghostty
|
Color parsing, palette generation, color math, and X11 color name utilities shared by libghostty-vt.
These APIs expose Ghostty's color semantics directly to embedders. Use them when an application needs to parse the same color strings as Ghostty config and theme files, generate the same 256-color palette used by the terminal, list supported X11 color names, or make UI decisions from luminance and contrast values.
ghostty_color_parse() accepts the flexible syntax used by Ghostty for terminal colors:
Leading and trailing spaces and tabs are ignored. Use ghostty_color_parse_x11() when only X11 names should be accepted.
ghostty_color_parse_palette_entry() parses a single Ghostty palette override in INDEX=COLOR form. The index may be decimal or use a 0x, 0o, or 0b prefix. The color side uses ghostty_color_parse().
ghostty_color_palette_generate() derives the 216-color cube and grayscale ramp from a base palette, background, and foreground. Set bits in GhosttyColorPaletteMask preserve specific indices from the base palette. The output may alias the base input.
The X11 name table is static program-lifetime memory. Entries are in rgb.txt order and are terminated by an entry with name == NULL. ghostty_color_x11_name_count() returns the number of non-terminator entries.
Typedefs | |
| typedef uint8_t | GhosttyColorPaletteIndex |
Macros | |
| #define | GHOSTTY_COLOR_PALETTE_MASK_WORD(index) |
| #define | GHOSTTY_COLOR_PALETTE_MASK_BIT(index) |
| #define | GHOSTTY_COLOR_PALETTE_MASK_SET(mask, index) |
| #define | GHOSTTY_COLOR_PALETTE_MASK_UNSET(mask, index) |
| #define | GHOSTTY_COLOR_PALETTE_MASK_IS_SET(mask, index) |
| #define | GHOSTTY_COLOR_NAMED_BLACK 0 |
| #define | GHOSTTY_COLOR_NAMED_RED 1 |
| #define | GHOSTTY_COLOR_NAMED_GREEN 2 |
| #define | GHOSTTY_COLOR_NAMED_YELLOW 3 |
| #define | GHOSTTY_COLOR_NAMED_BLUE 4 |
| #define | GHOSTTY_COLOR_NAMED_MAGENTA 5 |
| #define | GHOSTTY_COLOR_NAMED_CYAN 6 |
| #define | GHOSTTY_COLOR_NAMED_WHITE 7 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_BLACK 8 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_RED 9 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_GREEN 10 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_YELLOW 11 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_BLUE 12 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_MAGENTA 13 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_CYAN 14 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_WHITE 15 |
Data Structures | |
| struct | GhosttyColorRgb |
| struct | GhosttyColorPaletteMask |
| struct | GhosttyColorX11Entry |
| typedef uint8_t GhosttyColorPaletteIndex |
| GHOSTTY_API double ghostty_color_contrast | ( | const GhosttyColorRgb * | a, |
| const GhosttyColorRgb * | b ) |
Calculate the WCAG contrast ratio between two RGB colors.
The contrast ratio is symmetric and ranges from 1.0 for identical colors to 21.0 for black and white.
| a | The first RGB color (must not be NULL) |
| b | The second RGB color (must not be NULL) |
References ghostty_color_contrast().
Referenced by ghostty_color_contrast().
| GHOSTTY_API double ghostty_color_luminance | ( | const GhosttyColorRgb * | color | ) |
Calculate W3C relative luminance for an RGB color.
Returns a normalized value from 0.0 for black to 1.0 for white. See https://www.w3.org/TR/WCAG20/#relativeluminancedef.
| color | The RGB color (must not be NULL) |
References ghostty_color_luminance().
Referenced by ghostty_color_luminance().
| GHOSTTY_API void ghostty_color_palette_default | ( | GhosttyColorRgb * | out | ) |
Get Ghostty's built-in default 256-color palette.
Writes exactly 256 entries: Ghostty's base16 defaults, the xterm 6x6x6 color cube, and the grayscale ramp.
| [out] | out | The output palette, an array of exactly 256 GhosttyColorRgb values |
References ghostty_color_palette_default().
Referenced by ghostty_color_palette_default().
| GHOSTTY_API void ghostty_color_palette_generate | ( | const GhosttyColorRgb * | base, |
| const GhosttyColorPaletteMask * | skip, | ||
| const GhosttyColorRgb * | bg, | ||
| const GhosttyColorRgb * | fg, | ||
| bool | harmonious, | ||
| GhosttyColorRgb * | out ) |
Generate a 256-color palette from base colors.
The base palette supplies indices 0-15, which are always preserved. If base is NULL, Ghostty's default palette is used. If skip is NULL, no extra indices are skipped. Set bits in skip preserve those indices from base. The 216-color cube at indices 16-231 is generated with trilinear CIELAB interpolation, and the grayscale ramp at indices 232-255 is interpolated from the background to the foreground.
For light themes, harmonious controls whether the generated palette keeps the background-to-foreground orientation. When false, Ghostty swaps the light background and dark foreground so the cube and ramp run dark-to-light. The output palette may be the same pointer as base.
| base | The base palette, an array of exactly 256 GhosttyColorRgb values, or NULL to use Ghostty's default palette | |
| skip | The palette indices to preserve from base, or NULL for an empty mask | |
| bg | The terminal background color (must not be NULL) | |
| fg | The terminal foreground color (must not be NULL) | |
| harmonious | Whether light themes keep background-to-foreground orientation | |
| [out] | out | The output palette, an array of exactly 256 GhosttyColorRgb values |
References ghostty_color_palette_generate().
Referenced by ghostty_color_palette_generate().
| GHOSTTY_API GhosttyResult ghostty_color_parse | ( | const char * | value, |
| size_t | len, | ||
| GhosttyColorRgb * | out ) |
Parse a flexible Ghostty color value.
Accepts Ghostty's terminal color syntax: X11 color names, hex colors in 3-, 6-, 9-, or 12-digit form (the leading # is optional for 3- and 6-digit values), and rgb:<red>/<green>/<blue> or rgbi:<red>/<green>/<blue> specifications. Leading and trailing spaces and tabs are trimmed.
| value | The color value bytes (must not be NULL) | |
| len | The length of value in bytes | |
| [out] | out | The parsed RGB color |
value is NULL References ghostty_color_parse().
Referenced by ghostty_color_parse().
| GHOSTTY_API GhosttyResult ghostty_color_parse_palette_entry | ( | const char * | value, |
| size_t | len, | ||
| uint8_t * | out_index, | ||
| GhosttyColorRgb * | out_rgb ) |
Parse a Ghostty palette entry.
Accepts Ghostty palette config syntax: N=COLOR. N is a palette index from 0 to 255 in decimal or in 0x, 0o, or 0b-prefixed form. Spaces and tabs around N and COLOR are ignored. COLOR accepts the same syntax as ghostty_color_parse().
| value | The palette entry bytes (must not be NULL) | |
| len | The length of value in bytes | |
| [out] | out_index | The parsed palette index |
| [out] | out_rgb | The parsed RGB color |
References ghostty_color_parse_palette_entry().
Referenced by ghostty_color_parse_palette_entry().
| GHOSTTY_API GhosttyResult ghostty_color_parse_x11 | ( | const char * | name, |
| size_t | len, | ||
| GhosttyColorRgb * | out ) |
Parse an X11 color name.
The color name is resolved from Ghostty's embedded rgb.txt table. Leading and trailing spaces and tabs are trimmed, and matching is ASCII case-insensitive. Hex values are not accepted by this function.
| name | The color name bytes (must not be NULL) | |
| len | The length of name in bytes | |
| [out] | out | The parsed RGB color |
name is NULL References ghostty_color_parse_x11().
Referenced by ghostty_color_parse_x11().
| GHOSTTY_API double ghostty_color_perceived_luminance | ( | const GhosttyColorRgb * | color | ) |
Calculate perceived luminance for an RGB color.
Returns a normalized value from 0.0 for black to 1.0 for white. Ghostty treats a background color as light when this exceeds 0.5. This is not the metric used internally by ghostty_color_palette_generate(), which uses CIELAB lightness.
| color | The RGB color (must not be NULL) |
References ghostty_color_perceived_luminance().
Referenced by ghostty_color_perceived_luminance().
| GHOSTTY_API void ghostty_color_rgb_get | ( | const GhosttyColorRgb * | color, |
| uint8_t * | r, | ||
| uint8_t * | g, | ||
| uint8_t * | b ) |
Get the RGB color components.
This function extracts the individual red, green, and blue components from a GhosttyColorRgb value. Primarily useful in WebAssembly environments where accessing struct fields directly is difficult.
| color | Pointer to the RGB color value |
| r | Pointer to store the red component (0-255) |
| g | Pointer to store the green component (0-255) |
| b | Pointer to store the blue component (0-255) |
References ghostty_color_rgb_get().
Referenced by ghostty_color_rgb_get().
| GHOSTTY_API size_t ghostty_color_x11_name_count | ( | void | ) |
Get the number of X11 color name entries.
The returned count excludes the NULL terminator and is provided so bindings can preallocate storage before reading ghostty_color_x11_names().
References ghostty_color_x11_name_count().
Referenced by ghostty_color_x11_name_count().
| GHOSTTY_API const GhosttyColorX11Entry * ghostty_color_x11_names | ( | void | ) |
Get Ghostty's X11 color name table.
The returned pointer references static memory valid for the program lifetime and is never NULL. Entries are in rgb.txt order and are terminated by an entry with name == NULL. Aliases are separate entries, such as "medium spring green" and "MediumSpringGreen". Names are the exact supported spellings from rgb.txt; ghostty_color_parse_x11() also matches them case-insensitively.
References ghostty_color_x11_names().
Referenced by ghostty_color_x11_names().
| #define GHOSTTY_COLOR_NAMED_BRIGHT_BLACK 8 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_BLUE 12 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_CYAN 14 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_GREEN 10 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_MAGENTA 13 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_RED 9 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_WHITE 15 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_YELLOW 11 |
| #define GHOSTTY_COLOR_PALETTE_MASK_BIT | ( | index | ) |
| #define GHOSTTY_COLOR_PALETTE_MASK_IS_SET | ( | mask, | |
| index ) |
Test whether a palette mask index is set.
| mask | Pointer to a GhosttyColorPaletteMask |
| index | The palette index (0-255) |
| #define GHOSTTY_COLOR_PALETTE_MASK_SET | ( | mask, | |
| index ) |
Set a palette mask index.
| mask | Pointer to a GhosttyColorPaletteMask |
| index | The palette index (0-255) |
| #define GHOSTTY_COLOR_PALETTE_MASK_UNSET | ( | mask, | |
| index ) |
Clear a palette mask index.
| mask | Pointer to a GhosttyColorPaletteMask |
| index | The palette index (0-255) |