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') }
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 = 60 threshMax = 200 counter = 0 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 counter+= 1 if counter > 0: rAvg = r/counter gAvg = g/counter bAvg = b/counter converter = Converter() hueColor = converter.rgbToCIE1931(rAvg, gAvg, bAvg) return hueColor else: print('problem') return (0,0)
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 __init__(self, light_id): self._id = light_id self.transitiontime = 0 self.converter = Converter() self.has_cache = False self.previous_state = light_state( self.get_brightness(), self.get_color_xy()) self.actual_state = light_state( self.previous_state.brightness, self.previous_state.xy) self.has_cache = True self.has_color = self.get_color_xy() is not None
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 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:
#----------------------------------------------------------------------------------------------------------------------- try: from Tkinter import * except ImportError: from tkinter import * #----------------------------------------------------------------------------------------------------------------------- # Import library for Hue lights #----------------------------------------------------------------------------------------------------------------------- from phue import Bridge #----------------------------------------------------------------------------------------------------------------------- from rgb_cie import Converter converter = Converter() converter.rgbToCIE1931(255,0,0) #See https://github.com/benknight/hue-python-rgb-converter # Import time library for creating delays in scroll function #----------------------------------------------------------------------------------------------------------------------- import time #----------------------------------------------------------------------------------------------------------------------- # Toggle variables for each button #----------------------------------------------------------------------------------------------------------------------- toggle1 = 0 toggle2 = 0 toggle3 = 0
import sys import os from qhue import Bridge from rgb_cie import Converter f = open("hue_id.txt", "r") hue_id = f.readlines()[-1].rstrip() f.close() b = Bridge(hue_id, "elbLovRPUcHaqss904iEJMH9LZrRwsvFeOKSfvOP") lights = b.lights converter = Converter() b.lights(1, 'state', on=True, xy=converter.hexToCIE1931(sys.argv[-4])) b.lights(2, 'state', on=True, xy=converter.hexToCIE1931(sys.argv[-3])) b.lights(3, 'state', on=True, xy=converter.hexToCIE1931(sys.argv[-2])) b.lights(4, 'state', on=True, xy=converter.hexToCIE1931(sys.argv[-1]))
import sys from qhue import Bridge from rgb_cie import Converter converter = Converter() b = Bridge("192.168.0.101", "elbLovRPUcHaqss904iEJMH9LZrRwsvFeOKSfvOP") strip_bri = b.lights[1]()['state']['bri'] bloom_1_bri = b.lights[2]()['state']['bri'] bloom_2_bri = b.lights[3]()['state']['bri'] strip_2_bri = b.lights[4]()['state']['bri'] strip_xy = b.lights[1]()['state']['xy'] bloom_1_xy = b.lights[2]()['state']['xy'] bloom_2_xy = b.lights[3]()['state']['xy'] strip_2_xy = b.lights[4]()['state']['xy'] strip_hex = converter.CIE1931ToHex(strip_xy[0], strip_xy[1], bri=(strip_bri / 254.0)) bloom_1_hex = converter.CIE1931ToHex(bloom_1_xy[0], bloom_1_xy[1], bri=(bloom_1_bri / 254.0)) bloom_2_hex = converter.CIE1931ToHex(bloom_2_xy[0], bloom_2_xy[1], bri=(bloom_2_bri / 254.0)) strip_2_hex = converter.CIE1931ToHex(strip_2_xy[0], strip_2_xy[1], bri=(strip_2_bri / 254.0)) print([
class hue_light(object): MIN_BRIGHTNESS = 1 MAX_BRIGHTNESS = 254 _request_string = 'lights/' def __init__(self, light_id): self._id = light_id self.transitiontime = 0 self.converter = Converter() self.has_cache = False self.previous_state = light_state( self.get_brightness(), self.get_color_xy()) self.actual_state = light_state( self.previous_state.brightness, self.previous_state.xy) self.has_cache = True self.has_color = self.get_color_xy() is not None def _api_request(self, method, request='', payload=None): if isinstance(payload, str): payload = json.loads(payload) if payload: payload['transitiontime'] = self.transitiontime return hue_api.request( method, hue_light._request_string + str(self._id) + '/' + request, payload) def is_reachable(self): return self._api_request('get')['state']['reachable'] def is_on(self): return self._api_request('get')['state']['on'] def turn_on(self): if not self.is_on(): self._api_request('put', 'state', {"on": True}) def turn_off(self): if self.is_on(): self._api_request('put', 'state', {"on": False}) def get_brightness(self): if self.has_cache: return self.actual_state.brightness return self._api_request('get')['state']['bri'] def set_brightness(self, brightness): if brightness == 'max': brightness = hue_light.MAX_BRIGHTNESS if brightness >= hue_light.MIN_BRIGHTNESS\ and brightness <= hue_light.MAX_BRIGHTNESS: self._save_brightness() self.actual_state.brightness = brightness self._api_request('put', 'state', {"bri": brightness}) def get_color_xy(self): if self.has_cache: return self.actual_state.xy state = self._api_request('get')['state'] if 'xy' in state: return self._api_request('get')['state']['xy'] else: return None def set_color_xy(self, xy): self._save_color_xy() self.actual_state.xy = xy self._api_request('put', 'state', {'xy': xy}) def set_color_rgb(self, rgb): self.set_color_r_g_b(rgb[0], rgb[1], rgb[2]) def set_color_r_g_b(self, red, green, blue): if not is_valid_rgb(red, green, blue): return self._save_color_xy() xy = self.converter.rgbToCIE1931(red, green, blue) self.actual_state.xy = xy self._api_request('put', 'state', {"xy": xy}) def revert_to_previous_state(self): if self.previous_state: self.set_color_xy(self.previous_state.xy) self.set_brightness(self.previous_state.brightness) self.actual_state = self.previous_state.copy() self.previous_state = None def _save_brightness(self): self.previous_state.brightness = self.actual_state.brightness def _save_color_xy(self): self.previous_state.xy = self.actual_state.xy
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)