Here's what I use. It's very similar. I think this came from the list
as well:
/**
* Returns the color of the given attribute in the given element.
*
* @param element An element with color attributes.
* @param attribute The name of the attribute (fill, stroke, etc.)
*
* @return An instance of Color.
*/
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: Ludwig Reiter [mailto:[EMAIL PROTECTED]
Sent: Monday, February 05, 2007 5:37 AM
To: [email protected]
Subject: Re: Parsing svg color to java Color
Hi,
You could use the following sample
public static final Color getColor(SVGColor svgColor) {
Color color = null;
RGBColor rgbColor = svgColor.getRGBColor();
CSSPrimitiveValue redValue = rgbColor.getRed();
CSSPrimitiveValue blueValue = rgbColor.getBlue();
CSSPrimitiveValue greenValue = rgbColor.getGreen();
float r = redValue.getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
float g =
greenValue.getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
float b = blueValue.getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
color = new Color((int)r,(int)g,(int)b);
return color;
}
that looks good to me, but I don't know where to get the SVGColor
object.
I've got an AbstractElement and can get it's attributes as String.
(It's a SVGRectElement - I suppose).
So where can I get the SVGColor from?
Thx,
Ludwig Reiter
--
View this message in context:
http://www.nabble.com/Parsing-svg-color-to-java-Color-tf3161121.html#a88
04006
Sent from the Batik - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]