Class: String

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

Overview

Monkey patch String class with color conversion utility methods

Instance Method Summary collapse

Instance Method Details

#to_color(format = :hex) ⇒ Object

Convert a String to color

Examples:

'0x0000ff'.to_color(:hex) #=> Color::RGB
'#0000ff'.to_color(:html) #=> Color::RGB
'aquamarine'.to_color(:name) #=> Color::RGB

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

def to_color(format = :hex)
  format = format.to_sym

  if format == :hex
    text = self.sub(/0x/, '')
    return Color::RGB.by_hex(text)
  end

  if format == :html
    return Color::RGB.from_html(self)
  end

  if format == :name
    return Color::Maker.by_name(self)
  end
  
  raise "Unknown format: #{format}"
end