Class: Vedeu::Translator
- Inherits:
-
Object
- Object
- Vedeu::Translator
- Includes:
- Coercions
- Defined in:
- lib/vedeu/output/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 if you have a capable terminal and the ‘VEDEU_TERM=xterm-truecolor` environment variable set, a 24-bit representation.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#colour ⇒ Object
(also: #value)
readonly
Returns the value of attribute colour.
Class Method Summary collapse
-
.escape_sequence(colour = '') ⇒ String
Convert a CSS/HTML colour string into a terminal escape sequence.
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.
- #escape_sequence ⇒ String (also: #to_s)
-
#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 = '') ⇒ Translator
constructor
Return a new instance of Translator.
-
#named ⇒ String
private
Returns an escape sequence for a named background colour.
- #named? ⇒ Boolean private
- #named_codes ⇒ Exception private
- #no_colour? ⇒ Boolean private
-
#numbered ⇒ String
private
Returns an escape sequence.
-
#numbered? ⇒ Boolean
private
Returns a boolean indicating whether the colour provided is a terminal numbered colour.
- #numbered_prefix ⇒ NotImplemented private
-
#red ⇒ Fixnum
private
Takes the red component of #css_to_rgb and converts to the correct value for setting the terminal red value.
-
#rgb ⇒ String
private
Returns an escape sequence.
-
#rgb? ⇒ Boolean
private
Returns a boolean indicated whether the colour is an HTML/CSS colour.
- #rgb_prefix ⇒ NotImplemented private
- #to_html ⇒ 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.
-
#valid_rgb? ⇒ Boolean
private
Returns a boolean indicated whether the colour is a valid HTML/CSS colour.
Methods included from Coercions
Methods included from Common
Constructor Details
#initialize(colour = '') ⇒ Translator
Return a new instance of Translator.
43 44 45 |
# File 'lib/vedeu/output/translator.rb', line 43 def initialize(colour = '') @colour = colour end |
Instance Attribute Details
#colour ⇒ Object (readonly) Also known as: value
Returns the value of attribute colour.
28 29 30 |
# File 'lib/vedeu/output/translator.rb', line 28 def colour @colour end |
Class Method Details
.escape_sequence(colour = '') ⇒ String
Convert a CSS/HTML colour string into a terminal escape sequence.
35 36 37 |
# File 'lib/vedeu/output/translator.rb', line 35 def self.escape_sequence(colour = '') new(colour).escape_sequence 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.
207 208 209 |
# File 'lib/vedeu/output/translator.rb', line 207 def blue (css_to_rgb[2] / 51) * 1 end |
#css_to_numbered ⇒ Fixnum (private)
177 178 179 180 181 182 183 184 185 |
# File 'lib/vedeu/output/translator.rb', line 177 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.
168 169 170 171 172 173 174 |
# File 'lib/vedeu/output/translator.rb', line 168 def css_to_rgb [ colour[1..2].to_i(16), colour[3..4].to_i(16), colour[5..6].to_i(16) ] end |
#escape_sequence ⇒ String Also known as: to_s
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/vedeu/output/translator.rb', line 49 def escape_sequence if no_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.
199 200 201 |
# File 'lib/vedeu/output/translator.rb', line 199 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.
98 99 100 |
# File 'lib/vedeu/output/translator.rb', line 98 def named ["\e[", named_codes, 'm'].join end |
#named? ⇒ Boolean (private)
88 89 90 |
# File 'lib/vedeu/output/translator.rb', line 88 def named? colour.is_a?(Symbol) && valid_name? end |
#named_codes ⇒ Exception (private)
214 215 216 |
# File 'lib/vedeu/output/translator.rb', line 214 def named_codes fail NotImplemented, 'Subclasses implement this.' end |
#no_colour? ⇒ Boolean (private)
83 84 85 |
# File 'lib/vedeu/output/translator.rb', line 83 def no_colour? colour.nil? || colour.to_s.empty? end |
#numbered ⇒ String (private)
Returns an escape sequence.
121 122 123 |
# File 'lib/vedeu/output/translator.rb', line 121 def numbered [numbered_prefix, css_to_numbered, 'm'].join end |
#numbered? ⇒ Boolean (private)
Returns a boolean indicating whether the colour provided is a terminal numbered colour.
114 115 116 |
# File 'lib/vedeu/output/translator.rb', line 114 def numbered? colour.is_a?(Fixnum) && valid_range? end |
#numbered_prefix ⇒ NotImplemented (private)
221 222 223 |
# File 'lib/vedeu/output/translator.rb', line 221 def numbered_prefix fail NotImplemented, 'Subclasses implement this.' end |
#red ⇒ Fixnum (private)
Takes the red component of #css_to_rgb and converts to the correct value for setting the terminal red value.
191 192 193 |
# File 'lib/vedeu/output/translator.rb', line 191 def red (css_to_rgb[0] / 51) * 36 end |
#rgb ⇒ String (private)
Returns an escape sequence.
142 143 144 145 146 147 148 149 150 |
# File 'lib/vedeu/output/translator.rb', line 142 def rgb if Configuration.colour_mode == 16777216 sprintf(rgb_prefix, *css_to_rgb) else numbered end end |
#rgb? ⇒ Boolean (private)
Returns a boolean indicated whether the colour is an HTML/CSS colour.
128 129 130 |
# File 'lib/vedeu/output/translator.rb', line 128 def rgb? colour.is_a?(String) && valid_rgb? end |
#rgb_prefix ⇒ NotImplemented (private)
228 229 230 |
# File 'lib/vedeu/output/translator.rb', line 228 def rgb_prefix fail NotImplemented, 'Subclasses implement this.' end |
#to_html ⇒ String
70 71 72 73 74 75 76 77 78 |
# File 'lib/vedeu/output/translator.rb', line 70 def to_html if rgb? colour else '' end end |
#valid_name? ⇒ Boolean (private)
Returns a boolean indicating whether the colour provided is a valid named colour.
106 107 108 |
# File 'lib/vedeu/output/translator.rb', line 106 def valid_name? 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.
156 157 158 |
# File 'lib/vedeu/output/translator.rb', line 156 def valid_range? colour >= 0 && colour <= 255 end |
#valid_rgb? ⇒ Boolean (private)
Returns a boolean indicated whether the colour is a valid HTML/CSS colour.
135 136 137 |
# File 'lib/vedeu/output/translator.rb', line 135 def valid_rgb? !!(colour =~ /^#([A-Fa-f0-9]{6})$/) end |