struct Termisu::Color

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:

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.cr

Constant 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

Instance Method Summary

Constructor Detail

def self.ansi256(index : Int32) : Color #

Creates an ANSI-256 palette color (0-255).

Can be called as:


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

Creates an ANSI-8 basic color (0-7).


[View source]
def self.black : Color #

[View source]
def self.blue : Color #

[View source]
def self.bright_black : Color #

[View source]
def self.bright_blue : Color #

[View source]
def self.bright_cyan : Color #

[View source]
def self.bright_green : Color #

[View source]
def self.bright_magenta : Color #

[View source]
def self.bright_red : Color #

[View source]
def self.bright_white : Color #

[View source]
def self.bright_yellow : Color #

[View source]
def self.cyan : Color #

[View source]
def self.default : Color #

Returns the default terminal color.


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

Creates a color from a hex string (#RRGGBB or RRGGBB).


[View source]
def self.grayscale(level : Int32) : Color #

Grayscale color from the 24-step grayscale ramp (ANSI-256 indices 232-255).

Parameters

  • level: Grayscale level from 0 (darkest) to 23 (brightest)

[View source]
def self.green : Color #

[View source]
def self.magenta : Color #

[View source]
def self.red : Color #

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

Creates an RGB/TrueColor color.

Can be called as:


[View source]
def self.white : Color #

[View source]
def self.yellow : Color #

[View source]

Instance Method Detail

def ==(other : Color) : Bool #

Equality comparison.


[View source]
def b : UInt8 #

[View source]
def default? : Bool #

Returns whether this is the default terminal color.


[View source]
def g : UInt8 #

[View source]
def index : Int32 #

ANSI color index (0-255) for ANSI8 and ANSI256 modes.


[View source]
def mode : Mode #

[View source]
def r : UInt8 #

RGB components (0-255) for RGB mode.


[View source]
def to_ansi256 : Color #

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

[View source]
def to_ansi8 : Color #

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

[View source]
def to_rgb : Color #

Converts this color to RGB mode.


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

Returns RGB components for this color.

Converts ANSI palette colors to their approximate RGB values.


[View source]
def to_s(io : IO) #

Returns a string representation of this color.


[View source]