Class: Vedeu::Colours::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/colours/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 when you have a capable terminal and the ‘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 = '') ⇒ Vedeu::Colours::Translator

Return a new instance of Vedeu::Colours::Translator.



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

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

Instance Attribute Details

#colourString (readonly) Also known as: value



33
34
35
# File 'lib/vedeu/colours/translator.rb', line 33

def colour
  @colour
end

Class Method Details

.coerce(value) ⇒ Object

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



41
42
43
44
45
# File 'lib/vedeu/colours/translator.rb', line 41

def self.coerce(value)
  return value if value.is_a?(self)

  new(value)
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.



250
251
252
# File 'lib/vedeu/colours/translator.rb', line 250

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

#css_to_numberedFixnum (private)



220
221
222
223
224
225
226
227
228
# File 'lib/vedeu/colours/translator.rb', line 220

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]


211
212
213
214
215
216
217
# File 'lib/vedeu/colours/translator.rb', line 211

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

#empty?Boolean



56
57
58
# File 'lib/vedeu/colours/translator.rb', line 56

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

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

An object is equal when its values are the same.



64
65
66
# File 'lib/vedeu/colours/translator.rb', line 64

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

#escape_sequenceString Also known as: to_s, to_str



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vedeu/colours/translator.rb', line 71

def escape_sequence
  if empty?
    ''

  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.



242
243
244
# File 'lib/vedeu/colours/translator.rb', line 242

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

#namedString (private)

Note:

Valid names can be found at EscapeSequences::Esc#codes

Returns an escape sequence for a named background colour.



145
146
147
# File 'lib/vedeu/colours/translator.rb', line 145

def named
  "\e[#{named_codes}m".freeze
end

#named?Boolean (private)



134
135
136
# File 'lib/vedeu/colours/translator.rb', line 134

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

#not_implementedVedeu::Error::NotImplemented (private) Also known as: named_codes, numbered_prefix, repository, rgb_prefix

Raises:



257
258
259
# File 'lib/vedeu/colours/translator.rb', line 257

def not_implemented
  fail Vedeu::Error::NotImplemented, 'Subclasses implement this.'.freeze
end

#numberedString (private)

Returns an escape sequence.



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

def numbered
  "#{numbered_prefix}#{css_to_numbered}m".freeze
end

#numbered?Boolean (private)

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



161
162
163
# File 'lib/vedeu/colours/translator.rb', line 161

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.



234
235
236
# File 'lib/vedeu/colours/translator.rb', line 234

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.



120
121
122
# File 'lib/vedeu/colours/translator.rb', line 120

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.



129
130
131
# File 'lib/vedeu/colours/translator.rb', line 129

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

#retrieve(colour) ⇒ String (private)

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



109
110
111
# File 'lib/vedeu/colours/translator.rb', line 109

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

#rgbString (private)

Returns an escape sequence.



185
186
187
188
189
190
191
192
193
# File 'lib/vedeu/colours/translator.rb', line 185

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.



176
177
178
179
180
# File 'lib/vedeu/colours/translator.rb', line 176

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

  false
end

#to_html(_options = {}) ⇒ String



97
98
99
100
101
# File 'lib/vedeu/colours/translator.rb', line 97

def to_html(_options = {})
  return colour if rgb?

  ''
end

#valid_name?Boolean (private)

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



153
154
155
# File 'lib/vedeu/colours/translator.rb', line 153

def valid_name?
  Vedeu::EscapeSequences::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.



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

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