Class: Ws2812::Color
- Inherits:
-
Object
- Object
- Ws2812::Color
- Defined in:
- lib/ws2812/color.rb
Overview
Simple wrapper class around RGB based color
Instance Attribute Summary collapse
-
#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_i(i) ⇒ Object
Converts color from integer
i
by taking the least significant 24 bits and using them for r(8), g(8), b(8); in this order.
Instance Method Summary collapse
-
#initialize(r, g, b) ⇒ Color
constructor
A new instance of Color.
-
#to_hex ⇒ Object
(also: #to_s)
Makes sense to represent color as hex, right?.
-
#to_i ⇒ Object
Converts color to integer by encoding
r
,g
,b
as 8bit values (in this order).
Constructor Details
#initialize(r, g, b) ⇒ Color
Returns a new instance of Color.
5 6 7 |
# File 'lib/ws2812/color.rb', line 5 def initialize(r, g, b) @r, @g, @b = r, g, b end |
Instance Attribute Details
#b ⇒ Object
Returns the value of attribute b.
8 9 10 |
# File 'lib/ws2812/color.rb', line 8 def b @b end |
#g ⇒ Object
Returns the value of attribute g.
8 9 10 |
# File 'lib/ws2812/color.rb', line 8 def g @g end |
#r ⇒ Object
Returns the value of attribute r.
8 9 10 |
# File 'lib/ws2812/color.rb', line 8 def r @r end |
Class Method Details
Instance Method Details
#to_hex ⇒ Object Also known as: to_s
Makes sense to represent color as hex, right?
28 29 30 |
# File 'lib/ws2812/color.rb', line 28 def to_hex "#%02x%02x%02x" % [r & 0xff, g & 0xff, b & 0xff] end |
#to_i ⇒ Object
Converts color to integer by encoding r
, g
, b
as 8bit values (in this order)
Thus Color.new(1,2,3).to_i # => 66051
15 16 17 |
# File 'lib/ws2812/color.rb', line 15 def to_i ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff) end |