class RPiRGB

see also: 500+ Named Colours with rgb and hex values www.cloford.com/resources/colours/500col.htm

Attributes

blue[R]
brightness[RW]
green[R]
red[R]

Public Class Methods

new(pins, brightness: 100, smooth: false, presets: {}, \ transition_time: 1.5) click to toggle source
# File lib/rpi_rgb.rb, line 41
def initialize(pins, brightness: 100, smooth: false, presets: {}, \
               transition_time: 1.5)
  
  @red, @green, @blue = pins.map do |x| 
    RPiLed.new(x, brightness: brightness, smooth: smooth, \
               transition_time: transition_time)
  end
  
  @brightness, @presets = brightness, presets
  @transition_time = transition_time
  @old_brightness = @brightness

end

Public Instance Methods

brightness=(val) click to toggle source
# File lib/rpi_rgb.rb, line 55
def brightness=(val)
  
  @brightness = val
  [@red, @green, @blue].each do |led|
    b = led.brightness
    led.brightness = val if b > 0
  end
  
end
color()
Alias for: colour
color=(val)
Alias for: colour=
colour() click to toggle source
# File lib/rpi_rgb.rb, line 91
def colour()
  @colour
end
Also aliased as: color
colour=(val) click to toggle source
# File lib/rpi_rgb.rb, line 65
def colour=(val)
  
  @colour = val
  
  if val.is_a? String then
    
    return mix( RGB::Color.from_rgb_hex(val).to_rgb) if val =~ /^#/
    
    h = {red: '#FF0000', green: '#00FF00', blue: '#0000FF', white: '#FFFFFF'}
            
    color = h.merge(@presets)[val.to_sym]
    
    if color then
      
      if color =~ /^#/ then
        mix( RGB::Color.from_rgb_hex(color).to_rgb) 
      else
        mix color
      end
    end
    
  elsif val.is_a? Array
    mix val
  end
end
Also aliased as: color=
mix(*rgb_values) click to toggle source
# File lib/rpi_rgb.rb, line 98
def mix(*rgb_values)    
  
  values = rgb_values.flatten.map {|x| (x * 0.392).round }
  
  r, g, b = if values.length < 4 then
    
    self.brightness = @old_brightness
    values.map {|v| v / (100.0 / @brightness ) }
    
  else
    
    @old_brightness = @brightness
    self.brightness = values.pop
    sleep @transition_time if @smooth
    values
    
  end
  
  [@red, @green, @blue].each(&:on)
  
  red.brightness, green.brightness, blue.brightness = r, g, b
end
off(durationx=nil, duration: nil) click to toggle source
# File lib/rpi_rgb.rb, line 125
def off(durationx=nil, duration: nil)
  [@red, @green, @blue].each{|x| x.off(durationx, duration: duration) }
end
on(durationx=nil, duration: nil) click to toggle source
# File lib/rpi_rgb.rb, line 129
def on(durationx=nil, duration: nil)
  [@red, @green, @blue].each{|x| x.on(durationx, duration: duration) }
end