struct Opal::Color

Overview

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

Defined in:

opal/style/color.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.ansi(code : Int32) : Color #

[View source]
def self.cmyk(c : Float64, m : Float64, y : Float64, k : Float64) : Color #

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


[View source]
def self.from(val : Color | Symbol | String) : Color #

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


[View source]
def self.hex(raw : String) : Color #

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


[View source]
def self.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]


[View source]
def self.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]


[View source]
def self.index(code : Int32) : Color #

[View source]
def self.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]


[View source]
def self.lerp(c1 : Color, c2 : Color, t : Float64) : Color #

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


[View source]
def self.new(type : Type, code : Int32 = 0, r : UInt8 = 0_u8, g : UInt8 = 0_u8, b : UInt8 = 0_u8) #

[View source]
def self.none : Color #

[View source]
def self.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]


[View source]
def self.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]


[View source]
def self.rgb(r : Int, g : Int, b : Int) : Color #

[View source]
def self.xyz(x : Float64, y : Float64, z : Float64) : Color #

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


[View source]

Class Method Detail

def self.black #

Named color helpers


[View source]
def self.blue #

[View source]
def self.bright_black #

[View source]
def self.bright_blue #

[View source]
def self.bright_cyan #

[View source]
def self.bright_green #

[View source]
def self.bright_magenta #

[View source]
def self.bright_red #

[View source]
def self.bright_white #

[View source]
def self.bright_yellow #

[View source]
def self.cyan #

[View source]
def self.gray #

[View source]
def self.green #

[View source]
def self.grey #

[View source]
def self.magenta #

[View source]
def self.no_color? : Bool #

Checks whether colors are suppressed via NO_COLOR standard


[View source]
def self.red #

[View source]
def self.white #

[View source]
def self.yellow #

[View source]

Instance Method Detail

def b : UInt8 #

[View source]
def bg_escape : String #

Emits the ANSI escape code for background


[View source]
def code : Int32 #

[View source]
def contrast_ratio(other : Color) : Float64 #

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


[View source]
def darken(factor : Float64) : Color #

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


[View source]
def ensure_contrast(bg : Color, min_ratio : Float64 = 4.5) : Color #

Adjusts luminance until contrast ratio against bg meets or exceeds min_ratio


[View source]
def fg_escape : String #

Emits the ANSI escape code for foreground


[View source]
def g : UInt8 #

[View source]
def lighten(factor : Float64) : Color #

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


[View source]
def none? : Bool #

[View source]
def r : UInt8 #

[View source]
def 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)


[View source]
def relative_luminance : Float64 #

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


[View source]
def to_cmyk : Tuple(Float64, Float64, Float64, Float64) #

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


[View source]
def to_hex : String #

Formats color as hex string "#RRGGBB"


[View source]
def to_hsl : Tuple(Float64, Float64, Float64) #

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


[View source]
def to_hsv : Tuple(Float64, Float64, Float64) #

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


[View source]
def to_lab : Tuple(Float64, Float64, Float64) #

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


[View source]
def to_oklab : Tuple(Float64, Float64, Float64) #

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


[View source]
def to_oklch : Tuple(Float64, Float64, Float64) #

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


[View source]
def to_rgb : Tuple(UInt8, UInt8, UInt8) #

Converts color to RGB tuple (approximate for ANSI)


[View source]
def to_xyz : Tuple(Float64, Float64, Float64) #

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


[View source]
def type : Type #

[View source]
def write_bg_escape(io : IO) : Nil #

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


[View source]
def write_fg_escape(io : IO) : Nil #

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


[View source]