struct
Termisu::Color
- Termisu::Color
- Struct
- Value
- Object
Overview
Terminal color support with ANSI-8, ANSI-256, and RGB/TrueColor modes.
Termisu supports three color modes:
ANSI-8 (Basic Colors)
The standard 8 ANSI colors (0-7) supported by all terminals: Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
ANSI-256 (Extended Palette)
256-color palette with:
- 0-7: Standard ANSI colors
- 8-15: Bright variants
- 16-231: 6×6×6 RGB color cube
- 232-255: 24-step grayscale ramp
RGB/TrueColor
24-bit true color with 16.7 million colors (requires terminal support).
Usage
# Basic ANSI colors
red = Color.red
blue = Color.blue
# 256-color palette
orange = Color.ansi256(208)
# RGB/TrueColor
custom = Color.rgb(255, 128, 64)
# Color conversion
ansi = custom.to_ansi256 # Convert RGB to closest 256-color
Defined in:
termisu/color.crConstant Summary
-
Black =
ansi8(0) -
Named color constants (enum-style syntax).
-
Blue =
ansi8(4) -
Cyan =
ansi8(6) -
Default =
ansi8(DEFAULT_INDEX) -
DEFAULT_INDEX =
-1 -
Special value for default terminal color.
-
Green =
ansi8(2) -
Magenta =
ansi8(5) -
Red =
ansi8(1) -
White =
ansi8(7) -
Yellow =
ansi8(3)
Constructors
-
.ansi256(index : Int32) : Color
Creates an ANSI-256 palette color (0-255).
-
.ansi8(index : Int32) : Color
Creates an ANSI-8 basic color (0-7).
- .black : Color
- .blue : Color
- .bright_black : Color
- .bright_blue : Color
- .bright_cyan : Color
- .bright_green : Color
- .bright_magenta : Color
- .bright_red : Color
- .bright_white : Color
- .bright_yellow : Color
- .cyan : Color
-
.default : Color
Returns the default terminal color.
-
.from_hex(hex : String) : Color
Creates a color from a hex string (#RRGGBB or RRGGBB).
-
.grayscale(level : Int32) : Color
Grayscale color from the 24-step grayscale ramp (ANSI-256 indices 232-255).
- .green : Color
- .magenta : Color
- .red : Color
-
.rgb(r : Int, g : Int, b : Int) : Color
Creates an RGB/TrueColor color.
- .white : Color
- .yellow : Color
Instance Method Summary
-
#==(other : Color) : Bool
Equality comparison.
- #b : UInt8
-
#default? : Bool
Returns whether this is the default terminal color.
- #g : UInt8
-
#index : Int32
ANSI color index (0-255) for ANSI8 and ANSI256 modes.
- #mode : Mode
-
#r : UInt8
RGB components (0-255) for RGB mode.
-
#to_ansi256 : Color
Converts this color to ANSI-256 palette index.
-
#to_ansi8 : Color
Converts this color to ANSI-8 (basic colors only).
-
#to_rgb : Color
Converts this color to RGB mode.
-
#to_rgb_components : Tuple(UInt8, UInt8, UInt8)
Returns RGB components for this color.
-
#to_s(io : IO)
Returns a string representation of this color.
Constructor Detail
Creates an ANSI-256 palette color (0-255).
Can be called as:
Color.ansi256(208)- method styleColor.new_ansi256(208)- alternative method style
Creates a color from a hex string (#RRGGBB or RRGGBB).
Grayscale color from the 24-step grayscale ramp (ANSI-256 indices 232-255).
Parameters
level: Grayscale level from 0 (darkest) to 23 (brightest)
Creates an RGB/TrueColor color.
Can be called as:
Color.rgb(255, 128, 64)- method styleColor.new_rgb(255, 128, 64)- alternative method style
Instance Method Detail
Converts this color to ANSI-256 palette index.
- ANSI8 colors are mapped to their corresponding ANSI-256 indices
- RGB colors are converted to the nearest 256-color palette entry
Converts this color to ANSI-8 (basic colors only).
- ANSI-256 colors are mapped to nearest ANSI-8 color
- RGB colors are converted to nearest ANSI-8 color
Returns RGB components for this color.
Converts ANSI palette colors to their approximate RGB values.