struct

Opal::Color

Inherits Struct < Value < Object

Color model supporting 16 standard ANSI colors, 256 indexed palette, and 24-bit TrueColor RGB.

Constructors

ansi(code : Int32) : Color
Source
cmyk(c : Float64, m : Float64, y : Float64, k : Float64) : Color

Constructs Color from CMYK components: C, M, Y, K in [0, 1]

Source
from(val : Color | Symbol | String) : Color

Resolves a symbol or string (e.g. :red, "#ff0000", "blue") into a Color

Source
hex(raw : String) : Color

Parses hex string like "#ff79c6", "61AFEF", or "#fff"

Source
hsl(h : Float64, s : Float64, l : Float64) : Color

Constructs Color from HSL components: h in [0, 360], s in [0, 1], l in [0, 1]

Source
hsv(h : Float64, s : Float64, v : Float64) : Color

Constructs Color from HSV components: h in [0, 360], s in [0, 1], v in [0, 1]

Source
index(code : Int32) : Color
Source
lab(l : Float64, a : Float64, b : Float64) : Color

Constructs Color from CIELAB components: L* in [0, 100], a* in [-128, 127], b* in [-128, 127]

Source
lerp(c1 : Color, c2 : Color, t : Float64) : Color

Linearly interpolates between two colors by factor t (0.0 to 1.0)

Source
new(type : Type, code : Int32 = 0, r : UInt8 = 0_u8, g : UInt8 = 0_u8, b : UInt8 = 0_u8)
Source
none
Source
oklab(l : Float64, a : Float64, b : Float64) : Color

Constructs Color from Oklab components: L in [0, 1], a in [-0.4, 0.4], b in [-0.4, 0.4]

Source
oklch(l : Float64, c : Float64, h : Float64) : Color

Constructs Color from Oklch components: L in [0, 1], C in [0, 0.4], h in [0, 360]

Source
rgb(r : Int, g : Int, b : Int) : Color
Source
xyz(x : Float64, y : Float64, z : Float64) : Color

Constructs Color from CIE XYZ components: X, Y, Z in [0, 1]

Source

Class methods

black

Named color helpers

Source
blue
Source
bright_black
Source
bright_blue
Source
bright_cyan
Source
bright_green
Source
bright_magenta
Source
bright_red
Source
bright_white
Source
bright_yellow
Source
cyan
Source
gray
Source
green
Source
grey
Source
magenta
Source
no_color?

Checks whether colors are suppressed via NO_COLOR standard

Source
white
Source
yellow
Source

Instance methods

bg_escape

Emits the ANSI escape code for background

Source
code
Source
contrast_ratio(other : Color) : Float64

Calculates WCAG 2.1 contrast ratio against another color (range 1.0 : 1 to 21.0 : 1)

Source
darken(factor : Float64) : Color

Darkens the color toward black by factor (0.0 to 1.0)

Source
ensure_contrast(bg : Color, min_ratio : Float64 = 4.5) : Color

Adjusts luminance until contrast ratio against bg meets or exceeds min_ratio

Source
fg_escape

Emits the ANSI escape code for foreground

Source
lighten(factor : Float64) : Color

Lightens the color toward white by factor (0.0 to 1.0)

Source
none?
Source
readable_against?(bg : Color, min_ratio : Float64 = 4.5) : Bool

Returns true if the contrast ratio meets or exceeds the required WCAG threshold (default 4.5 for AA normal text)

Source
relative_luminance

Calculates WCAG 2.1 relative luminance (0.0 for black to 1.0 for white)

Source
to_cmyk

Converts Color to CMYK components: {C, M, Y, K} in [0..1]

Source
to_hex

Formats color as hex string "#RRGGBB"

Source
to_hsl

Converts Color to HSL components: {H [0..360], S [0..1], L [0..1]}

Source
to_hsv

Converts Color to HSV components: {H [0..360], S [0..1], V [0..1]}

Source
to_lab

Converts Color to CIELAB components: {L* [0..100], a* [-128..127], b* [-128..127]}

Source
to_oklab

Converts Color to Oklab components: {L [0..1], a [-0.4..0.4], b [-0.4..0.4]}

Source
to_oklch

Converts Color to Oklch components: {L [0..1], C [0..0.4], h [0..360]}

Source
to_rgb

Converts color to RGB tuple (approximate for ANSI)

Source
to_xyz

Converts Color to CIE XYZ components: {X, Y, Z} in [0..1]

Source
type
Source
write_bg_escape(io : IO) : Nil

Writes background ANSI escape sequence directly to an IO without intermediate String allocations

Source
write_fg_escape(io : IO) : Nil

Writes foreground ANSI escape sequence directly to an IO without intermediate String allocations

Source

Nested types