Colors are represented as a sum type supporting 16 basic ANSI colors, 256-color palette indices, and 24-bit truecolor with optional alpha. Constructors clamp components to their valid ranges instead of raising.
The distinguished default color is Rgba \{r=0; g=0; b=0; a=0\} (fully transparent). Renderers treat alpha = 0 as "use terminal default" and emit SGR 39/49.
Note.pack/unpack require a 64-bit platform (Sys.int_size >= 62).
Extended palette index in [0, 255]. Indices 0–15 are basic colors, 16–231 the 6×6×6 RGB cube (r*36 + g*6 + b + 16 where r,g,b ∈ [0,5]), 232–255 a 24-level grayscale ramp. Out-of-range values are clamped.
*)
| Rgbof{
r : int;
g : int;
b : int;
}
(*
Truecolor RGB. Components in [0, 255], clamped.
*)
| Rgbaof{
r : int;
g : int;
b : int;
a : int;
}
(*
Truecolor RGBA. Alpha in [0, 255] where 0 is fully transparent and 255 fully opaque. Alpha is used for blending but not directly emitted to the terminal.
of_rgba_f r g b a is a color from normalized RGBA floats in [0.0, 1.0]. Components are clamped and converted to 8-bit integers. Inverse of to_rgba_f within rounding tolerance.
grayscale ~level is a grayscale color. level is in [0, 23] where 0 is darkest and 23 lightest; out-of-range values are clamped. Maps to palette indices 232–255.
equal a b is true iff a and b have identical RGBA components. Colors with different representations but identical RGBA values are equal (e.g. Extended 0 equals Black).
to_rgb c is the RGB components of c in [0, 255], discarding alpha. default maps to (0, 0, 0); use alpha or to_rgba to distinguish it from explicit black.
downgrade ~level c converts c to the specified color depth using nearest-neighbor matching in RGB space (squared Euclidean distance). level defaults to detection from environment variables (COLORTERM, TERM). `Truecolor returns c unchanged. Transparent colors (default) pass through unchanged.
to_sgr_codes ~bg c is the SGR parameter codes for c. If bg is true, generates background codes; otherwise foreground codes. Uses the most compact encoding for each color variant.
Sourceval emit_sgr_codes : bg:bool ->(int -> unit)->t-> unit
emit_sgr_codes ~bg push c emits SGR codes for c via the push callback. Zero-allocation alternative to to_sgr_codes.