Class: SuperDiff::Csi::EightBitColor

Inherits:
Color
  • Object
show all
Defined in:
lib/super_diff/csi/eight_bit_color.rb

Constant Summary collapse

VALID_COMPONENT_RANGE =
(0..6).freeze
VALID_CODE_RANGE =
(0..255).freeze
VALID_CODES_BY_NAME =
{
  black: 0,
  red: 1,
  green: 2,
  yellow: 3,
  blue: 4,
  magenta: 5,
  cyan: 6,
  white: 7,
  bright_black: 8,
  bright_red: 9,
  bright_green: 10,
  bright_yellow: 11,
  bright_blue: 12,
  bright_magenta: 13,
  bright_cyan: 14,
  bright_white: 15
}.freeze
STARTING_INDICES =
{
  standard: 0,
  high_intensity: 8,
  grayscale: 232
}.freeze
VALID_PAIR_TYPES =
STARTING_INDICES.keys
VALID_PAIR_INDEX_RANGES =
{
  standard: 0..7,
  high_intensity: 0..7,
  grayscale: 0..23
}.freeze
LEADING_CODES_BY_LAYER =
{ foreground: 38, background: 48 }.freeze
LAYERS_BY_LEADING_CODE =
LEADING_CODES_BY_LAYER.invert.freeze
SERIAL_CODE =
5
OPENING_REGEX =
/\e\[(\d+);#{SERIAL_CODE};(\d+)m/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Color

#background?, exists?, #foreground?, resolve, sub_colorized_areas_in

Constructor Details

#initialize(value = nil, layer: nil, red: nil, green: nil, blue: nil, type: nil, index: nil) ⇒ EightBitColor

Returns a new instance of EightBitColor.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/super_diff/csi/eight_bit_color.rb', line 44

def initialize(
  value = nil,
  layer: nil,
  red: nil,
  green: nil,
  blue: nil,
  type: nil,
  index: nil
)
  if value
    case value
    when Symbol
      @code = interpret_color_name!(value)
      @layer = interpret_layer!(layer)
    when Array
      @code = interpret_pair!(type: type, index: index)
      @layer = interpret_layer!(layer)
    else
      if value.is_a?(String) && value.start_with?("\e[")
        pair = interpret_sequence!(value)
        @code = pair.fetch(:code)
        @layer = pair.fetch(:layer)
      else
        @code = interpret_code!(value)
        @layer = interpret_layer!(layer)
      end
    end
  else
    @code = interpret_triplet!(red: red, green: green, blue: blue)
    @layer = interpret_layer!(layer)
  end
end

Class Method Details

.opening_regexObject



40
41
42
# File 'lib/super_diff/csi/eight_bit_color.rb', line 40

def self.opening_regex
  OPENING_REGEX
end

Instance Method Details

#to_foregroundObject



81
82
83
# File 'lib/super_diff/csi/eight_bit_color.rb', line 81

def to_foreground
  self.class.new(code, layer: :foreground)
end

#to_sObject



77
78
79
# File 'lib/super_diff/csi/eight_bit_color.rb', line 77

def to_s
  "\e[#{leading_code};#{SERIAL_CODE};#{code}m"
end