def _create_colors(self): c = Converter() self.colors = { 'red': (c.rgbToCIE1931(1, 0, 0), 'xy'), 'green': (c.rgbToCIE1931(0, 1, 0), 'xy'), 'blue': (c.rgbToCIE1931(0, 0, 1), 'xy'), 'yellow': (c.rgbToCIE1931(1, 1, 0), 'xy'), 'orange': (c.rgbToCIE1931(1, 0.49, 0), 'xy'), 'white': (353, 'ct') }
class Lamp(): converter = Converter() def __init__(self, lampname): self.name = lampname self.bridge = qhue.Bridge(ip, username) self.lampid = get_lamp_id_by_name(self.bridge, lampname) self.url = self.bridge.lights[self.lampid].url + '/state' self.on = self.bridge.lights[self.lampid]()['state']['on'] self.xy = self.bridge.lights[self.lampid]()['state']['xy'] self.brightnessPercent = self.bridge.lights[self.lampid]()['state']['bri'] / 255.0 def set_color_rgb(self, r, g, b, brightnessPercent = None): xy = self.converter.rgbToCIE1931(r, g, b) self.set_color_xy(xy, brightnessPercent) def set_color_xy(self, xyval, brightnessPercent = None): xdiff = math.fabs(xyval[0] - self.xy[0]) ydiff = math.fabs(xyval[1] - self.xy[1]) color_diffpercent = (xdiff + ydiff) / 1.0 if brightnessPercent is None: brightnessPercent = self.brightnessPercent brightness_diffpercent = math.fabs(self.brightnessPercent - brightnessPercent) if color_diffpercent < change_treshold and brightness_diffpercent < change_treshold: return if brightnessPercent < 0.05: requests.put(self.url, data='{ "on": false, "transitiontime" : 1}') self.on = False else: if self.on == True: requests.put(url=self.url, data='{ "xy": [' + str(xyval[0]) + ', ' + str(xyval[1]) + '], "bri": ' + str(int(brightnessPercent * 255)) + ', "transitiontime": 1}') else: requests.put(self.url, data='{ "xy": [' + str(xyval[0]) + ', ' + str(xyval[1]) + '], "bri": ' + str(int(brightnessPercent * 255)) + ', "on": true, "transitiontime": 1}') self.on = True self.xy = xyval time.sleep(0.1)#wait for transition to complete
def _filter_colors(self, color_dict, skip_colors=[]): # scan through available colors and look for something not black or grey # return a grey if we must though color_converter = Converter() color_selected = False index_col = 0 keys = color_dict.keys() total_colors = len(keys) save_for_later = -1 while not color_selected: if 'tan' in color_dict[keys[index_col]]: if save_for_later == -1: save_for_later = index_col index_col += 1 elif 'brown' in color_dict[keys[index_col]]: if save_for_later == -1: save_for_later = index_col index_col += 1 elif 'white' in color_dict[keys[index_col]]: if save_for_later == -1: save_for_later = index_col index_col += 1 elif 'black' in color_dict[keys[index_col]]: if save_for_later == -1: save_for_later = index_col index_col += 1 elif 'grey' in color_dict[keys[index_col]] or 'gray' in color_dict[ keys[index_col]]: if save_for_later == -1: save_for_later = index_col index_col += 1 elif color_dict[keys[index_col]] in skip_colors: if save_for_later == -1: save_for_later = index_col index_col += 1 else: return keys[index_col], color_dict[keys[index_col]] if index_col == total_colors: color_selected = True return keys[save_for_later], color_dict[keys[save_for_later]]
def set_light_color(self, light, color): color_converter = Converter() if type(light) is phue.Light: light.on = True if type(color) is list: light.xy = color else: try: light.xy = color_converter.hexToCIE1931(color) except socket.timeout: pass light.brightness = 254 elif type(light) is WirelessLights: light.on() if type(color) is list: # limitless can't handle this light.white() elif type(color) is int: light.setColorInt(color) else: light.setColorHSL(self.convert_color(color)) light.setBrightness(100)
def control(vled, bri): xy = Converter().rgbToCIE1931(vled[0], vled[1], vled[2]) l.xy(xy[0], xy[1]) l.bri(bri) print('Signal sended')
twitch_bot_utils.printer("Colors: %d %d %d" % (int("0x"+value[0:2],0), int("0x"+value[2:4],0), int("0x"+value[4:6],0))) twitch_bot_utils.printer(color) sendToAll("{ \"bri\": 254, \"xy\":%s }" % color) user_wait(light_length) set_animating(0) modedefault() return True #constants auth = twitch_auth.auth() auth.add_admin("spiffbot") master = auth.get_streamer() user_stack = [] set_animating(0) stayAlive = 1 converter = Converter() light_length = 15 irc = twitch_bot_utils.irc_connection("irc.twitch.tv","6667",auth.get_bot(),auth.get_oauth(), auth.get_streamer(),[admin_commands,user_commands]) twitch_bot_utils.printer("READY!") irc.msg("READY!") user_stack_thread = threading.Thread(target=user_stack_consumer) user_stack_thread.daemon = True user_stack_thread.start() #Main loop while stayAlive:
def getPixels(): #grab screenshot and get the size image = ImageGrab.grab() im = image.load() maxX, maxY = image.size step = 100 #loop through pixels for rgb data data = [] for y in range(0, maxY, step): for x in range(0, maxX, step): pixel = im[x,y] data.append(pixel) #loop and check for white/black to exclude from averaging r = 0 g = 0 b = 0 threshMin = 30 threshMax = 255 counter = 0 counter_2 = 0 r_0 = 0 g_0 = 0 b_0 = 0 r_1 = 0 g_1 = 0 b_1 = 0 r_2 = 0 g_2 = 0 b_2 = 0 r_3 = 0 g_3 = 0 b_3 = 0 chunks = len(data)/4 for z in range(len(data)): rP, gP, bP = data[z] if rP > threshMax and gP > threshMax and bP > threshMax or rP < threshMin and gP < threshMin and bP < threshMin: pass else: r+= rP g+= gP b+= bP if counter_2 > chunks * 3: r_0 += rP g_0 += gP b_0 += bP elif counter_2 > chunks * 2: r_1 += rP g_1 += gP b_1 += bP elif counter_2 > chunks: r_2 += rP g_2 += gP b_2 += bP elif counter_2 > 0: r_3 += rP g_3 += gP b_3 += bP counter+= 1 counter_2+= 1 if counter > 0: rAvg = r/counter gAvg = g/counter bAvg = b/counter rAvg_0 = r_0/(counter/4) gAvg_0 = g_0/(counter/4) bAvg_0 = b_0/(counter/4) rAvg_1 = r_1/(counter/4) gAvg_1 = g_1/(counter/4) bAvg_1 = b_1/(counter/4) rAvg_2 = r_2/(counter/4) gAvg_2 = g_2/(counter/4) bAvg_2 = b_2/(counter/4) rAvg_3 = r_3/(counter/4) gAvg_3 = g_3/(counter/4) bAvg_3 = b_3/(counter/4) converter = Converter() request_string = "http://192.168.0.110/ambient_loop?r="+str(rAvg)+'&g='+str(gAvg)+'&b='+str(bAvg) request_string_0 = "http://192.168.0.110/ambient_loop?r="+str(rAvg_0)+'&g='+str(gAvg_0)+'&b='+str(bAvg_0) request_string_1 = "http://192.168.0.110/ambient_loop?r="+str(rAvg_1)+'&g='+str(gAvg_1)+'&b='+str(bAvg_1) request_string_2 = "http://192.168.0.110/ambient_loop?r="+str(rAvg_2)+'&g='+str(gAvg_2)+'&b='+str(bAvg_2) request_string_3 = "http://192.168.0.110/ambient_loop?r="+str(rAvg_3)+'&g='+str(gAvg_3)+'&b='+str(bAvg_3) print(request_string) print(request_string_0) print(request_string_1) print(request_string_2) print(request_string_3) urllib.request.urlopen(request_string).read() # hueColor = converter.rgbToCIE1931(rAvg, gAvg, bAvg) return 'meow' else: print('problem') return (0,0)