def handle(self): color = [] if self.args.color: color = parse_color(self.args.color, self.args) elif self.args.automatic: color = [util.get_x_color(verbose=self.args.verbose)] zones = parse_zones(self.args.zones) if not self.args.effect: effects = ['static'] if self.args.automatic and not self.args.brightness and len( zones) == 1: effects.append('brightness') else: effects = self.args.effect stop = len(zones) if len(effects) == 1 and stop > 1: while len(effects) < stop: effects.append(effects[0]) elif stop == 1 and len(effects) > 1: stop = len(effects) while len(zones) < stop: zones.append([*zones[0]]) self.setter.set(effects=effects, color=color, zones=zones)
def parse_color_argument(color, args: Namespace): if len(color) == 1: # Hex: Just one input argument or key word rgb = color[0].lower() if rgb == "random": rgb = util.get_random_color_rgb() elif rgb == "x": rgb = util.get_x_color(verbose=args.verbose) else: rgb = util.hex_to_decimal(rgb) else: if len(color) == 3: # RGB: Three base10 input arguments rgb = [] for i in color: if i.lower() == "rng": rgb.append(util.randint(0, 255)) else: rgb.append(int(i)) else: print("Unknown color input:", color) rgb = util.get_random_color_rgb() return rgb
def parse_color(color, args: Namespace): """ Set the color either from the input argument or use a fallback color """ RGB = [] if (color): # Set colors from input argument stop = len(color) i = 0 while i < stop: if len(color[i]) > 3 or color[i] in ['x', 'X']: if not len(color[i]) in [1, 6]: print('color', len(RGB) + 1, '(', color[i], ') looks to have a typo') RGB.append(parse_color_argument([color[i]], args)) i += 1 elif stop > i + 2: if len(color[i]) > 3 or len(color[i + 1]) > 3 or len( color[i + 2]) > 3: print('color', len(RGB) + 1, '(', color[i], color[i + 1], color[i + 2], ') looks to have a typo') rgb = [color[i], color[i + 1], color[i + 2]] RGB.append(parse_color_argument(rgb, args)) i += 3 else: print("Unexpected arguments for color") break else: # Use X colors as fallback if no color argument is set # TODO: Maybe also add argument to pull colors from # ~/.cache/wal.colors.jason RGB.append(util.get_x_color(verbose=args.verbose)) return RGB
def test_x_color(self) -> None: color = util.get_x_color() self.assertEqual(3, len(color))
def set_effect_to_device(self, device, effects, color, zones): # Known Effects: advanced, breath_dual, breath_random, breath_single, # breath_triple, blinking, none, reactive, ripple, ripple_random, # pulsate, spectrum, starlight_dual, starlight_random, # starlight_single, static, wave if self.args.verbose: debug_msg = {} og_color_len = len(color) c_used = 0 i = 0 stop = len(zones) if len(effects) < stop: stop = len(effects) if self.args.verbose: print(" Warning: Only", stop, "effects provided for", len(zones), "zones") while i < stop: arg = effects[i].split(',') effect = arg[0] if effect not in settings.EFFECTS and effect not in settings.CUSTOM_EFFECTS: print(" Warning:", effect, "is not a known effect.") arg.pop(0) used = 0 for zone in zones[i]: if self.args.verbose: if zone not in debug_msg: debug_msg[zone] = [] if not util.rgb_support(device, zone, effect): if self.args.verbose: if util.rgb_support(device, zone): debug_msg[zone].append( ['Does not support', effect]) else: debug_msg[zone].append([zone, 'is not supported']) continue if zone == 'generic': prop = device.fx else: if zone in ["scroll", "wheel"]: if self.args.verbose: debug_msg[zone].append( ['not valid, assuming scroll_wheel']) zone = "scroll_wheel" zones[i].append(zone) continue prop = getattr(device.fx.misc, zone) if og_color_len == 0 and effect not in settings.COLORLESS_EFFECTS and not ( effect == 'multicolor' and (len(arg) == 0 or arg[0] == '0')): # Lets get default colors # We can skip this if the effect does not need colors # * multicolor does NOT use consume colors if set to 0 (default) try: # hasattr will error anyway if does not work, no point testing it color = parse_color( util.bytes_array_to_hex_array( getattr(prop, 'colors')), self.args) except: color = [util.get_x_color(self.args.verbose)] c_used = 0 if self.args.verbose: debug_msg[zone].append( ["No color given, using:", color]) if effect not in settings.ALL_EFFECTS: debug_msg[zone].append([ "Sorry", effect, "is supported by the hardware, but this software does not\n", " Consider makeing a bug report:\n", " https://github.com/LoLei/razer-cli/issues" ]) continue # Prep colors used = 0 if effect not in settings.COLORLESS_EFFECTS: # Custom effects do this in there own block used = 1 if effect == 'breath_triple': used = 3 elif effect in ['breath_dual', 'starlight_dual']: used = 2 while len(color) < c_used + used: color.append(util.get_random_color_rgb()) # Do I really need this message? # if self.args.verbose: # debug_msg[zone].append(["Using color(s):", # [*color[c_used:c_used+used]], # "\n from:",color]) # Set Effect if effect in ['none', 'breath_random', 'spectrum']: # Effects that use no parameters if getattr(prop, effect)() and self.args.verbose: debug_msg[zone].append(["Setting to", effect]) elif effect in [ 'static', 'breath_single', 'blinking', 'pulsate' ]: # Effects that use 1 color rgb = color[c_used] if getattr(prop, effect)(*rgb) and self.args.verbose: debug_msg[zone].append( ["Setting", effect, "to:", *rgb]) elif effect == 'breath_dual': # Effects that use 2 colors rgb = color[c_used] rgb2 = color[c_used + 1] if getattr(prop, effect)(*rgb, * rgb2) and self.args.verbose: debug_msg[zone].append([ "Setting", effect, "to:\n [", *rgb, ']\n [', *rgb2, ']' ]) elif effect == 'breath_triple': # Effects that use 3 colors rgb = color[c_used] rgb2 = color[c_used + 1] rgb3 = color[c_used + 2] if getattr(prop, effect)(*rgb, *rgb2, * rgb3) and self.args.verbose: debug_msg[zone].append([ "Setting", effect, "to:\n [", *rgb, ']\n [', *rgb2, ']\n [', *rgb3, ']' ]) elif effect == 'reactive': # These are just numbers 1-4 # time = [razer_constants.REACTIVE_500MS, razer_constants.REACTIVE_1000MS, # razer_constants.REACTIVE_1500MS, razer_constants.REACTIVE_2000MS] if len(arg) == 0: time = util.randint(1, 4) else: time = int(arg[0]) if getattr(prop, effect)(*color[c_used], time) and self.args.verbose: debug_msg[zone].append([ "Setting", effect, "to:", *color[c_used], '@ timing level', time ]) elif effect == 'ripple': # Effects that use 1 color and a refresh rate if len(arg) == 0: refresh = razer_constants.RIPPLE_REFRESH_RATE else: refresh = float(arg[0]) rgb = color[c_used] if getattr(prop, effect)(*rgb, refresh) and self.args.verbose: debug_msg[zone].append(["Setting", effect, "to:", rgb]) elif effect == 'ripple_random': if len(arg) == 0: refresh = razer_constants.RIPPLE_REFRESH_RATE else: refresh = float(arg[0]) if getattr(prop, effect)(refresh) and self.args.verbose: debug_msg[zone].append(["Setting to", effect]) elif effect == 'starlight_single': # These are just numbers 1-3 # time = [razer_constants.STARLIGHT_FAST, razer_constants.STARLIGHT_NORMAL, # razer_constants.STARLIGHT_SLOW] if len(arg) == 0: time = util.randint(1, 3) else: time = int(arg[0]) if getattr(prop, effect)(*color[c_used], time) and self.args.verbose: debug_msg[zone].append([ "Setting", effect, "to:", *color[c_used], '@ timing level', time ]) elif effect == 'starlight_dual': # These are just numbers 1-3 # time = [razer_constants.STARLIGHT_FAST, razer_constants.STARLIGHT_NORMAL, # razer_constants.STARLIGHT_SLOW] if len(arg) == 0: time = util.randint(1, 3) else: time = int(arg[0]) if getattr(prop, effect)(*color[c_used], *color[c_used + 1], time) and self.args.verbose: debug_msg[zone].append([ "Setting", effect, " @ timing level", time, "to:\n [", *color[c_used], ']\n [', *color[c_used + 1] ]) elif effect == 'starlight_random': if len(arg) == 0: time = util.randint(1, 3) else: time = int(arg[0]) if getattr(prop, effect)(time) and self.args.verbose: debug_msg[zone].append( ["Setting", effect, '@ timing level', time]) elif effect == 'wave': # These are just numbers 1-2 # direction=[razer_constants.WAVE_LEFT, razer_constants.WAVE_RIGHT] if len(arg) == 0: direction = util.randint(1, 2) else: direction = int(arg[0]) if getattr(prop, effect)(direction) and self.args.verbose: debug_msg[zone].append( ["Setting", effect, ' in direction', direction]) elif effect == 'brightness': if len(arg) == 0: b = 100 else: b = int(arg[0]) prop.brightness = b if self.args.verbose: debug_msg[zone].append(["Setting", effect, 'to', b]) elif effect == 'active': if len(arg) == 0: b = True else: b = bool(arg[0]) prop.active = b if self.args.verbose: debug_msg[zone].append(["Setting", effect, 'to', b]) elif effect == 'multicolor': if len(arg) > 0: used = int(arg[0]) else: used = 0 while len(color) < c_used + used: color.append(util.get_random_color_rgb()) cols = prop.advanced.cols rows = prop.advanced.rows if used == 0: colors_to_dist = [ util.get_random_color_rgb() for _ in range(cols * rows) ] else: colors_to_dist = [] end = len(color) start = c_used while start < end: colors_to_dist.append(color[start]) start += 1 try: counter = 0 for row in range(rows): for col in range(cols): prop.advanced.matrix.set( row, col, colors_to_dist[counter % len(colors_to_dist)]) counter += 1 # prop.advanced.draw_fb_or() prop.advanced.draw() if self.args.verbose: debug_msg[zone].append([ "Setting", effect, "\n ", colors_to_dist ]) except (AssertionError, ValueError) as e: if self.args.verbose: debug_msg[zone].append(["Warning:", str(e)]) else: debug_msg[zone].append([ "Looks like someone forget to program a action for", effect, "after adding it to the settings file" ]) c_used += used i += 1 if self.args.verbose: for i in debug_msg: print(' ' + i + ':') for x in debug_msg[i]: print(' ', *x) # Save used settings for this device to a file if og_color_len == 0: color = [] util.write_settings_to_file(device, effects, color, zones=zones)