Ejemplo n.º 1
0
def read_stop_gradient(gradient):
    stop_data = {"id": gradient.attrib.get("id"), "stops": []}
    for stop in gradient:
        offset = stop.attrib.get("offset")
        style = Style(Style.parse_str(stop.attrib['style']))
        color = Color.parse_str(style.get("stop-color"))[1]
        opacity = style.get("stop-opacity")
        stop_data.get("stops").append(
            tuple([float(offset)] + [x / 256.0
                                     for x in color] + [float(opacity)]))
    return stop_data
Ejemplo n.º 2
0
 def get_selected_gradients_data(self):
     selected_objects = self.svg.selected
     gradient_list = []
     if len(selected_objects) > 0:
         for item in selected_objects:
             style = Style(
                 Style.parse_str(selected_objects.get(item).get('style')))
             fill = stroke = "None"
             if style.get("fill"):
                 fill = style.get("fill")[5:-1] if "url" in style.get(
                     "fill") else "None"
             if style.get("stroke"):
                 stroke = style("stroke")[5:-1] if "url" in style.get(
                     "stroke") else "None"
             if fill == "None" and stroke == "None":
                 continue
             # read fill data
             if "radialGradient" in fill or "linearGradient" in fill:
                 real_fill = self.svg.getElementById(fill).attrib[
                     "{" + NSS["xlink"] + "}href"][1:]
                 real_fill_node = self.svg.getElementById(real_fill)
                 if real_fill_node not in gradient_list:
                     gradient_list.append(real_fill_node)
             # read stroke data
             if "radialGradient" in stroke or "linearGradient" in stroke:
                 real_stroke = self.svg.getElementById(stroke).attrib[
                     "{" + NSS["xlink"] + "}href"][1:]
                 real_stroke_node = self.svg.getElementById(real_stroke)
                 if real_stroke_node not in gradient_list:
                     gradient_list.append(real_stroke_node)
     data = []
     # read gradients data
     for gradient in gradient_list:
         # parse gradient stops
         stop_data = read_stop_gradient(gradient)
         data.append(stop_data)
     return data