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

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

#absent?(variable) ⇒ Boolean Originally defined in module Vedeu::Common

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 whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

#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

#become(klass, attributes) ⇒ Class Originally defined in module Vedeu::Common

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.

Converts one class into another.

Parameters:

  • klass (Class)

    The class to become an instance of.

  • attributes (Hash)

    The attributes of klass.

Returns:

  • (Class)

    Returns a new instance of klass.

#boolean(value) ⇒ Boolean Originally defined in module Vedeu::Common

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 value was a boolean.

Parameters:

  • value (void)

Returns:

#boolean?(value) ⇒ Boolean Originally defined in module Vedeu::Common

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 whether the value is a Boolean.

Parameters:

Returns:

#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]



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
# File 'lib/vedeu/colours/colour.rb', line 92

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

#escape?(value) ⇒ Boolean Originally defined in module Vedeu::Common

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 whether the value is an escape sequence object (e.g. Vedeu::Cells::Escape.)

Returns:

#falsy?(value) ⇒ Boolean Originally defined in module Vedeu::Common

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 whether the value should be considered false.

Parameters:

  • value (void)

Returns:

#foregroundVedeu::Colours::Foreground



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

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:



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

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

#hash?(value) ⇒ Boolean Originally defined in module Vedeu::Common

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 whether the value is a Hash.

Parameters:

  • value (Hash|void)

Returns:

#initialize(attributes = {}) ⇒ void Originally defined in module Repositories::Defaults

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.

Note:

If a particular key is missing from the attributes parameter, then it is added with the respective value from #defaults.

Returns a new instance of the class including this module.

Parameters:

  • attributes (Hash) (defaults to: {})

#line_model?Boolean Originally defined in module Vedeu::Common

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 model is a Views::Line.

Returns:

#numeric?(value) ⇒ Boolean Originally defined in module Vedeu::Common

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 whether the value is a Fixnum.

Parameters:

  • value (Fixnum|void)

Returns:

#present?(variable) ⇒ Boolean Originally defined in module Vedeu::Common

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 whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

#snake_case(klass) ⇒ String Originally defined in module Vedeu::Common

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.

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

snake_case('MyClassName') # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • klass (Module|Class|String)

Returns:

  • (String)

#stream_model?Boolean Originally defined in module Vedeu::Common

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 model is a Views::Stream.

Returns:

#string?(value) ⇒ Boolean Originally defined in module Vedeu::Common

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 whether the value is a Fixnum.

Parameters:

  • value (String|void)

Returns:

#to_astString

Returns:

  • (String)


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

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>>)


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

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)


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

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

#truthy?(value) ⇒ Boolean Originally defined in module Vedeu::Common

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 whether the value should be considered true.

Parameters:

  • value (void)

Returns:

#validate(attributes) ⇒ Hash (private) Originally defined in module Repositories::Defaults

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.

Parameters:

  • attributes (Hash)

Returns:

  • (Hash)

Raises:

  • (Vedeu::Error::InvalidSyntax)

    When the value given for an argument or parameter cannot be used because it is not valid for the use case, unsupported or the method expects a different type.

#view_model?Boolean Originally defined in module Vedeu::Common

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 model is a Views::View.

Returns: