Color API
Types
RgbColor
zig
pub const RgbColor = struct {
r: u8,
g: u8,
b: u8,
};HexColor
zig
pub const HexColor = struct {
value: u24,
};Ansi256Color
zig
pub const Ansi256Color = struct {
index: u8,
};HslColor
zig
pub const HslColor = struct {
h: u16, // 0-359
s: u8, // 0-100
l: u8, // 0-100
};HsvColor
zig
pub const HsvColor = struct {
h: u16, // 0-359
s: u8, // 0-100
v: u8, // 0-100
};CmykColor
zig
pub const CmykColor = struct {
c: u8, // 0-100
m: u8, // 0-100
y: u8, // 0-100
k: u8, // 0-100
};XyzColor
zig
pub const XyzColor = struct {
x: f64,
y: f64,
z: f64,
};LabColor
zig
pub const LabColor = struct {
l: f64, // Lightness
a: f64, // Green-red axis
b_val: f64, // Blue-yellow axis
};Ansi4
zig
pub const Ansi4 = enum {
black, red, green, yellow, blue, magenta, cyan, white,
bright_black, bright_red, bright_green, bright_yellow,
bright_blue, bright_magenta, bright_cyan, bright_white,
default,
};Color
zig
pub const Color = union(enum) {
ansi4: Ansi4,
ansi256: Ansi256Color,
rgb: RgbColor,
hex: HexColor,
hsl: HslColor,
hsv: HsvColor,
};Functions
RgbColor.init
zig
pub fn init(r: u8, g: u8, b: u8) RgbColorHexColor.init
zig
pub fn init(hex_string: []const u8) !HexColorParses "#FF0000", "FF0000", "#F00", or "F00".
HexColor.fromInt
zig
pub fn fromInt(value: u24) HexColorAnsi256Color.init
zig
pub fn init(index: u8) Ansi256ColorHslColor.init
zig
pub fn init(h: u16, s: u8, l: u8) HslColorWraps hue at 360.
HsvColor.init
zig
pub fn init(h: u16, s: u8, v: u8) HsvColorWraps hue at 360.
CmykColor.init
zig
pub fn init(c: u8, m: u8, y: u8, k: u8) CmykColorCmykColor.fromRgb
zig
pub fn fromRgb(rgb: RgbColor) CmykColorXyzColor.init
zig
pub fn init(x: f64, y: f64, z: f64) XyzColorXyzColor.fromRgb
zig
pub fn fromRgb(rgb: RgbColor) XyzColorLabColor.init
zig
pub fn init(l: f64, a: f64, b_val: f64) LabColorLabColor.fromXyz
zig
pub fn fromXyz(xyz: XyzColor) LabColorLabColor.distance
zig
pub fn distance(self: LabColor, other: LabColor) f64CIE76 color distance.
Color Methods
toFg / toBg / toUnderline
zig
pub fn toFg(self: Color) []const u8
pub fn toBg(self: Color) []const u8
pub fn toUnderline(self: Color) []const u8toRgb / toHex / toHsl / toHsv / toCmyk / toXyz / toLab
zig
pub fn toRgb(self: Color) RgbColor
pub fn toHex(self: Color) u24
pub fn toHsl(self: Color) HslColor
pub fn toHsv(self: Color) HsvColor
pub fn toCmyk(self: Color) CmykColor
pub fn toXyz(self: Color) XyzColor
pub fn toLab(self: Color) LabColorluminance
zig
pub fn luminance(self: Color) f64Relative luminance (0.0-1.0).
lighten / darken / saturate / desaturate
zig
pub fn lighten(self: Color, amount: f64) Color
pub fn darken(self: Color, amount: f64) Color
pub fn saturate(self: Color, amount: f64) Color
pub fn desaturate(self: Color, amount: f64) Colorinvert / grayscale / mix
zig
pub fn invert(self: Color) Color
pub fn grayscale(self: Color) Color
pub fn mix(self: Color, other: Color, ratio: f64) Colorrotate / adjustHue
zig
pub fn rotate(self: Color, degrees: u16) Color
pub fn adjustHue(self: Color, degrees: i32) Colorcomplementary / analogous / triadic / splitComplementary / tetradic
zig
pub fn complementary(self: Color) Color
pub fn analogous(self: Color) [2]Color
pub fn triadic(self: Color) [2]Color
pub fn splitComplementary(self: Color) [2]Color
pub fn tetradic(self: Color) [3]ColorisLight / isDark
zig
pub fn isLight(self: Color) bool
pub fn isDark(self: Color) boolcolorDistance / contrastRatio
zig
pub fn colorDistance(self: Color, other: Color) f64
pub fn contrastRatio(self: Color, other: Color) f64lerp
zig
pub fn lerp(c1: Color, c2: Color, t: f64) ColornearestAnsi256
zig
pub fn nearestAnsi256(self: Color) u8kelvin / temperatureToRgb
zig
pub fn kelvin(temp: u16) Color
pub fn temperatureToRgb(temp: u16) RgbColorColor temperature in Kelvin (1000-40000).
fade / blend
zig
pub fn fade(self: Color, amount: f64) Color
pub fn blend(self: Color, other: Color, ratio: f64) ColorFade a color toward gray (0.0 = gray, 1.0 = original). Blend is an alias for mix.
saturateTo / lightenTo
zig
pub fn saturateTo(self: Color, target: u8) Color
pub fn lightenTo(self: Color, target: u8) ColorSet saturation or lightness to a specific value (0-100).
grayscaleLuminance / mixHsl
zig
pub fn grayscaleLuminance(self: Color) Color
pub fn mixHsl(self: Color, other: Color, ratio: f64) ColorGrayscale using perceptual luminance. Mix two colors in HSL space.
monochromatic
zig
pub fn monochromatic(self: Color, count: u8) [8]ColorReturns up to 8 monochromatic variations of the color (varying lightness).
