Class: Vedeu::Translator

Inherits:
Object
  • Object
show all
Includes:
Coercions
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

Methods included from Coercions

#coerce, #coercer, included

Methods included from Common

#defined_value?, #to_sentence

Constructor Details

#initialize(colour = '') ⇒ Translator

Return a new instance of Translator.

Parameters:

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


43
44
45
# File 'lib/vedeu/output/translator.rb', line 43

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

Instance Attribute Details

#colourObject (readonly) Also known as: value

Returns the value of attribute colour.



28
29
30
# File 'lib/vedeu/output/translator.rb', line 28

def colour
  @colour
end

Class Method Details

.escape_sequence(colour = '') ⇒ String

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

Parameters:

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

Returns:

  • (String)


35
36
37
# File 'lib/vedeu/output/translator.rb', line 35

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:



207
208
209
# File 'lib/vedeu/output/translator.rb', line 207

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

#css_to_numberedFixnum (private)

Returns:



177
178
179
180
181
182
183
184
185
# File 'lib/vedeu/output/translator.rb', line 177

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)


168
169
170
171
172
173
174
# File 'lib/vedeu/output/translator.rb', line 168

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

#escape_sequenceString Also known as: to_s

Returns:

  • (String)

See Also:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vedeu/output/translator.rb', line 49

def escape_sequence
  if no_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:



199
200
201
# File 'lib/vedeu/output/translator.rb', line 199

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)


98
99
100
# File 'lib/vedeu/output/translator.rb', line 98

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

#named?Boolean (private)

Returns:

  • (Boolean)


88
89
90
# File 'lib/vedeu/output/translator.rb', line 88

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

#named_codesException (private)

Returns:

  • (Exception)

Raises:

  • (NotImplemented)

    Subclasses of this class must implement this method.



214
215
216
# File 'lib/vedeu/output/translator.rb', line 214

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

#no_colour?Boolean (private)

Returns:

  • (Boolean)


83
84
85
# File 'lib/vedeu/output/translator.rb', line 83

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

#numberedString (private)

Returns an escape sequence.

Returns:

  • (String)


121
122
123
# File 'lib/vedeu/output/translator.rb', line 121

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)


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

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

#numbered_prefixNotImplemented (private)

Returns:

  • (NotImplemented)

Raises:

  • (NotImplemented)

    Subclasses of this class must implement this method.



221
222
223
# File 'lib/vedeu/output/translator.rb', line 221

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

#redFixnum (private)

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

Returns:



191
192
193
# File 'lib/vedeu/output/translator.rb', line 191

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

#rgbString (private)

Returns an escape sequence.

Returns:

  • (String)


142
143
144
145
146
147
148
149
150
# File 'lib/vedeu/output/translator.rb', line 142

def rgb
  if Configuration.colour_mode == 16777216
    sprintf(rgb_prefix, *css_to_rgb)

  else
    numbered

  end
end

#rgb?Boolean (private)

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

Returns:

  • (Boolean)


128
129
130
# File 'lib/vedeu/output/translator.rb', line 128

def rgb?
  colour.is_a?(String) && valid_rgb?
end

#rgb_prefixNotImplemented (private)

Returns:

  • (NotImplemented)

Raises:

  • (NotImplemented)

    Subclasses of this class must implement this method.



228
229
230
# File 'lib/vedeu/output/translator.rb', line 228

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

#to_htmlString

Returns:

  • (String)


70
71
72
73
74
75
76
77
78
# File 'lib/vedeu/output/translator.rb', line 70

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)


106
107
108
# File 'lib/vedeu/output/translator.rb', line 106

def valid_name?
  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)


156
157
158
# File 'lib/vedeu/output/translator.rb', line 156

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

#valid_rgb?Boolean (private)

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

Returns:

  • (Boolean)


135
136
137
# File 'lib/vedeu/output/translator.rb', line 135

def valid_rgb?
  !!(colour =~ /^#([A-Fa-f0-9]{6})$/)
end