xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bishop, Michael W. CONTR J9C880" <Michael.Bis...@je.jfcom.mil>
Subject RE: java.awt.Color ->SVG
Date Mon, 02 Apr 2007 14:16:50 GMT
java.awt.Color -> SVG:

    public static Map convertColor(Document document, Color color) {
        final SVGGeneratorContext svgContext =
            SVGGeneratorContext.createDefault(document);
        final SVGColor svgColor = new SVGColor(svgContext);

        return svgColor.toSVG(color, svgContext).getAttributeMap(null);
    }

SVG -> java.awt.Color (attribute is the attribute of the element you
want to get a color value for):

    public static Color getColor(Element element, String attribute) {
        Color returnColor = null;
        final Document document = element.getOwnerDocument();
        final ViewCSS viewCSS = (ViewCSS) document.getDocumentElement();
        final CSSStyleDeclaration computedStyle =
            viewCSS.getComputedStyle(element, null);
        final SVGPaint svgPaint =
            (SVGPaint) computedStyle.getPropertyCSSValue(attribute);

        if (svgPaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR)
{
            final RGBColor rgb = svgPaint.getRGBColor();
            final float red =
 
rgb.getRed().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
            final float green =
 
rgb.getGreen().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
            final float blue =
 
rgb.getBlue().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
            returnColor = new Color(red / 255, green / 255, blue / 255);
        }

        return returnColor;
    }

Michael Bishop

-----Original Message-----
From: Daniel Meyer [mailto:daniel.meyer@pt.lu] 
Sent: Monday, April 02, 2007 9:37 AM
To: batik-users@xmlgraphics.apache.org
Subject: java.awt.Color ->SVG

Hi,

What is the best way to convert back and forth from
java.awt.Color instances to SVG attributes.

Thanks a lot!
Daniel

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Mime
View raw message