Class: ASE::Color
- Inherits:
-
Object
- Object
- ASE::Color
- Defined in:
- lib/ase/color.rb
Instance Attribute Summary collapse
-
#a ⇒ Object
Returns the value of attribute a.
-
#b ⇒ Object
Returns the value of attribute b.
-
#g ⇒ Object
Returns the value of attribute g.
-
#r ⇒ Object
Returns the value of attribute r.
Class Method Summary collapse
- .from_hex(hex) ⇒ Object
- .from_rgba(*args) ⇒ Object (also: from_rgb)
Instance Method Summary collapse
- #[](i) ⇒ Object
-
#initialize(r, g, b, a = 255) ⇒ Color
constructor
We always internally store the color as RGBA.
- #to_css ⇒ Object
- #to_hex(incl_hash = true, incl_alpha = false) ⇒ Object (also: #to_s)
- #to_rgb ⇒ Object
- #to_rgba ⇒ Object
Constructor Details
#initialize(r, g, b, a = 255) ⇒ Color
We always internally store the color as RGBA.
20 21 22 23 24 25 |
# File 'lib/ase/color.rb', line 20 def initialize(r, g, b, a = 255) @r = r @g = g @b = b @a = a end |
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
17 18 19 |
# File 'lib/ase/color.rb', line 17 def a @a end |
#b ⇒ Object
Returns the value of attribute b.
17 18 19 |
# File 'lib/ase/color.rb', line 17 def b @b end |
#g ⇒ Object
Returns the value of attribute g.
17 18 19 |
# File 'lib/ase/color.rb', line 17 def g @g end |
#r ⇒ Object
Returns the value of attribute r.
17 18 19 |
# File 'lib/ase/color.rb', line 17 def r @r end |
Class Method Details
.from_hex(hex) ⇒ Object
12 13 14 |
# File 'lib/ase/color.rb', line 12 def from_hex(hex) self.new *hex.gsub('#', '').scan(/../).map { |c| c.to_i(16) } end |
.from_rgba(*args) ⇒ Object Also known as: from_rgb
6 7 8 9 |
# File 'lib/ase/color.rb', line 6 def from_rgba(*args) args = args.first if args.length == 1 self.new *args end |
Instance Method Details
#[](i) ⇒ Object
39 40 41 |
# File 'lib/ase/color.rb', line 39 def [](i) [@r, @g, @b, @a][i] end |
#to_css ⇒ Object
35 36 37 |
# File 'lib/ase/color.rb', line 35 def to_css "rgba(#{r}, #{g}, #{b}, #{a})" end |
#to_hex(incl_hash = true, incl_alpha = false) ⇒ Object Also known as: to_s
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ase/color.rb', line 43 def to_hex(incl_hash=true, incl_alpha=false) hex = incl_hash ? '#' : '' colors = [@r, @g, @b] colors << @a if incl_alpha colors.each do |c| color = c.to_s(16) if c < 16 hex << "0#{color}" else hex << color end end return hex end |
#to_rgb ⇒ Object
31 32 33 |
# File 'lib/ase/color.rb', line 31 def to_rgb [@r, @g, @b] end |
#to_rgba ⇒ Object
27 28 29 |
# File 'lib/ase/color.rb', line 27 def to_rgba [@r, @g, @b, @a] end |