Class: Vedeu::Colours::Translator Private
- Inherits:
-
Object
- Object
- Vedeu::Colours::Translator
- 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.
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
Instance Attribute Summary collapse
- #colour ⇒ String (also: #value) readonly private
Class Method Summary collapse
-
.coerce(value) ⇒ Object
private
Produces new objects of the correct class from the value, ignores objects Colours::that have already been coerced.
Instance Method Summary collapse
-
#blue ⇒ Fixnum
private
private
Takes the blue component of #css_to_rgb and converts to the correct value for setting the terminal blue value.
- #css_to_numbered ⇒ Fixnum private private
-
#css_to_rgb ⇒ Array
private
private
Returns a collection of converted HTML/CSS octets as their decimal equivalents.
- #empty? ⇒ Boolean private
-
#eql?(other) ⇒ Boolean
(also: #==)
private
An object is equal when its values are the same.
- #escape_sequence ⇒ String (also: #to_s, #to_str) private
-
#green ⇒ Fixnum
private
private
Takes the green component of #css_to_rgb and converts to the correct value for setting the terminal green value.
-
#initialize(colour = '') ⇒ Vedeu::Colours::Translator
constructor
private
Return a new instance of Vedeu::Colours::Translator.
-
#named ⇒ String
private
private
Returns an escape sequence for a named background colour.
- #named? ⇒ Boolean private private
- #not_implemented ⇒ Vedeu::Error::NotImplemented (also: #named_codes, #numbered_prefix, #repository, #rgb_prefix) private private
-
#numbered ⇒ String
private
private
Returns an escape sequence.
-
#numbered? ⇒ Boolean
private
private
Returns a boolean indicating whether the colour provided is a terminal numbered colour.
-
#red ⇒ Fixnum
private
private
Takes the red component of #css_to_rgb and converts to the correct value for setting the terminal red value.
-
#register(colour, escape_sequence) ⇒ String
private
private
Registers a HTML/CSS colour code and escape sequence to reduce processing.
-
#registered?(colour) ⇒ Boolean
private
private
Returns a boolean indicating the HTML/CSS colour code has been registered.
-
#retrieve(colour) ⇒ String
private
private
Retrieves the escape sequence for the HTML/CSS colour code.
-
#rgb ⇒ String
private
private
Returns an escape sequence.
-
#rgb? ⇒ Boolean
private
private
Returns a boolean indicated whether the colour is a valid HTML/CSS colour.
- #to_html(_options = {}) ⇒ String private
-
#valid_name? ⇒ Boolean
private
private
Returns a boolean indicating whether the colour provided is a valid named colour.
-
#valid_range? ⇒ Boolean
private
private
Returns a boolean indicating whether the numbered colour is within the range of valid terminal numbered colours.
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.
53 54 55 |
# File 'lib/vedeu/colours/translator.rb', line 53 def initialize(colour = '') @colour = colour || '' end |
Instance Attribute Details
#colour ⇒ String (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.
35 36 37 |
# File 'lib/vedeu/colours/translator.rb', line 35 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.
43 44 45 46 47 |
# File 'lib/vedeu/colours/translator.rb', line 43 def self.coerce(value) return value if value.is_a?(self) new(value) end |
Instance Method Details
#blue ⇒ Fixnum (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.
252 253 254 |
# File 'lib/vedeu/colours/translator.rb', line 252 def blue (css_to_rgb[2] / 51) * 1 end |
#css_to_numbered ⇒ Fixnum (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.
222 223 224 225 226 227 228 229 230 |
# File 'lib/vedeu/colours/translator.rb', line 222 def css_to_numbered if rgb? [16, red, green, blue].inject(:+) elsif numbered? colour end end |
#css_to_rgb ⇒ Array (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.
213 214 215 216 217 218 219 |
# File 'lib/vedeu/colours/translator.rb', line 213 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.
58 59 60 |
# File 'lib/vedeu/colours/translator.rb', line 58 def empty? colour.nil? || colour.to_s.empty? 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.
66 67 68 |
# File 'lib/vedeu/colours/translator.rb', line 66 def eql?(other) self.class == other.class && colour == other.colour end |
#escape_sequence ⇒ String 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.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/vedeu/colours/translator.rb', line 73 def escape_sequence if empty? '' elsif registered?(colour) retrieve(colour) elsif rgb? rgb elsif numbered? numbered elsif named? named else '' end end |
#green ⇒ Fixnum (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.
244 245 246 |
# File 'lib/vedeu/colours/translator.rb', line 244 def green (css_to_rgb[1] / 51) * 6 end |
#named ⇒ 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.
Valid names can be found at EscapeSequences::Esc#codes
Returns an escape sequence for a named background colour.
147 148 149 |
# File 'lib/vedeu/colours/translator.rb', line 147 def named "\e[#{named_codes}m".freeze end |
#named? ⇒ 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.
136 137 138 |
# File 'lib/vedeu/colours/translator.rb', line 136 def named? colour.is_a?(Symbol) && valid_name? end |
#not_implemented ⇒ Vedeu::Error::NotImplemented (private) Also known as: named_codes, numbered_prefix, repository, rgb_prefix
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.
259 260 261 |
# File 'lib/vedeu/colours/translator.rb', line 259 def not_implemented fail Vedeu::Error::NotImplemented, 'Subclasses implement this.'.freeze end |
#numbered ⇒ 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.
Returns an escape sequence.
170 171 172 |
# File 'lib/vedeu/colours/translator.rb', line 170 def numbered "#{numbered_prefix}#{css_to_numbered}m".freeze end |
#numbered? ⇒ 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 whether the colour provided is a terminal numbered colour.
163 164 165 |
# File 'lib/vedeu/colours/translator.rb', line 163 def numbered? colour.is_a?(Fixnum) && valid_range? end |
#red ⇒ Fixnum (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.
236 237 238 |
# File 'lib/vedeu/colours/translator.rb', line 236 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.
122 123 124 |
# File 'lib/vedeu/colours/translator.rb', line 122 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.
131 132 133 |
# File 'lib/vedeu/colours/translator.rb', line 131 def registered?(colour) repository.registered?(colour) 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.
111 112 113 |
# File 'lib/vedeu/colours/translator.rb', line 111 def retrieve(colour) repository.retrieve(colour) end |
#rgb ⇒ 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.
Returns an escape sequence.
187 188 189 190 191 192 193 194 195 |
# File 'lib/vedeu/colours/translator.rb', line 187 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)
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 indicated whether the colour is a valid HTML/CSS colour.
178 179 180 181 182 |
# File 'lib/vedeu/colours/translator.rb', line 178 def rgb? return true if colour =~ /^#([A-Fa-f0-9]{6})$/.freeze false end |
#to_html(_options = {}) ⇒ String
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.
99 100 101 102 103 |
# File 'lib/vedeu/colours/translator.rb', line 99 def to_html( = {}) return colour if rgb? '' end |
#valid_name? ⇒ 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 whether the colour provided is a valid named colour.
155 156 157 |
# File 'lib/vedeu/colours/translator.rb', line 155 def valid_name? Vedeu::EscapeSequences::Esc.codes.keys.include?(colour) end |
#valid_range? ⇒ 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 whether the numbered colour is within the range of valid terminal numbered colours.
201 202 203 |
# File 'lib/vedeu/colours/translator.rb', line 201 def valid_range? colour >= 0 && colour <= 255 end |