Class: Vedeu::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/output/translator.rb

Overview

TODO:

More documentation required (create a fancy chart!)

Convert a CSS/HTML colour string into a terminal escape sequence.

If provided with an empty value or a string it cannot convert, it will return an empty string.

When provided with a named colour, uses the terminal’s value for that colour. If a theme is being used with the terminal, which overrides the defaults, then the theme’s colour will be used. The recognised names are: :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, :default.

When a number between 0 and 255 is provided, Vedeu will use the terminal colour corresponding with that colour.

Finally, when provided a CSS/HTML colour string e.g. ‘#ff0000’, Vedeu will translate that to the 8-bit escape sequence or if you have a capable terminal and the ‘VEDEU_TERM=xterm-truecolor` environment variable set, a 24-bit representation.

Direct Known Subclasses

Background, Foreground

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(colour = '') ⇒ Translator

Return a new instance of Vedeu::Translator.

Parameters:

  • colour (Fixnum|String|Symbol) (defaults to: '')


59
60
61
# File 'lib/vedeu/output/translator.rb', line 59

def initialize(colour = '')
  @colour = colour
end

Instance Attribute Details

#colourString (readonly) Also known as: value

Returns:

  • (String)


26
27
28
# File 'lib/vedeu/output/translator.rb', line 26

def colour
  @colour
end

Class Method Details

.coerce(value) ⇒ Object

Produces new objects of the correct class from the value, ignores objects that have already been coerced.

Parameters:

  • value (Object|NilClass)

Returns:

  • (Object)


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vedeu/output/translator.rb', line 34

def self.coerce(value)
  if value.nil?
    ''

  elsif value.is_a?(self)
    value

  else
    new(value)

  end
end

.escape_sequence(colour = '') ⇒ String

Convert a CSS/HTML colour string into a terminal escape sequence.

Parameters:

  • colour (Fixnum|String|Symbol) (defaults to: '')

Returns:

  • (String)


51
52
53
# File 'lib/vedeu/output/translator.rb', line 51

def self.escape_sequence(colour = '')
  new(colour).escape_sequence
end

Instance Method Details

#blueFixnum (private)

Takes the blue component of #css_to_rgb and converts to the correct value for setting the terminal blue value.

Returns:

  • (Fixnum)


256
257
258
# File 'lib/vedeu/output/translator.rb', line 256

def blue
  (css_to_rgb[2] / 51) * 1
end

#css_to_numberedFixnum (private)

Returns:

  • (Fixnum)


226
227
228
229
230
231
232
233
234
# File 'lib/vedeu/output/translator.rb', line 226

def css_to_numbered
  if rgb?
    [16, red, green, blue].inject(:+)

  elsif numbered?
    colour

  end
end

#css_to_rgbArray (private)

Returns a collection of converted HTML/CSS octets as their decimal equivalents.

Examples:

colour = '#aadd55'
css_to_rgb # => [170, 221, 85]

Returns:

  • (Array)


217
218
219
220
221
222
223
# File 'lib/vedeu/output/translator.rb', line 217

def css_to_rgb
  [
    colour[1..2].to_i(16),
    colour[3..4].to_i(16),
    colour[5..6].to_i(16),
  ]
end

#eql?(other) ⇒ Boolean Also known as: ==

An object is equal when its values are the same.

Parameters:

Returns:

  • (Boolean)


67
68
69
# File 'lib/vedeu/output/translator.rb', line 67

def eql?(other)
  self.class == other.class && colour == other.colour
end

#escape_sequenceString Also known as: to_s

Returns:

  • (String)

See Also:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vedeu/output/translator.rb', line 74

def escape_sequence
  if no_colour?
    ''

  elsif registered?(colour)
    retrieve(colour)

  elsif rgb?
    rgb

  elsif numbered?
    numbered

  elsif named?
    named

  else
    ''

  end
end

#greenFixnum (private)

Takes the green component of #css_to_rgb and converts to the correct value for setting the terminal green value.

Returns:

  • (Fixnum)


248
249
250
# File 'lib/vedeu/output/translator.rb', line 248

def green
  (css_to_rgb[1] / 51) * 6
end

#namedString (private)

Note:

Valid names can be found at Esc#codes

Returns an escape sequence for a named background colour.

Returns:

  • (String)


152
153
154
# File 'lib/vedeu/output/translator.rb', line 152

def named
  ["\e[", named_codes, 'm'].join
end

#named?Boolean (private)

Returns:

  • (Boolean)


142
143
144
# File 'lib/vedeu/output/translator.rb', line 142

def named?
  colour.is_a?(Symbol) && valid_name?
end

#no_colour?Boolean (private)

Returns:

  • (Boolean)


137
138
139
# File 'lib/vedeu/output/translator.rb', line 137

def no_colour?
  colour.nil? || colour.to_s.empty?
end

#not_implementedNotImplemented (private) Also known as: named_codes, numbered_prefix, repository, rgb_prefix

Returns:

  • (NotImplemented)

Raises:

  • (NotImplemented)

    Subclasses of this class must implement this method.



263
264
265
# File 'lib/vedeu/output/translator.rb', line 263

def not_implemented
  fail NotImplemented, 'Subclasses implement this.'
end

#numberedString (private)

Returns an escape sequence.

Returns:

  • (String)


175
176
177
# File 'lib/vedeu/output/translator.rb', line 175

def numbered
  [numbered_prefix, css_to_numbered, 'm'].join
end

#numbered?Boolean (private)

Returns a boolean indicating whether the colour provided is a terminal numbered colour.

Returns:

  • (Boolean)


168
169
170
# File 'lib/vedeu/output/translator.rb', line 168

def numbered?
  colour.is_a?(Fixnum) && valid_range?
end

#redFixnum (private)

Takes the red component of #css_to_rgb and converts to the correct value for setting the terminal red value.

Returns:

  • (Fixnum)


240
241
242
# File 'lib/vedeu/output/translator.rb', line 240

def red
  (css_to_rgb[0] / 51) * 36
end

#register(colour, escape_sequence) ⇒ String (private)

Registers a HTML/CSS colour code and escape sequence to reduce processing.

Parameters:

  • colour (String)

    A HTML/CSS colour code.

  • escape_sequence (String)

    The HTML/CSS colour code as an escape sequence.

Returns:

  • (String)


124
125
126
# File 'lib/vedeu/output/translator.rb', line 124

def register(colour, escape_sequence)
  repository.register(colour, escape_sequence)
end

#registered?(colour) ⇒ Boolean (private)

Returns a boolean indicating the HTML/CSS colour code has been registered.

Parameters:

  • colour (String)

Returns:

  • (Boolean)


132
133
134
# File 'lib/vedeu/output/translator.rb', line 132

def registered?(colour)
  repository.registered?(colour)
end

#retrieve(colour) ⇒ String (private)

Retrieves the escape sequence for the HTML/CSS colour code.

Parameters:

  • colour (String)

Returns:

  • (String)


114
115
116
# File 'lib/vedeu/output/translator.rb', line 114

def retrieve(colour)
  repository.retrieve(colour)
end

#rgbString (private)

Returns an escape sequence.

Returns:

  • (String)


191
192
193
194
195
196
197
198
199
# File 'lib/vedeu/output/translator.rb', line 191

def rgb
  if Vedeu::Configuration.colour_mode == 16_777_216
    register(colour, format(rgb_prefix, *css_to_rgb))

  else
    numbered

  end
end

#rgb?Boolean (private)

Returns a boolean indicated whether the colour is a valid HTML/CSS colour.

Returns:

  • (Boolean)


182
183
184
185
186
# File 'lib/vedeu/output/translator.rb', line 182

def rgb?
  return true if colour =~ /^#([A-Fa-f0-9]{6})$/

  false
end

#to_htmlString

Returns:

  • (String)


98
99
100
101
102
103
104
105
106
# File 'lib/vedeu/output/translator.rb', line 98

def to_html
  if rgb?
    colour

  else
    ''

  end
end

#valid_name?Boolean (private)

Returns a boolean indicating whether the colour provided is a valid named colour.

Returns:

  • (Boolean)


160
161
162
# File 'lib/vedeu/output/translator.rb', line 160

def valid_name?
  Vedeu::Esc.codes.keys.include?(colour)
end

#valid_range?Boolean (private)

Returns a boolean indicating whether the numbered colour is within the range of valid terminal numbered colours.

Returns:

  • (Boolean)


205
206
207
# File 'lib/vedeu/output/translator.rb', line 205

def valid_range?
  colour >= 0 && colour <= 255
end