예제 #1
0
    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
예제 #2
0
    def load_keys_geometry(self, svgdoc, color_scheme, keys, label_rgba):
        for rect in svgdoc.getElementsByTagName("rect"):
            id = rect.attributes["id"].value

            styleString = rect.attributes["style"].value
            result = re.search("(fill:#\d?\D?\d?\D?\d?\D?\d?\D?\d?\D?\d?\D?;)", styleString).groups()[0]

            rgba = [
                hexstring_to_float(result[6:8]) / 255,
                hexstring_to_float(result[8:10]) / 255,
                hexstring_to_float(result[10:12]) / 255,
                1,
            ]  # not bothered for now

            key = RectKey(
                id,
                (float(rect.attributes["x"].value), float(rect.attributes["y"].value)),
                (float(rect.attributes["width"].value), float(rect.attributes["height"].value)),
                rgba,
            )

            # print color_scheme.get_key_label_rgba(name)

            # old colors for backwards compatibility
            key.hover_rgba = rgba
            key.pressed_rgba = rgba
            key.latched_rgba = [0.5, 0.5, 0.5, 1]
            key.locked_rgba = [1, 0, 0, 1]
            key.scanned_rgba = [0.45, 0.45, 0.7, 1]
            key.stroke_rgba = [0.0, 0.0, 0.0, 1.0]
            key.label_rgba = label_rgba

            # get colors from color scheme
            if color_scheme:
                key.rgba = color_scheme.get_key_rgba(id, "fill")
                key.hover_rgba = color_scheme.get_key_rgba(id, "hover")
                key.pressed_rgba = color_scheme.get_key_rgba(id, "pressed")
                key.latched_rgba = color_scheme.get_key_rgba(id, "latched")
                key.locked_rgba = color_scheme.get_key_rgba(id, "locked")
                key.scanned_rgba = color_scheme.get_key_rgba(id, "scanned")
                key.stroke_rgba = color_scheme.get_key_rgba(id, "stroke")
                key.label_rgba = color_scheme.get_key_rgba(id, "label")

            keys[id] = key

            # TODO fix LineKeys
            """
예제 #3
0
    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
예제 #4
0
    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
예제 #5
0
    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(_("Duplicate key_id '{}' found "
                      "in color scheme file. "
                      "Key_ids must occur only once."
                     .format(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
예제 #6
0
    def load(filename, system=False):

        color_scheme = None

        f = open(filename)
        try:
            cp = configparser.SafeConfigParser()
            domdoc = minidom.parse(f).documentElement
            try:
                color_scheme = ColorScheme()

                color_scheme.name = domdoc.attributes["name"].value

                # pane colors
                for i,pane in enumerate(domdoc.getElementsByTagName("pane")):
                    if pane.hasAttribute("fill"):
                        value = pane.attributes["fill"].value
                        rgba = [hexstring_to_float(value[1:3])/255,
                        hexstring_to_float(value[3:5])/255,
                        hexstring_to_float(value[5:7])/255,
                        1]
                        color_scheme.pane_fill_color[i] = rgba

                    if pane.hasAttribute("fill-opacity"):
                        value = float(pane.attributes["fill-opacity"].value)
                        color_scheme.pane_fill_opacity[i] = value

                # key colors
                for group in domdoc.getElementsByTagName("key_group"):

                    # 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])
                    ids = [x for x in re.split('\W+', text) if x]

                    key_defaults = color_scheme.key_defaults
                    key_colors   = color_scheme.key_colors

                    for attrib in key_defaults.keys():

                        # 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]

                            if default_group:
                                value = key_defaults[attrib]
                                key_defaults[attrib] = rgb + value[3:4]
                            else:
                                for key_id in ids:
                                    colors = key_colors.get(key_id, {})
                                    value = colors.get(attrib, [.0,.0,.0])
                                    colors[attrib] = rgb + value[3:4]
                                    key_colors[key_id] = colors

                        # read opacity attribute
                        oattrib = attrib + "-opacity"
                        if group.hasAttribute(oattrib):
                            opacity = float(group.attributes[oattrib].value)
                            if default_group:
                                value = key_defaults[attrib]
                                key_defaults[attrib] = value[:3] + [opacity]
                            else:
                                for key_id in ids:
                                    colors = \
                                       key_colors.get(key_id, {})
                                    value = key_colors.get(attrib, [.0])
                                    if len(value) == 1:
                                        colors[attrib] = [opacity]
                                    else:
                                        colors[attrib] = value[:3] + [opacity]
                                    key_colors[key_id] = colors

                    # read main opacity setting
                    # applies to all colors that don't have their own opacity
                    if group.hasAttribute("opacity"):
                        value = float(group.attributes["opacity"].value)
                        if default_group:
                            color_scheme.default_key_opacity = value
                        else:
                            for key_id in ids:
                                color_scheme.key_opacity[key_id] = value

                color_scheme.filename = filename
                color_scheme.system = system

            except Exception, (exception):
                raise Exceptions.ColorSchemeFileError(_("Error loading ")
                    + filename, chained_exception = exception)
            finally:
                domdoc.unlink()
예제 #7
0
    def parse_path(self, path, pane):
        id = path.attributes["id"].value
        styleString = path.attributes["style"].value
        result = re.search("(fill:#\d?\D?\d?\D?\d?\D?\d?\D?\d?\D?\d?\D?;)", styleString).groups()[0]

        rgba = (
            hexstring_to_float(result[6:8]) / 255,
            hexstring_to_float(result[8:10]) / 255,
            hexstring_to_float(result[10:12]) / 255,
            1,
        )  # not bothered for now

        dList = path.attributes["d"].value.split(" ")
        dList = dList[1:-2]  # trim unwanted M, Z
        coordList = []

        transformMatrix = None
        if path.hasAttribute("transform"):
            transform = path.attributes["transform"].value
            if transform.startswith("matrix"):
                # Convert strings to floats
                transformCoords = map(float, transform[7:-1].split(","))

                transformMatrix = (
                    (transformCoords[0], transformCoords[2], transformCoords[4]),
                    (transformCoords[1], transformCoords[3], transformCoords[5]),
                    (0, 0, 1),
                )
            elif transform.startswith("translate"):
                transformCoords = map(float, transform[10:-1].split(","))

                transformMatrix = ((1.0, 0.0, transformCoords[0]), (0.0, 1.0, transformCoords[1]), (0.0, 0.0, 1.0))
            else:
                print "Warning: Unhandled transform " + transform

        xTotal = 0.0
        yTotal = 0.0
        numCoords = 0
        for d in dList:
            l = d.split(",")
            if len(l) == 1:
                # A letter
                coordList.append(l)
            else:
                # A coord
                numCoords = numCoords + 1

                l = map(float, l)

                if transformMatrix:
                    l = matmult(transformMatrix, l + [1])[:-1]

                xTotal = xTotal + l[0]
                yTotal = yTotal + l[1]

                coordList.append(l[0])
                coordList.append(l[1])

        # Point at which we want the label drawn
        fontCoord = (xTotal / numCoords, yTotal / numCoords)

        return LineKey(pane, coordList, fontCoord, rgba)