Class: Vedeu::Colours::Colour

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Repositories::Defaults
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

Methods included from Repositories::Defaults

#initialize, #validate

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?

Class Method Details

.defaultVedeu::Colours::Colour



50
51
52
# File 'lib/vedeu/colours/colour.rb', line 50

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

Instance Method Details

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



68
69
70
71
72
73
# File 'lib/vedeu/colours/colour.rb', line 68

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

#backgroundVedeu::Colours::Background



76
77
78
# File 'lib/vedeu/colours/colour.rb', line 76

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

#background=(value) ⇒ Vedeu::Colours::Foreground

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

Parameters:

  • value (String)

Returns:



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

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

#defaultsHash<Symbol => void> (private)

The default options/attributes for a new instance of this class.

Returns:

  • (Hash<Symbol => void>)


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

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:

  • other (void)

Returns:



92
93
94
95
96
# File 'lib/vedeu/colours/colour.rb', line 92

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

#foregroundVedeu::Colours::Foreground



100
101
102
# File 'lib/vedeu/colours/colour.rb', line 100

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

#foreground=(value) ⇒ Vedeu::Colours::Foreground

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

Parameters:

  • value (String)

Returns:



108
109
110
# File 'lib/vedeu/colours/colour.rb', line 108

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

#to_astString

Returns:

  • (String)


113
114
115
116
117
# File 'lib/vedeu/colours/colour.rb', line 113

def to_ast
  [foreground.to_ast, background.to_ast].reject do |value|
    absent?(value)
  end.join(' ')
end

#to_hHash<Symbol => Hash<Symbol => String>> Also known as: to_hash

Returns:

  • (Hash<Symbol => Hash<Symbol => String>>)


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

def to_h
  {
    colour: background.to_h.merge!(foreground.to_h),
  }
end

#to_sString Also known as: to_str

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

Returns:

  • (String)


131
132
133
# File 'lib/vedeu/colours/colour.rb', line 131

def to_s
  "#{foreground}#{background}"
end