Class: Unmagic::Color::RGB::ANSI

Inherits:
Object
  • Object
show all
Defined in:
lib/unmagic/color/rgb/ansi.rb

Overview

ANSI SGR (Select Graphic Rendition) color code parsing.

Parses ANSI color codes used in terminal output. Handles standard 3/4-bit colors, 256-color palette, and 24-bit true color formats.

## Supported Formats

Standard 3/4-bit colors (foreground and background):

Unmagic::Color::RGB::ANSI.parse("31")        # Red foreground
Unmagic::Color::RGB::ANSI.parse("41")        # Red background
Unmagic::Color::RGB::ANSI.parse("91")        # Bright red foreground
Unmagic::Color::RGB::ANSI.parse("101")       # Bright red background

256-color palette:

Unmagic::Color::RGB::ANSI.parse("38;5;196")  # Red foreground (256-color)
Unmagic::Color::RGB::ANSI.parse("48;5;196")  # Red background (256-color)

24-bit true color:

Unmagic::Color::RGB::ANSI.parse("38;2;255;0;0")    # Red foreground (true color)
Unmagic::Color::RGB::ANSI.parse("48;2;255;0;0")    # Red background (true color)

Examples:

Parse standard ANSI color

Unmagic::Color::RGB::ANSI.parse("31")
#=> RGB instance for red

Parse 24-bit true color

Unmagic::Color::RGB::ANSI.parse("38;2;100;150;200")
#=> RGB instance for RGB(100, 150, 200)

Defined Under Namespace

Classes: ParseError

Class Method Summary collapse

Class Method Details

.parse(input) ⇒ RGB

Parse an ANSI SGR color code.

Accepts SGR parameters only (not full escape sequences). Handles foreground and background colors in all formats. Integers are automatically converted to strings.

Examples:

Parse standard ANSI color with string

Unmagic::Color::RGB::ANSI.parse("31")
#=> Unmagic::Color::RGB instance for red

Parse standard ANSI color with integer

Unmagic::Color::RGB::ANSI.parse(31)
#=> Unmagic::Color::RGB instance for red

Parse 256-color palette

Unmagic::Color::RGB::ANSI.parse("38;5;196")
#=> Unmagic::Color::RGB instance for bright red

Parse 24-bit true color

color = Unmagic::Color::RGB::ANSI.parse("38;2;100;150;200")
color.to_hex
#=> "#6496c8"

Parameters:

  • input (String, Integer)

    The ANSI code to parse

Returns:

  • (RGB)

    The parsed RGB color

Raises:

  • (ParseError)

    If the input is not a valid ANSI color code



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/unmagic/color/rgb/ansi.rb', line 91

def parse(input)
  raise ParseError, "Input must be a string or integer" unless input.is_a?(::String) || input.is_a?(::Integer)

  # Convert integers to strings
  input = input.to_s if input.is_a?(::Integer)

  # Strip and validate format
  clean = input.strip
  raise ParseError, "Can't parse empty string" if clean.empty?

  # Must be numeric with optional semicolons
  unless clean.match?(/\A\d+(?:;\d+)*\z/)
    raise ParseError, "Invalid ANSI format: #{input.inspect} (must be numeric with optional semicolons)"
  end

  # Split on semicolons
  parts = clean.split(";").map(&:to_i)

  parse_sgr_params(parts)
end

.valid?(value) ⇒ Boolean

Check if a string or integer is a valid ANSI color code.

Examples:

Check string

Unmagic::Color::RGB::ANSI.valid?("31")
#=> true

Check integer

Unmagic::Color::RGB::ANSI.valid?(31)
#=> true

Invalid input

Unmagic::Color::RGB::ANSI.valid?("invalid")
#=> false

Parameters:

  • value (String, Integer)

    The value to validate

Returns:

  • (Boolean)

    true if valid ANSI code, false otherwise



58
59
60
61
62
63
# File 'lib/unmagic/color/rgb/ansi.rb', line 58

def valid?(value)
  parse(value)
  true
rescue ParseError
  false
end