Class: SuperDiff::Csi::TwentyFourBitColor

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

Defined Under Namespace

Classes: Triplet

Constant Summary collapse

VALID_COMPONENT_RANGE =
(0..255).freeze
LEADING_CODES_BY_LAYER =
{ foreground: 38, background: 48 }.freeze
LAYERS_BY_LEADING_CODE =
LEADING_CODES_BY_LAYER.invert.freeze
SERIAL_CODE =
2
OPENING_REGEX =
/\e\[(\d+);#{SERIAL_CODE};(\d+);(\d+);(\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) ⇒ TwentyFourBitColor

Returns a new instance of TwentyFourBitColor.



14
15
16
17
18
19
20
21
22
23
# File 'lib/super_diff/csi/twenty_four_bit_color.rb', line 14

def initialize(value = nil, layer: nil, red: nil, green: nil, blue: nil)
  if value
    pair = interpret_sequence!(value)
    @triplet = pair.fetch(:triplet)
    @layer = pair.fetch(:layer)
  else
    @triplet = interpret_triplet!(red: red, green: green, blue: blue)
    @layer = interpret_layer!(layer)
  end
end

Class Method Details

.opening_regexObject



10
11
12
# File 'lib/super_diff/csi/twenty_four_bit_color.rb', line 10

def self.opening_regex
  OPENING_REGEX
end

Instance Method Details

#to_foregroundObject



35
36
37
38
39
40
41
42
# File 'lib/super_diff/csi/twenty_four_bit_color.rb', line 35

def to_foreground
  self.class.new(
    red: triplet.red,
    green: triplet.green,
    blue: triplet.blue,
    layer: :foreground,
  )
end

#to_sObject



25
26
27
28
29
30
31
32
33
# File 'lib/super_diff/csi/twenty_four_bit_color.rb', line 25

def to_s
  [
    "\e[#{leading_code}",
    SERIAL_CODE,
    triplet.red,
    triplet.blue,
    triplet.green,
  ].join(";") + "m"
end