class HueColoredBulb

Exposes basic on, off, and recolor commands for a single named Hue bulb

on the local network.

Attributes

client[R]

The named light itself and the Hue client object are both worth reusing

as instance variables
light[R]

The named light itself and the Hue client object are both worth reusing

as instance variables

Public Class Methods

new(name='Bloom') click to toggle source

Note that the initializer only cares about a single named bulb

and does not look around for other bulbs to care about.
# File lib/lita/hue_colored_bulb.rb, line 11
def initialize(name='Bloom')
  @client = Hue::Client.new

  # Your client likely has multiple bulbs attached, but here you're only
  #   going to want to find a single bulb that matches the supplied name.
  @light = @client.lights.select do |light|
    light.name == name
  end.first

  # No point continuing if the bulb can't be found by name.
  raise ArgumentError if @light.nil?
end

Public Instance Methods

colors() click to toggle source
# File lib/lita/hue_colored_bulb.rb, line 77
def colors
  [
    'red', 'orange', 'yellow',           # red is 0
    'chartreuse', 'green', 'aquamarine', # green is 21,000
    'cyan', 'azure', 'blue',             # blue is 44,000
    'violet', 'magenta', 'rose'          # rose is about 60,000
  ]
end
demo(sleep_seconds=0.25) click to toggle source

Fun demo to spin through all named colors, one color every quarter second.

# File lib/lita/hue_colored_bulb.rb, line 41
def demo(sleep_seconds=0.25)
  colors.each do |color_name|
    self.set_color color_name
    sleep sleep_seconds
  end
end
hue_for_color(name) click to toggle source

START:color_backend RGB color wheel from 0 to 65535:

red is 0 (and 65535 because the wheel starts over at the end)
green is ~21000
blue is ~44000
# File lib/lita/hue_colored_bulb.rb, line 102
def hue_for_color(name)
  # green has an index of 4 in the colors array above
  color_index = colors.find_index(name)

  # each color is 65535 / 12 points "wide",
  #   which is 5461 points on this RGB color wheel.
  color_width = (max_color / colors.count).to_i

  # green's hue is thus 4 * 5461 = 21845.
  color_index * color_width
end
off!() click to toggle source
# File lib/lita/hue_colored_bulb.rb, line 36
def off!
  light.off!
end
on!() click to toggle source

START:basics on! and off! methods are passed right through this API. They're plenty

simple for your purposes as is.
# File lib/lita/hue_colored_bulb.rb, line 32
def on!
  light.on!
end
set_color(name) click to toggle source

Take a color name like cyan, look up its hue on the 65000 point scale,

and pass that number to the light's hue= method to recolor the light.
# File lib/lita/hue_colored_bulb.rb, line 88
def set_color(name)
  unless colors.include? name.downcase
    raise ArgumentError.new("I don't know that color!")
  end

  light.hue = hue_for_color(name)
end

Private Instance Methods

max_color() click to toggle source

The hue gem has a built-in constant range to track the number of distinct

color hues the system exposes for a given colored bulb, i.e. 2^16 or
"16-bit" color.
# File lib/lita/hue_colored_bulb.rb, line 118
def max_color
  # 0..65535
  Hue::Light::HUE_RANGE.last
end