Class: Vedeu::Colours::Translator Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Vedeu::Common
Defined in:
lib/vedeu/colours/translator.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Methods included from Vedeu::Common

#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?

Constructor Details

#initialize(colour = '') ⇒ Vedeu::Colours::Translator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

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


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

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

Instance Attribute Details

#colourString (readonly) Also known as: value

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


45
46
47
# File 'lib/vedeu/colours/translator.rb', line 45

def colour
  @colour
end

Class Method Details

.coerce(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • value (Object|NilClass)

Returns:

  • (Object)


53
54
55
56
57
# File 'lib/vedeu/colours/translator.rb', line 53

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

  new(value)
end

Instance Method Details

#blueFixnum (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Fixnum)


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

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

#css_to_numberedFixnum (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Fixnum)


190
191
192
# File 'lib/vedeu/colours/translator.rb', line 190

def css_to_numbered
  [16, red, green, blue].inject(:+)
end

#css_to_rgbArray (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

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

Returns:

  • (Array)


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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



68
69
70
# File 'lib/vedeu/colours/translator.rb', line 68

def empty?
  absent?(colour)
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An object is equal when its values are the same.

Parameters:

  • other (void)

Returns:



76
77
78
# File 'lib/vedeu/colours/translator.rb', line 76

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

#escape_sequenceString Also known as: to_s, to_str

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)

See Also:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vedeu/colours/translator.rb', line 83

def escape_sequence
  return '' if empty?

  if registered?(colour)
    retrieve(colour)

  elsif rgb?
    rgb

  elsif numbered?
    numbered

  elsif named?
    named

  else
    ''

  end
end

#greenFixnum (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Fixnum)


206
207
208
# File 'lib/vedeu/colours/translator.rb', line 206

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

#namedObject (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (Vedeu::Error::NotImplemented)

    When the method called should be handled by a subclass of the current class, or by the class including or extending the current module.



219
220
221
# File 'lib/vedeu/colours/translator.rb', line 219

def named
  raise Vedeu::Error::NotImplemented
end

#numberedString (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an escape sequence.

Returns:

  • (String)


139
140
141
# File 'lib/vedeu/colours/translator.rb', line 139

def numbered
  format(numbered_prefix, colour)
end

#numbered_prefixString (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an escape sequence.

Returns:

  • (String)


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

def numbered_prefix
  "#{prefix}5;%sm"
end

#redFixnum (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Fixnum)


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

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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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)


123
124
125
# File 'lib/vedeu/colours/translator.rb', line 123

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

#registered?(colour) ⇒ Boolean (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • colour (String)

Returns:



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

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

#repositoryObject (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (Vedeu::Error::NotImplemented)

    When the method called should be handled by a subclass of the current class, or by the class including or extending the current module.



224
225
226
# File 'lib/vedeu/colours/translator.rb', line 224

def repository
  raise Vedeu::Error::NotImplemented
end

#retrieve(colour) ⇒ String (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • colour (String)

Returns:

  • (String)


112
113
114
# File 'lib/vedeu/colours/translator.rb', line 112

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

#rgbString (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an escape sequence.

Returns:

  • (String)


153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/vedeu/colours/translator.rb', line 153

def rgb
  if registered?(colour)
    retrieve(colour)

  elsif [8, 16, 256].include?(Vedeu.config.colour_mode)
    register(colour, format(numbered_prefix, css_to_numbered))

  elsif Vedeu.config.colour_mode == 16_777_216
    register(colour, format(rgb_prefix, *css_to_rgb))

  end
end

#rgb_prefixString (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns part of an escape sequence.

Returns:

  • (String)


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

def rgb_prefix
  "#{prefix}2;%s;%s;%sm"
end

#validatorVedeu::Colours::Validator (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



229
230
231
# File 'lib/vedeu/colours/translator.rb', line 229

def validator
  @validator ||= Vedeu::Colours::Validator.new(colour)
end