Class: Vedeu::Colours::Translator Private
- Inherits:
-
Object
- Object
- Vedeu::Colours::Translator
- 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.
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 ⇒ Object private private
-
#numbered ⇒ String
private
private
Returns an escape sequence.
-
#numbered_prefix ⇒ String
private
private
Returns an escape sequence.
-
#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.
- #repository ⇒ Object private private
-
#retrieve(colour) ⇒ String
private
private
Retrieves the escape sequence for the HTML/CSS colour code.
-
#rgb ⇒ String
private
private
Returns an escape sequence.
-
#rgb_prefix ⇒ String
private
private
Returns part of an escape sequence.
- #validator ⇒ Vedeu::Colours::Validator private private
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.
63 64 65 |
# File 'lib/vedeu/colours/translator.rb', line 63 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.
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.
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
#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.
214 215 216 |
# File 'lib/vedeu/colours/translator.rb', line 214 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.
190 191 192 |
# File 'lib/vedeu/colours/translator.rb', line 190 def css_to_numbered [16, red, green, blue].inject(:+) 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.
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.
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.
76 77 78 |
# File 'lib/vedeu/colours/translator.rb', line 76 def eql?(other) self.class.equal?(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.
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 |
#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.
206 207 208 |
# File 'lib/vedeu/colours/translator.rb', line 206 def green (css_to_rgb[1] / 51) * 6 end |
#named ⇒ Object (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.
219 220 221 |
# File 'lib/vedeu/colours/translator.rb', line 219 def named raise Vedeu::Error::NotImplemented 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.
139 140 141 |
# File 'lib/vedeu/colours/translator.rb', line 139 def numbered format(numbered_prefix, colour) end |
#numbered_prefix ⇒ 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.
146 147 148 |
# File 'lib/vedeu/colours/translator.rb', line 146 def numbered_prefix "#{prefix}5;%sm" 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.
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.
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.
132 133 134 |
# File 'lib/vedeu/colours/translator.rb', line 132 def registered?(colour) repository.registered?(colour) end |
#repository ⇒ Object (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.
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.
112 113 114 |
# File 'lib/vedeu/colours/translator.rb', line 112 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.
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_prefix ⇒ 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 part of an escape sequence.
169 170 171 |
# File 'lib/vedeu/colours/translator.rb', line 169 def rgb_prefix "#{prefix}2;%s;%s;%sm" end |
#validator ⇒ Vedeu::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 |