Class: CapeCod::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/cape-cod/color.rb

Constant Summary collapse

CODES =

The ANSI color codes.

{
  black:    0,
  red:      1,
  green:    2,
  yellow:   3,
  blue:     4,
  magenta:  5,
  cyan:     6,
  white:    7,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(*color, ground) ⇒ Color

Initializes a the color.

color may be either a single Integer representation of a RGB color, a Symbol with the color name (valid color names are listed in the COLORS hash), or three integers representing the RGB channels. ground is either :background or :foreground.



28
29
30
31
32
33
34
35
36
37
# File 'lib/cape-cod/color.rb', line 28

def initialize(*color, ground)
  with_valid_color_and_ground *color, ground do |*c, g|
    if c.size == 3
      c = [(c[0] << 16) | (c[1] << 8) | c[2]]
    end

    @ground = ground
    @color  = c.first
  end
end

Instance Method Details

#ansi_codeObject

Returns a string representing the ANSI escape code for this color.



42
43
44
45
46
47
# File 'lib/cape-cod/color.rb', line 42

def ansi_code
  case @color
  when Symbol  then code_from_name
  when Integer then code_from_hex
  end
end