Creating an SVGIcon using a valid svg xml with SVGSalamander

488 views Asked by At

I have an .xml document which is a valid SVG image.

I want to load this icon with different colors. But I could not.

As a solution firstly I read an xml file as a string and replaced colors using simple String.replace()

Now I must create an SVGIcon using my new XML content.

Is it posssible to do this with SVG Salamander library.

1

There are 1 answers

0
Anders Petersson On

With SVG Salamander:

Get the diagram from the cache and call a recursive search and replace:

SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri);
setStroke(Color.BLACK, getHexString(Color.GREEN), diagram.getRoot());

Code for the functions:

private void setStroke(Color fromColor, String toColor, SVGElement node) throws SVGException {
    if (node.hasAttribute("stroke", AnimationElement.AT_CSS)) {
        StyleAttribute abs = node.getStyleAbsolute("stroke");
        Color was = abs.getColorValue();
        if (was.equals(fromColor)) {
            abs.setStringValue(toColor);
        }
    }
    for (int i = 0; i < node.getNumChildren(); ++i) {
        setStroke(fromColor, toColor, node.getChild(i));
    }
}

private String getHexString(Color color) {
    return String.format("#%06x", (0xFFFFFF & color.getRGB()));
}