Class: RubyTUI::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytui/color.rb

Overview

Terminal color supporting named (16), indexed (256), and RGB truecolor.

Use the predefined constants for the standard 16 colors, or the factory methods for indexed and RGB colors.

Examples:

Named colors

Style.new.fg(Color::RED).bg(Color::BLACK)

256-color palette

Style.new.fg(Color.indexed(208))  # orange

RGB truecolor

Style.new.fg(Color.rgb(255, 128, 0))

Standard Named Colors (16-color palette) collapse

RESET =

Terminal default color (SGR 39 / 49).

new(:reset).freeze
BLACK =

Black (SGR 30 / 40).

new(:named, 0).freeze
RED =

Red (SGR 31 / 41).

new(:named, 1).freeze
GREEN =

Green (SGR 32 / 42).

new(:named, 2).freeze
YELLOW =

Yellow (SGR 33 / 43).

new(:named, 3).freeze
BLUE =

Blue (SGR 34 / 44).

new(:named, 4).freeze
MAGENTA =

Magenta (SGR 35 / 45).

new(:named, 5).freeze
CYAN =

Cyan (SGR 36 / 46).

new(:named, 6).freeze
WHITE =

White (SGR 37 / 47).

new(:named, 7).freeze
BRIGHT_BLACK =

Bright black, usually rendered as gray (SGR 90 / 100).

new(:named, 8).freeze
BRIGHT_RED =

Bright red (SGR 91 / 101).

new(:named, 9).freeze
BRIGHT_GREEN =

Bright green (SGR 92 / 102).

new(:named, 10).freeze
BRIGHT_YELLOW =

Bright yellow (SGR 93 / 103).

new(:named, 11).freeze
BRIGHT_BLUE =

Bright blue (SGR 94 / 104).

new(:named, 12).freeze
BRIGHT_MAGENTA =

Bright magenta (SGR 95 / 105).

new(:named, 13).freeze
BRIGHT_CYAN =

Bright cyan (SGR 96 / 106).

new(:named, 14).freeze
BRIGHT_WHITE =

Bright white (SGR 97 / 107).

new(:named, 15).freeze

Color Aliases collapse

GRAY =

Alias for BRIGHT_BLACK.

BRIGHT_BLACK
GREY =

Alias for BRIGHT_BLACK.

BRIGHT_BLACK
DARK_GRAY =

Alias for BRIGHT_BLACK.

BRIGHT_BLACK
DARK_GREY =

Alias for BRIGHT_BLACK.

BRIGHT_BLACK
LIGHT_RED =

Alias for BRIGHT_RED.

BRIGHT_RED
LIGHT_GREEN =

Alias for BRIGHT_GREEN.

BRIGHT_GREEN
LIGHT_YELLOW =

Alias for BRIGHT_YELLOW.

BRIGHT_YELLOW
LIGHT_BLUE =

Alias for BRIGHT_BLUE.

BRIGHT_BLUE
LIGHT_MAGENTA =

Alias for BRIGHT_MAGENTA.

BRIGHT_MAGENTA
LIGHT_CYAN =

Alias for BRIGHT_CYAN.

BRIGHT_CYAN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, value = nil) ⇒ Color

Returns a new instance of Color.

Parameters:

  • kind (Symbol) —

    color kind

  • value (Integer, Array<Integer>, nil) (defaults to: nil) —

    color value



25
26
27
28
# File 'lib/rubytui/color.rb', line 25

def initialize(kind, value = nil)
  @kind = kind
  @value = value
end

Instance Attribute Details

#kind ⇒ Symbol (readonly)

Returns color kind — :named, :indexed, :rgb, or :reset.

Returns:

  • (Symbol) —

    color kind — :named, :indexed, :rgb, or :reset



19
20
21
# File 'lib/rubytui/color.rb', line 19

def kind
  @kind
end

#value ⇒ Integer, ... (readonly)

Returns color value (index, [r,g,b], or named index).

Returns:

  • (Integer, Array<Integer>, nil) —

    color value (index, [r,g,b], or named index)



21
22
23
# File 'lib/rubytui/color.rb', line 21

def value
  @value
end

Class Method Details

.indexed(n) ⇒ Color

Create a color from the 256-color indexed palette.

Parameters:

  • n (Integer) —

    color index (0-255)

Returns:

Raises:

  • (ArgumentError) —

    if n is outside 0-255



48
49
50
51
52
# File 'lib/rubytui/color.rb', line 48

def indexed(n)
  raise ArgumentError, "Color index must be 0-255, got #{n}" unless (0..255).cover?(n)

  new(:indexed, n).freeze
end

.rgb(r, g, b) ⇒ Color

Create an RGB truecolor.

Parameters:

  • r (Integer) —

    red component (0-255)

  • g (Integer) —

    green component (0-255)

  • b (Integer) —

    blue component (0-255)

Returns:



60
61
62
# File 'lib/rubytui/color.rb', line 60

def rgb(r, g, b)
  new(:rgb, [r, g, b].freeze).freeze
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Value equality: same kind and value.

Parameters:

  • other (Object) —

    object to compare

Returns:

  • (Boolean) —

    true if other is a Color with equal #kind and #value



34
35
36
# File 'lib/rubytui/color.rb', line 34

def ==(other)
  other.is_a?(Color) && @kind == other.kind && @value == other.value
end

#hash ⇒ Integer

Returns hash derived from #kind and #value.

Returns:



40
# File 'lib/rubytui/color.rb', line 40

def hash = [@kind, @value].hash

#to_bg_ansi ⇒ String

Generate ANSI background escape code fragment.

Returns:

  • (String) —

    SGR code fragment (e.g., "41", "48;5;208", "48;2;255;128;0")



80
81
82
83
84
85
86
87
# File 'lib/rubytui/color.rb', line 80

def to_bg_ansi
  case @kind
  when :reset   then "49"
  when :named   then (@value < 8 ? "#{40 + @value}" : "#{100 + @value - 8}")
  when :indexed then "48;5;#{@value}"
  when :rgb     then "48;2;#{@value[0]};#{@value[1]};#{@value[2]}"
  end
end

#to_fg_ansi ⇒ String

Generate ANSI foreground escape code fragment (without ESC[ prefix or m suffix).

Returns:

  • (String) —

    SGR code fragment (e.g., "31", "38;5;208", "38;2;255;128;0")



68
69
70
71
72
73
74
75
# File 'lib/rubytui/color.rb', line 68

def to_fg_ansi
  case @kind
  when :reset   then "39"
  when :named   then (@value < 8 ? "#{30 + @value}" : "#{90 + @value - 8}")
  when :indexed then "38;5;#{@value}"
  when :rgb     then "38;2;#{@value[0]};#{@value[1]};#{@value[2]}"
  end
end