Class: Vedeu::Colours::Translator
- Inherits:
-
Object
- Object
- Vedeu::Colours::Translator
- Defined in:
- lib/vedeu/colours/translator.rb
Overview
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
Class Method Summary collapse
-
.coerce(value) ⇒ Object
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
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
-
#css_to_rgb ⇒ Array
private
Returns a collection of converted HTML/CSS octets as their decimal equivalents.
- #empty? ⇒ Boolean
-
#eql?(other) ⇒ Boolean
(also: #==)
An object is equal when its values are the same.
- #escape_sequence ⇒ String (also: #to_s, #to_str)
-
#green ⇒ Fixnum
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
Return a new instance of Vedeu::Colours::Translator.
-
#named ⇒ String
private
Returns an escape sequence for a named background colour.
- #named? ⇒ Boolean private
- #not_implemented ⇒ Vedeu::Error::NotImplemented (also: #named_codes, #numbered_prefix, #repository, #rgb_prefix) private
-
#numbered ⇒ String
private
Returns an escape sequence.
-
#numbered? ⇒ Boolean
private
Returns a boolean indicating whether the colour provided is a terminal numbered colour.
-
#red ⇒ Fixnum
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
Registers a HTML/CSS colour code and escape sequence to reduce processing.
-
#registered?(colour) ⇒ Boolean
private
Returns a boolean indicating the HTML/CSS colour code has been registered.
-
#retrieve(colour) ⇒ String
private
Retrieves the escape sequence for the HTML/CSS colour code.
-
#rgb ⇒ String
private
Returns an escape sequence.
-
#rgb? ⇒ Boolean
private
Returns a boolean indicated whether the colour is a valid HTML/CSS colour.
- #to_html(_options = {}) ⇒ String
-
#valid_name? ⇒ Boolean
private
Returns a boolean indicating whether the colour provided is a valid named colour.
-
#valid_range? ⇒ Boolean
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
Return a new instance of Vedeu::Colours::Translator.
55 56 57 |
# File 'lib/vedeu/colours/translator.rb', line 55 def initialize(colour = '') @colour = colour || '' end |
Instance Attribute Details
#colour ⇒ String (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 46 47 48 49 |
# File 'lib/vedeu/colours/translator.rb', line 41 def self.coerce(value) if value.is_a?(self) value else new(value) end end |
Instance Method Details
#blue ⇒ Fixnum (private)
Takes the blue component of #css_to_rgb and converts to the correct value for setting the terminal blue value.
253 254 255 |
# File 'lib/vedeu/colours/translator.rb', line 253 def blue (css_to_rgb[2] / 51) * 1 end |
#css_to_numbered ⇒ Fixnum (private)
223 224 225 226 227 228 229 230 231 |
# File 'lib/vedeu/colours/translator.rb', line 223 def css_to_numbered if rgb? [16, red, green, blue].inject(:+) elsif numbered? colour end end |
#css_to_rgb ⇒ Array (private)
Returns a collection of converted HTML/CSS octets as their decimal equivalents.
214 215 216 217 218 219 220 |
# File 'lib/vedeu/colours/translator.rb', line 214 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
60 61 62 |
# File 'lib/vedeu/colours/translator.rb', line 60 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.
68 69 70 |
# File 'lib/vedeu/colours/translator.rb', line 68 def eql?(other) self.class == other.class && colour == other.colour end |
#escape_sequence ⇒ String Also known as: to_s, to_str
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/vedeu/colours/translator.rb', line 75 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)
Takes the green component of #css_to_rgb and converts to the correct value for setting the terminal green value.
245 246 247 |
# File 'lib/vedeu/colours/translator.rb', line 245 def green (css_to_rgb[1] / 51) * 6 end |
#named ⇒ String (private)
Valid names can be found at Esc#codes
Returns an escape sequence for a named background colour.
148 149 150 |
# File 'lib/vedeu/colours/translator.rb', line 148 def named "\e[#{named_codes}m" end |
#named? ⇒ Boolean (private)
138 139 140 |
# File 'lib/vedeu/colours/translator.rb', line 138 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
260 261 262 |
# File 'lib/vedeu/colours/translator.rb', line 260 def not_implemented fail Vedeu::Error::NotImplemented, 'Subclasses implement this.' end |
#numbered ⇒ String (private)
Returns an escape sequence.
171 172 173 |
# File 'lib/vedeu/colours/translator.rb', line 171 def numbered "#{numbered_prefix}#{css_to_numbered}m" end |
#numbered? ⇒ Boolean (private)
Returns a boolean indicating whether the colour provided is a terminal numbered colour.
164 165 166 |
# File 'lib/vedeu/colours/translator.rb', line 164 def numbered? colour.is_a?(Fixnum) && valid_range? end |
#red ⇒ Fixnum (private)
Takes the red component of #css_to_rgb and converts to the correct value for setting the terminal red value.
237 238 239 |
# File 'lib/vedeu/colours/translator.rb', line 237 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.
124 125 126 |
# File 'lib/vedeu/colours/translator.rb', line 124 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.
133 134 135 |
# File 'lib/vedeu/colours/translator.rb', line 133 def registered?(colour) repository.registered?(colour) end |
#retrieve(colour) ⇒ String (private)
Retrieves the escape sequence for the HTML/CSS colour code.
113 114 115 |
# File 'lib/vedeu/colours/translator.rb', line 113 def retrieve(colour) repository.retrieve(colour) end |
#rgb ⇒ String (private)
Returns an escape sequence.
188 189 190 191 192 193 194 195 196 |
# File 'lib/vedeu/colours/translator.rb', line 188 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.
179 180 181 182 183 |
# File 'lib/vedeu/colours/translator.rb', line 179 def rgb? return true if colour =~ /^#([A-Fa-f0-9]{6})$/ false end |
#to_html(_options = {}) ⇒ String
101 102 103 104 105 |
# File 'lib/vedeu/colours/translator.rb', line 101 def to_html( = {}) return colour if rgb? '' end |
#valid_name? ⇒ Boolean (private)
Returns a boolean indicating whether the colour provided is a valid named colour.
156 157 158 |
# File 'lib/vedeu/colours/translator.rb', line 156 def valid_name? Vedeu::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.
202 203 204 |
# File 'lib/vedeu/colours/translator.rb', line 202 def valid_range? colour >= 0 && colour <= 255 end |