Class: Ws2812::Color

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

Overview

Simple wrapper class around RGB based color

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bObject

Returns the value of attribute b.



8
9
10
# File 'lib/ws2812/color.rb', line 8

def b
  @b
end

#gObject

Returns the value of attribute g.



8
9
10
# File 'lib/ws2812/color.rb', line 8

def g
  @g
end

#rObject

Returns the value of attribute r.



8
9
10
# File 'lib/ws2812/color.rb', line 8

def r
  @r
end

Class Method Details

.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.



22
23
24
# File 'lib/ws2812/color.rb', line 22

def self.from_i(i)
	Color.new((i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff)
end

Instance Method Details

#to_hexObject 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_iObject

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