Class: Rainbow::Color

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

Direct Known Subclasses

Indexed

Defined Under Namespace

Classes: Indexed, Named, RGB

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#groundObject (readonly)

Returns the value of attribute ground.



4
5
6
# File 'lib/rainbow/color.rb', line 4

def ground
  @ground
end

Class Method Details

.build(ground, values) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rainbow/color.rb', line 6

def self.build(ground, values)
  raise ArgumentError.new(
    "Wrong number of arguments for color definition, should be 1 or 3"
  ) unless [1, 3].include?(values.size)

  color = values.size == 1 ? values.first : values

  case color
    when ::Fixnum then Indexed.new(ground, color)
    when ::Symbol then Named.new(ground, color)
    when ::Array  then RGB.new(ground, *color)
    when ::String then RGB.new(ground, *parse_hex_color(color))
  end
end

.parse_hex_color(hex) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/rainbow/color.rb', line 21

def self.parse_hex_color(hex)
  hex = hex.gsub('#', '')
  r   = hex[0..1].to_i(16)
  g   = hex[2..3].to_i(16)
  b   = hex[4..5].to_i(16)

  [r, g, b]
end