Module: IQ::Color

Defined in:
lib/iq/color.rb,
lib/iq/color/version.rb

Defined Under Namespace

Classes: Base, RGBA

Constant Summary collapse

VERSION =
"0.9.4"

Class Method Summary collapse

Class Method Details

.from_css(css_string) ⇒ Object

Returns a new instance based on the CSS3 color string supplied. Accepts both hex and ‘rgb’ notation e.g. '#fed', '#ffeedd' and 'rgb(255,238,221)' (note that white space is allowed either side of the commas). For both color notation formats, an IQ::Color:RGB instance is returned.

In addition to the standard rgb notations above, the 'rgba' notation can be supplied where ‘a’ is the alpha channel value between 0 and 1 (0 is fully transparent and 1 is fully opaque). In this case a new IQ::Color::RGBA instance is returned.

A value of 'transparent' may be supplied as a shortcut for rgba(0,0,0,0) which will return a new IQ::Color::RGBA instance.

More on CSS3 colour strings can be found at: www.w3.org/TR/css3-color

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/iq/color.rb', line 20

def self.from_css(css_string)
  raise ArgumentError, 'Must supply a string' unless css_string.is_a?(String)
  
  if css_string[0, 1] == '#' || css_string.match(/^rgb\(.*\)$/)
    # RGB notations e.g. '#f00', '#ff0000' or 'rgb(n,n,n)'.
    IQ::Color::RGB.from_css css_string
  elsif css_string.match(/^rgba\(.*\)$/) || css_string == 'transparent'
    # RGBA notation e.g. 'rgba(n,n,n,n)' or 'transparent' string.
    IQ::Color::RGBA.from_css css_string
  else
    raise ArgumentError, 'Unrecognised CSS colour format'
  end
end