import gtk.gdk
import phue
import time

def pixel_at(x, y):
  rw = gtk.gdk.get_default_root_window()
  pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
  pixbuf = pixbuf.get_from_drawable(rw, rw.get_colormap(), x, y, 0, 0, 1, 1)
  return tuple(pixbuf.pixel_array[0, 0])

# begin healthbar @ 220, 1000
# end healthbar @ 440, 1000

Y = 1000
BEGIN_X = 220
END_X = 440

def get_health(ox, oy):
  for x in range(BEGIN_X, END_X):
    r, g, b = pixel_at(ox+x, oy+Y)

    if r == 30 and g == 18 and b == 40:
      return 0.0 # dead

    if g < 40:
      return 1.0 * (x - BEGIN_X) / (END_X - BEGIN_X) # missing health

  return 1.0 # full health


def main():
  bridge = phue.Bridge('192.168.1.144')
  bridge.connect()

  last_hp = None

  while True:
    hp = round(get_health(1920, 0)*100) / 100.0

    if hp == last_hp:
      continue

    last_hp = hp

    if hp <= 0.0:
      print 'you\'re a shitter'
      hue = 0.75 * 65536
      bri = 127
    else:
      print 'you\'re at %.2f' % hp
      bri = 254
      hue = int(hp * 65536 / 3.0)

    try:
      bridge.set_light(8, {'hue': hue, 'bri': bri}, transitiontime=0)
    except phue.PhueRequestTimeout:
      print 'timed out'
      pass

    time.sleep(0.1)

if __name__ == '__main__':
  main()

@@ -374,16 +374,55 @@
  = 440%0A%0A
+LIGHTS = 4%0AACTIVE_BRI = 0%0ADIM_BRI = 0%0A%0A
 def get_
@@ -882,26 +882,8 @@
 ue%0A%0A
-    last_hp = hp%0A%0A
     
@@ -928,16 +928,36 @@
 hitter'%0A
+      bri = DIM_BRI%0A
       hu
@@ -960,16 +960,20 @@
   hue = 
+int(
 0.75 * 6
@@ -980,24 +980,9 @@
 5536
-%0A      bri = 127
+)
 %0A   
@@ -1039,11 +1039,18 @@
 i = 
-254
+ACTIVE_BRI
 %0A   
@@ -1113,17 +1113,22 @@
 t_light(
-8
+LIGHTS
 , %7B'hue'
@@ -1165,16 +1165,35 @@
 time=0)%0A
+      last_hp = hp%0A
     exce
@@ -1252,14 +1252,54 @@
     
-  pass
+except Exception:%0A      print 'network sucked'
 %0A%0A  

Fix some small bugs