def get_random_color(): color_names = list(color_data.keys()) while True: random.shuffle(color_names) for name in color_names: raw_color = color_data[name] try: color = tuple(raw_color) yield color except Exception as e: print(e) import pdb pdb.set_trace() pass
def colorPage(type, path, headers, args): socketVal = {'r': 0, 'g':0, 'b':0} nodelist = [] random_mode = False change_speed = 6 change_speed_changed = False if args is not None: for arg in args.split("&"): argsp = arg.split('=') argkey = argsp[0].lower() argval = "" if len(argsp) > 1: argval = unquote(argsp[1]) argval = argval.replace('+', ' ') print "key: "+argkey+" val: "+argval if argkey == "red": socketVal['r'] = int(argval) elif argkey == "green": socketVal['g'] = int(argval) elif argkey == "blue": socketVal['b'] = int(argval) elif argkey == "speed": change_speed = int(argval) change_speed_changed = True elif argkey == "color": if colors.has_key(argval.lower()): socketVal = colors[argval.lower()] elif argkey == "node": nodelist.append(argval) elif argkey == "random": random_mode = True elif argkey == "quit": print "Stopping server" sys.exit() colorList = "" ckeys = colors.keys() ckeys.sort(cmp=lambda x,y:RGBtoHSV(colors[x]['r'],colors[x]['g'],colors[x]['b'])[0] - RGBtoHSV(colors[y]['r'],colors[y]['g'],colors[y]['b'])[0]) for c in ckeys: comps = colors[c] luma=comps['r']*.3 + comps['g']*.59 + comps['b']*.11 if luma < 128: textcolor = 'white' else: textcolor = 'black' colorList += "<DIV style=\"background-color:#%02x%02x%02x;color:%s;text-align:center;margin:2px 0px;cursor:pointer;\" onclick=\"setcolor(this)\">%s</DIV>" % (comps['r'], comps['g'], comps['b'], textcolor, c) return web_template % { 'red':socketVal['r'], 'green':socketVal['g'], 'blue':socketVal['b'],'speed':change_speed, 'nodes':[], 'colors':colorList }
def do_activate(self): all_colors_re = Colors_Hex_RE all_colors_re += r"|".join(colors.keys()) self.__colors_re = re.compile(all_colors_re, re.IGNORECASE) self.doc = self.view.get_buffer() # Connect signals to corresponding callbacks self.edit_handler = \ self.view.connect( 'notify::editable', self.do_language_notify ) self.lang_handler = \ self.doc.connect( 'notify::language', self.do_language_notify )
def colorPage(args): socketVal = {'r': 0, 'g':0, 'b':0, 'r2':0, 'g2':0, 'b2':0} nodelist = [] random_mode = False change_speed = 6 change_speed_changed = False if args is not None: ignrgb = False for arg in args.keys(): argkey = arg.lower() argval = args[arg] if argval is not None: argval = unquote(argval) print "key: "+argkey+" val: "+argval if not ignrgb and argkey == "red": socketVal['r'] = int(argval) elif not ignrgb and argkey == "green": socketVal['g'] = int(argval) elif not ignrgb and argkey == "blue": socketVal['b'] = int(argval) elif not ignrgb and argkey == "red2": socketVal['r2'] = int(argval) elif not ignrgb and argkey == "green2": socketVal['g2'] = int(argval) elif not ignrgb and argkey == "blue2": socketVal['b2'] = int(argval) elif argkey == "speed": change_speed = int(argval) change_speed_changed = True elif argkey == "color": if colors.has_key(argval.lower()): socketVal = colors[argval.lower()] for k in ('r','g','b'): socketVal[k+"2"] = socketVal[k] ignrgb = True elif argkey == "node": nodelist.append(argval) elif argkey == "random": random_mode = True elif argkey == "quit": print "Stopping server" sd.close() exitRequest = True sys.exit() return (digiweb.TextHtml, "exiting") if random_mode: socketdata = "n" else: if args is None or not args.has_key("red2"): socketdata = "A"+"".join([k+str(socketVal[k]) for k in ('r','g','b')]) else: try: socketdata = "F"+"".join([k+str(socketVal[k]) for k in ('r','g','b')]) except: pass try: socketdata += "C"+"".join([k[0]+str(socketVal[k]) for k in ('r2','g2','b2')]) except: pass if change_speed_changed: socketdata += "s"+str(change_speed) socketdata += "\n" print "sending "+socketdata+" to "+str(nodelist) + "\n" if 'all' in nodelist: nodelist = nodeData.keys() for node in nodelist: try: sd.sendto(socketdata, 0, (node, 0xe8, 0xc105, 0x11)) except: print "Failed to send to "+node nodeList = "" for node in nodeData.keys(): try: nodeline = "<OPTION value='"+node+"'" if node in nodelist: nodeline += " selected" nodeline += ">"+\ nodeData[node]['nodeId']+\ "</OPTION>" nodeList += nodeline except: pass colorList = "" ckeys = colors.keys() ckeys.sort() for c in ckeys: comps = colors[c] luma=comps['r']*.3 + comps['g']*.59 + comps['b']*.11 if luma < 128: textcolor = 'white' else: textcolor = 'black' colorEntry = "<DIV style=\"background-color:#%02x%02x%02x;color:%s;text-align:center;margin:2px 0px;cursor:pointer;\" onclick=\"setcolor(this)\">%s</DIV>" % (comps['r'], comps['g'], comps['b'], textcolor, c) colorList += colorEntry if len(nodelist) > 0: try: nodeaddr = nodelist[0] nodeData[nodeaddr]['red'] = socketVal['r'] nodeData[nodeaddr]['green'] = socketVal['g'] nodeData[nodeaddr]['blue'] = socketVal['b'] nodeData[nodeaddr]['red2'] = socketVal['r2'] nodeData[nodeaddr]['green2'] = socketVal['g2'] nodeData[nodeaddr]['blue2'] = socketVal['b2'] if change_speed_changed: nodeData[nodeaddr]['speed'] = change_speed except: exctype, value = sys.exc_info()[:2] print "failed to update nodeData: "+str(exctype)+", "+str(value) elif len(nodeData) > 0: nodeaddr = nodeData.keys()[0] socketVal = { 'r': nodeData[nodeaddr]['red'], 'g': nodeData[nodeaddr]['green'], 'b': nodeData[nodeaddr]['blue'], 'r2': nodeData[nodeaddr]['red2'], 'g2': nodeData[nodeaddr]['green2'], 'b2': nodeData[nodeaddr]['blue2'] } return (digiweb.TextHtml, web_template % { 'red':socketVal['r'], 'green':socketVal['g'], 'blue':socketVal['b'],'speed':change_speed, 'red2':socketVal['r2'], 'green2':socketVal['g2'], 'blue2':socketVal['b2'], 'nodes':nodeList, 'colors':colorList })
def returnValue(): nonlocal output output = optList.get() root.destroy() output = '' root = tk.Tk() root.geometry(f'+{600}+{300}') container = tk.Frame(root) label = ttk.Label(container, text=message, font=('Helvetica', 12, 'bold')) label.grid(row=0, column=0, columnspan=2, padx=4, pady=2) optList = ttk.Combobox(container, values=options) optList.grid(row=1, column=0, columnspan=2, padx=4, pady=4, sticky='we') cancelBtn = ttk.Button(container, text='Cancel', command=returnNone) cancelBtn.grid(row=2, column=0, padx=4, pady=4, sticky='w') okBtn = ttk.Button(container, text='Ok', command=returnValue) okBtn.grid(row=2, column=1, padx=4, pady=2, sticky='e') container.pack(padx=8, pady=8) root.mainloop() return output if __name__ == '__main__': a = askOptions('Choose the target color: ', tuple(colors.keys())) print(a)
def findNext(targ_mask): """Find the first pixel in a cv2 mask""" matchPxs = cv2.findNonZero(targ_mask) return matchPxs[0, 0] def click(x, y, delay): """Do a long click in the specified location""" pag.mouseDown(x, y, pause=0.2) pag.mouseUp(pause=delay) if __name__ == '__main__': if len(sys.argv) == 1: while True: targetColor = askOptions('Choose the target color: ', tuple(colors.keys())) if not targetColor: break elif targetColor not in colors.keys(): pag.alert("Invalid color. Try again.") continue sleep(0.5) mainApplication(targetColor) else: for targetColor in sys.argv[1:]: mainApplication(targetColor)