Class: Sickill::Rainbow::AnsiRgb

Inherits:
Object
  • Object
show all
Defined in:
lib/ansi_rgb.rb

Overview

Retrieve ANSI color code from RGB color.

Instance Method Summary collapse

Constructor Details

#initialize(ground, rgb) ⇒ AnsiRgb

ground is one of :foreground, :background rgb is an array of 3 values between 0 and 255.



9
10
11
12
13
14
15
16
# File 'lib/ansi_rgb.rb', line 9

def initialize(ground, rgb)
  if RGB.outside_range?(rgb)
    raise ArgumentError.new("RGB value outside 0-255 range")
  end

  @ground_code = { :foreground => 38, :background => 48 }[ground]
  @red, @green, @blue = rgb[0], rgb[1], rgb[2]
end

Instance Method Details

#codeObject

Get the ANSI color code for this RGB color.



19
20
21
22
23
24
25
26
# File 'lib/ansi_rgb.rb', line 19

def code
  index = 16 +
          RGB.to_ansi_domain(@red) * 36 +
          RGB.to_ansi_domain(@green) * 6 +
          RGB.to_ansi_domain(@blue)

  "#{@ground_code};5;#{index}"
end