""" The "onestr_rgbcolor" type handles RGB color values. It supports hexadecimal colors: * ``#FF0000`` * ``FF0000`` * ``#F00`` * ``F00`` RGB tuples or list (only from Python API for this one): * ``(255, 0, 0)`` * ``[255, 0, 0]`` and named colors: +------------+------------+-----------+-------------+ | ``white`` | ``red`` | ``lime`` | ``blue`` | +------------+------------+-----------+-------------+ | ``silver`` | ``maroon`` | ``green`` | ``navy`` | +------------+------------+-----------+-------------+ | ``gray`` | ``yellow`` | ``aqua`` | ``fuchsia`` | +------------+------------+-----------+-------------+ | ``black`` | ``olive`` | ``teal`` | ``purple`` | +------------+------------+-----------+-------------+ Device Profile -------------- Example of a onestr_rgbcolor value type in a device profile: :: profile = { # ... "settings": { "color": { "label": "Set color for all LEDs at once", "description": "Set color for all LEDs at once", "cli": ["-c", "--color"], "command": [0x21, 0xff, 0x03], "value_type": "onestr_rgbcolor", "led_count": 10, "default": "000000; 000000; 000000; 000000; 000000; 000000; 000000; 000000; 000000; 000000", }, }, # ... } CLI --- Example of CLI option generated with this handler:: -c COLOR, --color=COLOR Set the mouse backlight color (e.g. red, #ff0000, ff0000, #f00, f00, default: #FF1800) Example of CLI usage:: rivalcfg --color=red rivalcfg --color=ff0000 rivalcfg --color=f00 Functions --------- """ import argparse from ..color_helpers import is_color, parse_color_string def process_value(setting_info, color): """Called by the :class:`rivalcfg.mouse.Mouse` class when processing a "onestr_rgbcolor" type setting. :param dict setting_info: The information dict of the setting from the device profile. :param str,tuple,list color: The color. :rtype: [int, int, int] """ colores_parsed = [] colors_all = color.split("; ") #checks for number of color inputs against number of LEDs specified in Device File. input_count = (color.split("; ")) input_count = int(len(input_count)) if setting_info["led_count"] < (input_count): raise SystemExit("This Device only has %s" % setting_info["led_count"], "LEDs, so it only supports %s" % setting_info["led_count"], "values.") #for each item in the (colors_all) list, check if it is a valid color/input and if it is, parses it through color helper, then adds the lists in a new one. for colors_indiv in (colors_all): # Color tuple !!!!!!!!!!!!!UNTESTED!!!!!!!!!!!!! if isinstance(colors_indiv, (tuple, list)): if len(colors_indiv) != 3: raise ValueError("Not a valid color %s" % str(colors_indiv)) for channel in colors_indiv: if not isinstance(channel, int) or channel < 0 or channel > 255: raise ValueError("Not a valid color %s" % str(colors_indiv)) return list(colors_indiv) if not is_color(colors_indiv): raise ValueError("Not a valid color %s" % str(colors_indiv)) colores_parsed = colores_parsed + list(parse_color_string(colors_indiv)) #returns value of list to mouse.py file, wich builds the datapacket with it (at least i think this is hwi it works). return list(colores_parsed) class CheckColorAction(argparse.Action): """Validate colors from CLI""" def __call__(self, parser, namespace, value, setting_info, option_string=None): colors_all_2 = value.split("; ") for colors_indiv_2 in (colors_all_2): if not is_color(colors_indiv_2): raise argparse.ArgumentError(self, "invalid color: '%s'" % colors_indiv_2) setattr(namespace, self.dest.upper(), value) def add_cli_option(cli_parser, setting_name, setting_info): """Add the given "rgbcolor" type setting to the given CLI arguments parser. :param ArgumentParser cli_parser: An :class:`ArgumentParser` instance. :param str setting_name: The name of the setting. :param dict setting_info: The information dict of the setting from the device profile. """ description = "%s (e.g. red, #ff0000, ff0000, #f00, f00, default: %s)" % ( setting_info["description"], str(setting_info["default"]), ) cli_parser.add_argument( *setting_info["cli"], dest=setting_name, help=description, type=str, action=CheckColorAction, metavar=setting_name.upper(), )