Class: CheekyPi::Color

Inherits:
Struct
  • Object
show all
Defined in:
lib/cheeky-pi/color.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bObject

Returns the value of attribute b

Returns:

  • (Object)

    the current value of b



2
3
4
# File 'lib/cheeky-pi/color.rb', line 2

def b
  @b
end

#gObject

Returns the value of attribute g

Returns:

  • (Object)

    the current value of g



2
3
4
# File 'lib/cheeky-pi/color.rb', line 2

def g
  @g
end

#rObject

Returns the value of attribute r

Returns:

  • (Object)

    the current value of r



2
3
4
# File 'lib/cheeky-pi/color.rb', line 2

def r
  @r
end

Class Method Details

.create(rgb) ⇒ Object

Create a new color from [r,g,b] array



29
30
31
# File 'lib/cheeky-pi/color.rb', line 29

def self.create(rgb)
  new *rgb
end

Instance Method Details

#blend(color, opacity) ⇒ Object

Blend with another color



20
21
22
23
24
25
26
# File 'lib/cheeky-pi/color.rb', line 20

def blend(color, opacity)
  self.class.new(
    self.r * (1 - opacity) + color.r * opacity,
    self.g * (1 - opacity) + color.g * opacity,
    self.b * (1 - opacity) + color.b * opacity
  )
end

#darken(percent) ⇒ Object

Darken a color by a percent



10
11
12
# File 'lib/cheeky-pi/color.rb', line 10

def darken(percent)
  blend(self.class.create(COLOR[:off]), percent)
end

#distance(color) ⇒ Object

Returns color distance (maximum of rgb distances individually) from a target color



15
16
17
# File 'lib/cheeky-pi/color.rb', line 15

def distance(color)
  [(self.r - color.r).abs, (self.g - color.g).abs, (self.b - color.b).abs].max
end

#lighten(percent) ⇒ Object

Lighten color by percent



5
6
7
# File 'lib/cheeky-pi/color.rb', line 5

def lighten(percent)
  blend(self.class.create(COLOR[:white]), percent)
end