Class: Subconv::Scc::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/subconv/scc/reader.rb

Overview

Color constants as immutable value objects with some convenience functions (e.g. conversion to string or symbol) All available colors are registered as constants in this class, e.g. Color::WHITE, Color::RED and so on The instances of this class are all frozen and can never be changed. Instances can be retrieved only by the constants or with ::for_value

Constant Summary collapse

COLORS =

rubocop:disable MutableConstant

{}
TO_SYMBOL_MAP =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color) ⇒ Color

Returns a new instance of Color.



73
74
75
# File 'lib/subconv/scc/reader.rb', line 73

def initialize(color)
  @color = color
end

Class Method Details

.for_value(value) ⇒ Object

Get the Color instance corresponding to a CEA608 color code



123
124
125
126
127
128
# File 'lib/subconv/scc/reader.rb', line 123

def self.for_value(value)
  color = COLORS[value]
  fail "Color value #{value} is unknown" if color.nil?

  color
end

.register_color(name, value) ⇒ Object

rubocop:enable MutableConstant



94
95
96
97
98
99
100
101
102
# File 'lib/subconv/scc/reader.rb', line 94

def self.register_color(name, value)
  # Make sure the new color is immutable
  new_color = Color.new(value).freeze
  # Register in lookup tables
  COLORS[value] = new_color
  TO_SYMBOL_MAP[value] = name
  # Register as class constant
  const_set(name.to_s.upcase, new_color)
end

Instance Method Details

#to_sObject Also known as: inspect

Lower-case CEA-608 name of the color



84
85
86
# File 'lib/subconv/scc/reader.rb', line 84

def to_s
  to_symbol.to_s
end

#to_symbolObject

Lower-case CEA608 name of the color as symbol



118
119
120
# File 'lib/subconv/scc/reader.rb', line 118

def to_symbol
  TO_SYMBOL_MAP[@color]
end

#valueObject Also known as: to_i

CEA-608 color code



78
79
80
# File 'lib/subconv/scc/reader.rb', line 78

def value
  @color
end