Class: Array

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

Overview

Monkey patch Array class with color conversion utility methods

Instance Method Summary collapse

Instance Method Details

#to_color(format = :rgb) ⇒ Color::RGB

Converts an array to color

Examples:

[150, 200, 100].to_color(:rgb) #=> Color::RGB
[200, 0.9, 0.3].to_color(:hsv) #=> Color::RGB
[150, 0.3, 0.1].to_color(:hsl) #=> Color::RGB

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/color/array.rb', line 11

def to_color(format = :rgb)
  format = format.to_sym
  if format == :rgb
    r, g, b = self
    return Color::Maker::Support::rgb_to_color(r: r.to_i, g: g.to_i, b: b.to_i)
  end

  if format == :hsv
    h, s, v = self
    return Color::Maker::Support::hsv_to_color(h: h.to_f, s: s.to_f, v: v.to_f)
  end

  if format == :hsl
    h, s, l = self
    return Color::Maker::Support::hsl_to_color(h: h.to_f, s: s.to_f, l: l.to_f)
  end

  raise "Unknown color format: #{format}"
end