class SuperDiff::Csi::TwentyFourBitColor

Constants

LAYERS_BY_LEADING_CODE
LEADING_CODES_BY_LAYER
OPENING_REGEX
SERIAL_CODE
VALID_COMPONENT_RANGE

Attributes

triplet[R]

Public Class Methods

new(value = nil, layer: nil, red: nil, green: nil, blue: nil) click to toggle source
# 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
opening_regex() click to toggle source
# File lib/super_diff/csi/twenty_four_bit_color.rb, line 10
def self.opening_regex
  OPENING_REGEX
end

Public Instance Methods

to_foreground() click to toggle source
# 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_s() click to toggle source
# 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

Private Instance Methods

interpret_layer!(value) click to toggle source
Calls superclass method SuperDiff::Csi::Color#interpret_layer!
# File lib/super_diff/csi/twenty_four_bit_color.rb, line 83
def interpret_layer!(value)
  if value.to_s.to_i > 0
    LAYERS_BY_LEADING_CODE.fetch(value.to_s.to_i)
  else
    super
  end
end
interpret_sequence!(sequence) click to toggle source
# File lib/super_diff/csi/twenty_four_bit_color.rb, line 52
def interpret_sequence!(sequence)
  match = sequence.match(OPENING_REGEX)

  if match
    {
      layer: interpret_layer!(match[1]),
      triplet: interpret_triplet!(
        red: match[2].to_i,
        green: match[3].to_i,
        blue: match[4].to_i,
      ),
    }
  end
end
interpret_triplet!(red:, green:, blue:) click to toggle source
# File lib/super_diff/csi/twenty_four_bit_color.rb, line 67
def interpret_triplet!(red:, green:, blue:)
  if invalid_triplet?(red, green, blue)
    raise ArgumentError.new(
      "(#{red},#{green},#{blue}) is not a valid color " +
      "specification. All components must be between " +
      "#{VALID_COMPONENT_RANGE.begin} and #{VALID_COMPONENT_RANGE.end}.",
    )
  end

  Triplet.new(red: red, green: green, blue: blue)
end
invalid_triplet?(*values) click to toggle source
# File lib/super_diff/csi/twenty_four_bit_color.rb, line 79
def invalid_triplet?(*values)
  values.none? { |component| VALID_COMPONENT_RANGE.cover?(component) }
end
leading_code() click to toggle source
# File lib/super_diff/csi/twenty_four_bit_color.rb, line 48
def leading_code
  LEADING_CODES_BY_LAYER.fetch(layer)
end