package matrix
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=9e4e90d17f9b2af1b07071fe425bc2c519c849c4f1d1ab73cde512be2d874849
sha512=06e9c4a741590942e81a27738d0b5c0413fafec8cf3b7dae047ad69f155e7b718aa4223818dc161b7d028efffcfd3365905e264d6fd31d453910ddfa91dcf9b9
doc/matrix.ansi/Ansi/Color/index.html
Module Ansi.ColorSource
Terminal colors.
Terminal colors.
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).
Colors
type t = | Black| Red| Green| Yellow| Blue| Magenta| Cyan| White| Bright_black(*Often rendered as gray.
*)| Bright_red| Bright_green| Bright_yellow| Bright_blue| Bright_magenta| Bright_cyan| Bright_white| Extended of int(*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 + 16wherer,g,b∈ [0,5]), 232–255 a 24-level grayscale ramp. Out-of-range values are clamped.| Rgb of {}(*Truecolor RGB. Components in [0, 255], clamped.
*)| Rgba of {}(*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.
*)
The type for terminal colors.
Constructors
of_rgb r g b is a truecolor RGB value. Components are clamped to [0, 255]. The result is opaque (alpha = 255).
of_rgba r g b a is a truecolor RGBA value. Components are clamped to [0, 255].
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.
of_palette_index idx is a color from the 256-color palette at idx, clamped to [0, 255]. Indices 0–15 return the corresponding basic color variant.
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.
of_hsl ~h ~s ~l ?a () is a color from HSL values with:
his hue in degrees [0.0, 360.0\), wrapped. Negative values are normalized (e.g.-10becomes350).sis saturation in [0.0, 1.0], clamped.lis lightness in [0.0, 1.0], clamped.ais alpha in [0.0, 1.0], defaults to1.0, clamped.
of_hex s parses a hex color string. Accepted formats (with or without '#' prefix):
- 3 digits:
"RGB"expanded to"RRGGBB". - 4 digits:
"RGBA"expanded to"RRGGBBAA". - 6 digits:
"RRGGBB"(opaque). - 8 digits:
"RRGGBBAA"(with alpha).
Returns None on invalid format or non-hex characters.
of_hex_exn s is like of_hex but raises Invalid_argument on failure.
Predicates and comparisons
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).
compare a b orders a and b by RGBA components. The order is compatible with equal.
Properties
alpha c is the alpha channel of c as a float in [0.0, 1.0]. Non-RGBA variants return 1.0. default returns 0.0.
to_rgba c is the RGBA components of c in [0, 255]. Non-RGBA variants return alpha = 255.
to_rgba_f c is the RGBA components of c as normalized floats in [0.0, 1.0].
with_rgba_f c f calls f r g b a with normalized RGBA floats. Avoids the tuple allocation of to_rgba_f.
to_hsl c is (h, s, l, a) where h is hue in [0.0, 360.0\), s and l are in [0.0, 1.0], and a is alpha in [0.0, 1.0]. Achromatic colors have hue = 0.
Operations
blend ~mode ~src ~dst () alpha-blends src over dst with:
`Linearstandard alpha compositing.`Perceptualadjusts alpha using perceptual curves for more natural appearance on semi-transparent overlays.
If src alpha ≥ 0.999, returns src RGB unchanged. If alpha ≤ ε, returns dst unchanged.
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.
invert c maps each RGB component v to 255 - v. The result is always opaque Rgb; alpha is discarded.
ANSI SGR codes
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.
emit_sgr_codes ~bg push c emits SGR codes for c via the push callback. Zero-allocation alternative to to_sgr_codes.
Binary encoding
pack c encodes c to an unboxed integer for compact storage. Uses a tagged representation (3 tag bits + data).
equal c (unpack (pack c)) holds for all c.
Note. Requires a 64-bit platform.
Converting
Formatting
pp formats a color for inspection (e.g. "Red", "Rgb(100,150,200)", "Extended(42)").