struct
Opal::Color
- Opal::Color
- Struct
- Value
- Object
Overview
Color model supporting 16 standard ANSI colors, 256 indexed palette, and 24-bit TrueColor RGB.
Defined in:
opal/style/color.crConstructors
- .ansi(code : Int32) : Color
-
.cmyk(c : Float64, m : Float64, y : Float64, k : Float64) : Color
Constructs Color from CMYK components: C, M, Y, K in [0, 1]
-
.from(val : Color | Symbol | String) : Color
Resolves a symbol or string (e.g.
-
.hex(raw : String) : Color
Parses hex string like "#ff79c6", "61AFEF", or "#fff"
-
.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]
-
.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]
- .index(code : Int32) : Color
-
.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]
-
.lerp(c1 : Color, c2 : Color, t : Float64) : Color
Linearly interpolates between two colors by factor t (0.0 to 1.0)
- .new(type : Type, code : Int32 = 0, r : UInt8 = 0_u8, g : UInt8 = 0_u8, b : UInt8 = 0_u8)
- .none : Color
-
.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]
-
.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]
- .rgb(r : Int, g : Int, b : Int) : Color
-
.xyz(x : Float64, y : Float64, z : Float64) : Color
Constructs Color from CIE XYZ components: X, Y, Z in [0, 1]
Class Method Summary
-
.black
Named color helpers
- .blue
- .bright_black
- .bright_blue
- .bright_cyan
- .bright_green
- .bright_magenta
- .bright_red
- .bright_white
- .bright_yellow
- .cyan
- .gray
- .green
- .grey
- .magenta
-
.no_color? : Bool
Checks whether colors are suppressed via NO_COLOR standard
- .red
- .white
- .yellow
Instance Method Summary
- #b : UInt8
-
#bg_escape : String
Emits the ANSI escape code for background
- #code : Int32
-
#contrast_ratio(other : Color) : Float64
Calculates WCAG 2.1 contrast ratio against another color (range 1.0 : 1 to 21.0 : 1)
-
#darken(factor : Float64) : Color
Darkens the color toward black by factor (0.0 to 1.0)
-
#ensure_contrast(bg : Color, min_ratio : Float64 = 4.5) : Color
Adjusts luminance until contrast ratio against bg meets or exceeds min_ratio
-
#fg_escape : String
Emits the ANSI escape code for foreground
- #g : UInt8
-
#lighten(factor : Float64) : Color
Lightens the color toward white by factor (0.0 to 1.0)
- #none? : Bool
- #r : UInt8
-
#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)
-
#relative_luminance : Float64
Calculates WCAG 2.1 relative luminance (0.0 for black to 1.0 for white)
-
#to_cmyk : Tuple(Float64, Float64, Float64, Float64)
Converts Color to CMYK components: {C, M, Y, K} in [0..1]
-
#to_hex : String
Formats color as hex string "#RRGGBB"
-
#to_hsl : Tuple(Float64, Float64, Float64)
Converts Color to HSL components: {H [0..360], S [0..1], L [0..1]}
-
#to_hsv : Tuple(Float64, Float64, Float64)
Converts Color to HSV components: {H [0..360], S [0..1], V [0..1]}
-
#to_lab : Tuple(Float64, Float64, Float64)
Converts Color to CIELAB components: {L* [0..100], a* [-128..127], b* [-128..127]}
-
#to_oklab : Tuple(Float64, Float64, Float64)
Converts Color to Oklab components: {L [0..1], a [-0.4..0.4], b [-0.4..0.4]}
-
#to_oklch : Tuple(Float64, Float64, Float64)
Converts Color to Oklch components: {L [0..1], C [0..0.4], h [0..360]}
-
#to_rgb : Tuple(UInt8, UInt8, UInt8)
Converts color to RGB tuple (approximate for ANSI)
-
#to_xyz : Tuple(Float64, Float64, Float64)
Converts Color to CIE XYZ components: {X, Y, Z} in [0..1]
- #type : Type
-
#write_bg_escape(io : IO) : Nil
Writes background ANSI escape sequence directly to an IO without intermediate String allocations
-
#write_fg_escape(io : IO) : Nil
Writes foreground ANSI escape sequence directly to an IO without intermediate String allocations
Constructor Detail
Constructs Color from CMYK components: C, M, Y, K in [0, 1]
Resolves a symbol or string (e.g. :red, "#ff0000", "blue") into a Color
Parses hex string like "#ff79c6", "61AFEF", or "#fff"
Constructs Color from HSL components: h in [0, 360], s in [0, 1], l in [0, 1]
Constructs Color from HSV components: h in [0, 360], s in [0, 1], v in [0, 1]
Constructs Color from CIELAB components: L* in [0, 100], a* in [-128, 127], b* in [-128, 127]
Linearly interpolates between two colors by factor t (0.0 to 1.0)
Constructs Color from Oklab components: L in [0, 1], a in [-0.4, 0.4], b in [-0.4, 0.4]
Constructs Color from Oklch components: L in [0, 1], C in [0, 0.4], h in [0, 360]
Constructs Color from CIE XYZ components: X, Y, Z in [0, 1]
Class Method Detail
Instance Method Detail
Calculates WCAG 2.1 contrast ratio against another color (range 1.0 : 1 to 21.0 : 1)
Darkens the color toward black by factor (0.0 to 1.0)
Adjusts luminance until contrast ratio against bg meets or exceeds min_ratio
Lightens the color toward white by factor (0.0 to 1.0)
Returns true if the contrast ratio meets or exceeds the required WCAG threshold (default 4.5 for AA normal text)
Calculates WCAG 2.1 relative luminance (0.0 for black to 1.0 for white)
Converts Color to CMYK components: {C, M, Y, K} in [0..1]
Converts Color to HSL components: {H [0..360], S [0..1], L [0..1]}
Converts Color to HSV components: {H [0..360], S [0..1], V [0..1]}
Converts Color to CIELAB components: {L* [0..100], a* [-128..127], b* [-128..127]}
Converts Color to Oklab components: {L [0..1], a [-0.4..0.4], b [-0.4..0.4]}
Converts Color to Oklch components: {L [0..1], C [0..0.4], h [0..360]}
Converts Color to CIE XYZ components: {X, Y, Z} in [0..1]
Writes background ANSI escape sequence directly to an IO without intermediate String allocations
Writes foreground ANSI escape sequence directly to an IO without intermediate String allocations