Class: IQ::Color::RGBA
- Inherits:
-
RGB
- Object
- RGB
- IQ::Color::RGBA
- Defined in:
- lib/iq/color/rgba.rb
Overview
A class for representing RGB values with an alpha channel.
Instance Attribute Summary collapse
-
#alpha ⇒ Object
readonly
Returns the value of attribute alpha.
Class Method Summary collapse
-
.from_css(css_string) ⇒ Object
Returns a new instance based on the css string supplied.
Instance Method Summary collapse
-
#initialize(red, green, blue, alpha = 1.0) ⇒ RGBA
constructor
Returns a new IQ::Color::RGBA instance with the supplied rgb and alpha values.
-
#to_css ⇒ Object
Returns the color as a css rgba string e.g.
Constructor Details
#initialize(red, green, blue, alpha = 1.0) ⇒ RGBA
Returns a new IQ::Color::RGBA instance with the supplied rgb and alpha values.
7 8 9 10 11 12 |
# File 'lib/iq/color/rgba.rb', line 7 def initialize(red, green, blue, alpha = 1.0) raise ArgumentError, 'Alpha value must be an integer of float' unless alpha.is_a?(Integer) || alpha.is_a?(Float) raise ArgumentError, 'Alpha value must be between 0.0 and 1.0' if alpha < 0 || alpha > 1 super red, green, blue @alpha = alpha.to_f end |
Instance Attribute Details
#alpha ⇒ Object (readonly)
Returns the value of attribute alpha.
4 5 6 |
# File 'lib/iq/color/rgba.rb', line 4 def alpha @alpha end |
Class Method Details
.from_css(css_string) ⇒ Object
Returns a new instance based on the css string supplied. Accepts hex, ‘rgb’ and ‘rgba’ notation e.g. '#fed'
, '#ffeedd'
, 'rgb(255,238,221)'
and 'rgb(255,238,221,1.0)'
(note that white space is allowed either side of the commas).
If an 'rgba'
string is supplied then the ‘a’ segment is the alpha value between 0 and 1 (0 is fully transparent and 1 is fully opaque).
A value of 'transparent'
may be supplied as a shortcut for rgba(0,0,0,0)
.
More on CSS3 colour strings can be found at: www.w3.org/TR/css3-color
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/iq/color/rgba.rb', line 33 def self.from_css(css_string) if css_string == 'transparent' new 0, 0, 0, 0 elsif match = css_string.match(/^rgba\(#{COLOR_MATCH},#{COLOR_MATCH},#{COLOR_MATCH},#{FLOAT_MATCH}\)$/) # 'rgba(n,n,n,n)' notation new _flatten_percentage(match[1]), _flatten_percentage(match[2]), _flatten_percentage(match[3]), match[4].to_f else super css_string end end |
Instance Method Details
#to_css ⇒ Object
Returns the color as a css rgba string e.g. 'rgba(14,20,40,0.4)'
.
15 16 17 |
# File 'lib/iq/color/rgba.rb', line 15 def to_css 'rgba(%d,%d,%d,%s)' % [red, green, blue, alpha] end |