Class: RubyTUI::Color
- Inherits:
-
Object
- Object
- RubyTUI::Color
- 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.
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
-
#kind ⇒ Symbol
readonly
Color kind —
:named,:indexed,:rgb, or:reset. -
#value ⇒ Integer, ...
readonly
Color value (index, [r,g,b], or named index).
Class Method Summary collapse
-
.indexed(n) ⇒ Color
Create a color from the 256-color indexed palette.
-
.rgb(r, g, b) ⇒ Color
Create an RGB truecolor.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Value equality: same kind and value.
- #hash ⇒ Integer
-
#initialize(kind, value = nil) ⇒ Color
constructor
A new instance of Color.
-
#to_bg_ansi ⇒ String
Generate ANSI background escape code fragment.
-
#to_fg_ansi ⇒ String
Generate ANSI foreground escape code fragment (without ESC[ prefix or m suffix)..
Constructor Details
#initialize(kind, value = nil) ⇒ Color
Returns a new instance of Color.
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.
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).
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.
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.
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.
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
40 |
# File 'lib/rubytui/color.rb', line 40 def hash = [@kind, @value].hash |
#to_bg_ansi ⇒ String
Generate ANSI background escape code fragment.
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).
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 |