Terminal.BasicANSI-16 basic color palette (codes 30-37, 90-97).
Standard 16-color palette with basic colors (black, red, green, yellow, blue, magenta, cyan, white) and their bright variants. Color names are prefixed with "basic-" to disambiguate from xterm-256 colors of the same name.
See ANSI 3-bit and 4-bit colors
Full color reference: Basic Palette table
val of_string : string -> tConvert a color name string to the palette type.
Color names are case-insensitive and use kebab-case format.
(* Look up a basic color *)
let red = Spectrum_palettes.Terminal.Xterm256.of_string "red" in
Printf.printf "Found color: red\n" (* Look up compound name *)
let color = Spectrum_palettes.Terminal.Xterm256.of_string "dark-orange" in
Printf.printf "Found: dark-orange\n" (* Handle invalid names *)
try
let _ = Spectrum_palettes.Terminal.Xterm256.of_string "not-a-color" in
()
with Spectrum_palette_ppx.Palette.InvalidColorName name ->
Printf.printf "Invalid color: %s\n" nameval to_code : t -> intGet the ANSI color code for a palette color.
Returns the terminal escape sequence code for this color.
(* Get code for red *)
let red = Spectrum_palettes.Terminal.Xterm256.of_string "red" in
let code = Spectrum_palettes.Terminal.Xterm256.to_code red in
Printf.printf "\027[38;5;%dm■\027[0m Red is code %d\n" code code (* Print all basic colors with their codes *)
List.iter (fun name ->
let color = Spectrum_palettes.Terminal.Basic.of_string name in
let code = Spectrum_palettes.Terminal.Basic.to_code color in
Printf.printf "%s: %d\n" name code
) ["basic-red"; "basic-green"; "basic-blue"]val to_color : t -> Gg.v4Convert a palette color to its RGB representation.
Returns the color as a Gg.v4 RGBA value.
(* Get RGB value for a color *)
let green = Spectrum_palettes.Terminal.Xterm256.of_string "green" in
let color = Spectrum_palettes.Terminal.Xterm256.to_color green in
let rgba = Spectrum_tools.Convert.Color.to_rgba color in
Printf.printf "Green: RGB(%d, %d, %d)\n" rgba.r rgba.g rgba.b (* Compare colors numerically *)
let red = Spectrum_palettes.Terminal.Basic.of_string "basic-red" in
let blue = Spectrum_palettes.Terminal.Basic.of_string "basic-blue" in
let red_color = Spectrum_palettes.Terminal.Basic.to_color red in
let blue_color = Spectrum_palettes.Terminal.Basic.to_color blue in
if Gg.Color.equal red_color blue_color then
Printf.printf "Colors are equal\n"
else
Printf.printf "Colors are different\n"List of all colors in the palette (in order).
Returns a list of all palette colors as Gg.v4 values.
(* Count colors in palette *)
let colors = Spectrum_palettes.Terminal.Xterm256.color_list in
Printf.printf "Xterm256 has %d colors\n" (List.length colors)
(* Output: Xterm256 has 256 colors *) (* Find average color *)
let colors = Spectrum_palettes.Terminal.Basic.color_list in
let sum_r, sum_g, sum_b, count =
List.fold_left (fun (r, g, b, n) color ->
let rgba = Spectrum_tools.Convert.Color.to_rgba' color in
(r +. rgba.r, g +. rgba.g, b +. rgba.b, n + 1)
) (0., 0., 0., 0) colors
in
let n = float_of_int count in
Printf.printf "Average: (%.2f, %.2f, %.2f)\n"
(sum_r /. n) (sum_g /. n) (sum_b /. n) (* Print all colors *)
let colors = Spectrum_palettes.Terminal.Basic.color_list in
List.iter (fun color ->
let rgba = Spectrum_tools.Convert.Color.to_rgba color in
Printf.printf "RGB(%d, %d, %d)\n" rgba.r rgba.g rgba.b
) colorsFind the nearest palette color to the given color using perceptual distance (LAB color space).
Uses octree-based spatial indexing in LAB space for efficient nearest-neighbor search. The returned color is guaranteed to be in the palette.
(* Find nearest xterm-256 color to orange *)
let orange = Gg.Color.v_srgb 1.0 0.5 0.0 in
let nearest = Spectrum_palettes.Terminal.Xterm256.nearest orange in
let rgba = Spectrum_tools.Convert.Color.to_rgba nearest in
Printf.printf "Nearest to orange: RGB(%d, %d, %d)\n"
rgba.r rgba.g rgba.b (* Quantize a gradient *)
for i = 0 to 10 do
let t = float_of_int i /. 10. in
let color = Gg.Color.v_srgb t 0.0 (1.0 -. t) in
let nearest = Spectrum_palettes.Terminal.Basic.nearest color in
let rgba = Spectrum_tools.Convert.Color.to_rgba nearest in
Printf.printf "%.1f -> RGB(%d, %d, %d)\n" t rgba.r rgba.g rgba.b
done (* Find nearest basic color *)
let custom = Gg.Color.v_srgb 0.7 0.3 0.8 in
let nearest = Spectrum_palettes.Terminal.Basic.nearest custom in
(* Result is one of the 16 basic ANSI colors *)