Class: Vedeu::Colours::Colour

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/colours/colour.rb

Overview

TODO:

Fix colours in all terminals. (GL: 2015-04-13)

Provides a container for terminal escape sequences controlling the foreground and background colours of a character or collection of characters.

Vedeu uses HTML/CSS style notation (i.e. ‘#aadd00’), they can be used at the stream level, the line level or for the whole interface. Terminals generally support either 8, 16 or 256 colours, with few supporting full 24-bit colour (see notes below).

Vedeu attempts to detect the colour depth using the ‘$TERM` environment variable.

To set your ‘$TERM` variable to allow 256 colour support:

“‘bash echo “export TERM=xterm-256color” >> ~/.bashrc “`

Notes: Terminals which support the 24-bit colour mode include (but are not limited to): iTerm2 (OSX), Gnome Terminal (Linux).

Setting your ‘$TERM` environment variable as above gets you up to 256 colours, but when you then add the `colour_mode 16_777_216` configuration to your client application, it’s really a hit and miss affair. iTerm2 renders all the colours correctly as does Gnome Terminal. Terminator (Linux) goes crazy though and defaults to 16 colours despite the ‘$TERM` setting. This area needs more work in Vedeu.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Colours::Colour

Returns a new instance of Vedeu::Colours::Colour.

Parameters:

Options Hash (attributes):

  • background (String)
  • foreground (String)


73
74
75
76
77
# File 'lib/vedeu/colours/colour.rb', line 73

def initialize(attributes = {})
  defaults.merge!(attributes).each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Class Method Details

.coerce(value) ⇒ Vedeu::Colours::Colour

Parameters:

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vedeu/colours/colour.rb', line 42

def self.coerce(value)
  return value if value.is_a?(self)
  return new unless value.is_a?(Hash)

  if value[:colour] && value[:colour].is_a?(self)
    value[:colour]

  elsif value[:colour] && value[:colour].is_a?(Hash)
    new(value[:colour])

  elsif value[:background] || value[:foreground]
    new(value)

  else
    new

  end
end

.defaultVedeu::Colours::Colour



62
63
64
# File 'lib/vedeu/colours/colour.rb', line 62

def self.default
  new(background: :default, foreground: :default)
end

Instance Method Details

#attributesHash<Symbol => Vedeu::Colours::Background, Vedeu::Colours::Foreground>



81
82
83
84
85
86
# File 'lib/vedeu/colours/colour.rb', line 81

def attributes
  {
    background: background,
    foreground: foreground,
  }
end

#backgroundVedeu::Colours::Background



89
90
91
# File 'lib/vedeu/colours/colour.rb', line 89

def background
  @background = Vedeu::Colours::Background.coerce(@background)
end

#background=(value) ⇒ String

Converts the value into a Vedeu::Colours::Background.

Parameters:

  • value (String)

Returns:

  • (String)


97
98
99
# File 'lib/vedeu/colours/colour.rb', line 97

def background=(value)
  @background = Vedeu::Colours::Background.coerce(value)
end

#defaultsHash<Symbol => Vedeu::Colours::Background| Vedeu::Colours:Foreground] (private)

Returns Hash<Symbol => Vedeu::Colours::Background| Vedeu::Colours:Foreground].

Returns:

  • (Hash<Symbol => Vedeu::Colours::Background| Vedeu::Colours:Foreground])

    Hash<Symbol => Vedeu::Colours::Background| Vedeu::Colours:Foreground]



137
138
139
140
141
142
# File 'lib/vedeu/colours/colour.rb', line 137

def defaults
  {
    background: Vedeu::Colours::Background.new,
    foreground: Vedeu::Colours::Foreground.new,
  }
end

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

An object is equal when its values are the same.

Parameters:

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/vedeu/colours/colour.rb', line 105

def eql?(other)
  self.class == other.class && background == other.background &&
    foreground == other.foreground
end

#foregroundVedeu::Colours::Foreground



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

def foreground
  @foreground = Vedeu::Colours::Foreground.coerce(@foreground)
end

#foreground=(value) ⇒ String

Converts the value into a Vedeu::Colours::Foreground.

Parameters:

  • value (String)

Returns:

  • (String)


120
121
122
# File 'lib/vedeu/colours/colour.rb', line 120

def foreground=(value)
  @foreground = Vedeu::Colours::Foreground.coerce(value)
end

#to_sString Also known as: to_str

Returns both or either of the converted attributes into a single escape sequence.

Returns:

  • (String)


128
129
130
# File 'lib/vedeu/colours/colour.rb', line 128

def to_s
  foreground.to_s + background.to_s
end