def _parse_color(node): item = KeyColor() ColorScheme._parse_dom_node_item(node, item) if node.hasAttribute("element"): item.element = node.attributes["element"].value if node.hasAttribute("rgb"): value = node.attributes["rgb"].value item.rgb = [hexstring_to_float(value[1:3])/255, hexstring_to_float(value[3:5])/255, hexstring_to_float(value[5:7])/255] if node.hasAttribute("opacity"): item.opacity = float(node.attributes["opacity"].value) state = {} ColorScheme._parse_state_attibute(node, "prelight", state) ColorScheme._parse_state_attibute(node, "pressed", state) ColorScheme._parse_state_attibute(node, "active", state) ColorScheme._parse_state_attibute(node, "locked", state) ColorScheme._parse_state_attibute(node, "insensitive", state) ColorScheme._parse_state_attibute(node, "scanned", state) item.state = state return item
def _parse_legacy_color_scheme(dom_node): """ Load a color scheme and return it as a new object. """ color_defaults = { "fill": [0.0, 0.0, 0.0, 1.0], "hovered": [0.0, 0.0, 0.0, 1.0], "pressed": [0.6, 0.6, 0.6, 1.0], "pressed-latched": [0.6, 0.6, 0.6, 1.0], "pressed-locked": [0.6, 0.6, 0.6, 1.0], "latched": [0.5, 0.5, 0.5, 1.0], "locked": [1.0, 0.0, 0.0, 1.0], "scanned": [0.45, 0.45, 0.7, 1.0], "stroke": [0.0, 0.0, 0.0, 1.0], "stroke-hovered": [0.0, 0.0, 0.0, 1.0], "stroke-pressed": [0.0, 0.0, 0.0, 1.0], "stroke-pressed-latched": [0.0, 0.0, 0.0, 1.0], "stroke-pressed-locked": [0.0, 0.0, 0.0, 1.0], "stroke-latched": [0.0, 0.0, 0.0, 1.0], "stroke-locked": [0.0, 0.0, 0.0, 1.0], "stroke-scanned": [0.0, 0.0, 0.0, 1.0], "label": [0.0, 0.0, 0.0, 1.0], "label-hovered": [0.0, 0.0, 0.0, 1.0], "label-pressed": [0.0, 0.0, 0.0, 1.0], "label-pressed-latched": [0.0, 0.0, 0.0, 1.0], "label-pressed-locked": [0.0, 0.0, 0.0, 1.0], "label-latched": [0.0, 0.0, 0.0, 1.0], "label-locked": [0.0, 0.0, 0.0, 1.0], "label-scanned": [0.0, 0.0, 0.0, 1.0], "dwell-progress": [0.82, 0.19, 0.25, 1.0], } items = [] # layer colors layers = dom_node.getElementsByTagName("layer") if not layers: # Still accept "pane" for backwards compatibility layers = dom_node.getElementsByTagName("pane") for i, layer in enumerate(layers): attrib = "fill" rgb = None opacity = None color = KeyColor() if layer.hasAttribute(attrib): value = layer.attributes[attrib].value color.rgb = [hexstring_to_float(value[1:3])/255, hexstring_to_float(value[3:5])/255, hexstring_to_float(value[5:7])/255] oattrib = attrib + "-opacity" if layer.hasAttribute(oattrib): color.opacity = float(layer.attributes[oattrib].value) color.element = "background" layer = Layer() layer.set_items([color]) items.append(layer) # key groups used_keys = {} root_key_group = None key_groups = [] for group in dom_node.getElementsByTagName("key_group"): # Check for default flag. # Default colors are applied to all keys # not found in the color scheme. default_group = False if group.hasAttribute("default"): default_group = bool(group.attributes["default"].value) # read key ids text = "".join([n.data for n in group.childNodes]) key_ids = [x for x in re.findall('\w+(?:[.][\w-]+)?', text) if x] # check for duplicate key definitions for key_id in key_ids: if key_id in used_keys: raise ValueError(_format("Duplicate key_id '{}' found " "in color scheme file. " "Key_ids must occur only once.", key_id)) used_keys.update(list(zip(key_ids, key_ids))) colors = [] for attrib in list(color_defaults.keys()): rgb = None opacity = None # read color attribute if group.hasAttribute(attrib): value = group.attributes[attrib].value rgb = [hexstring_to_float(value[1:3])/255, hexstring_to_float(value[3:5])/255, hexstring_to_float(value[5:7])/255] # read opacity attribute oattrib = attrib + "-opacity" if group.hasAttribute(oattrib): opacity = float(group.attributes[oattrib].value) if not rgb is None or not opacity is None: elements = ["fill", "stroke", "label", "dwell-progress"] for element in elements: if attrib.startswith(element): break else: element = "fill" if attrib.startswith(element): state_attrib = attrib[len(element):] if state_attrib.startswith("-"): state_attrib = state_attrib[1:] else: state_attrib = attrib color = KeyColor() color.rgb = rgb color.opacity = opacity color.element = element if state_attrib: color.state = {state_attrib : True} else: color.state = {} colors.append(color) key_group = KeyGroup() key_group.set_items(colors) key_group.key_ids = key_ids if default_group: root_key_group = key_group else: key_groups.append(key_group) if root_key_group: root_key_group.append_items(key_groups) items.append(root_key_group) return items