You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by James Balnaves <li...@balnaves.com> on 2006/02/01 22:33:55 UTC
How do I parse style information to get Java Color?
I have a path in an SVG document that looks like this:
<g style="stroke-linecap:round; fill:rgb(11,11,138); text-
rendering:optimizeLegibility; image-rendering:optimizeQuality; color-
rendering:optimizeQuality; stroke-linejoin:round; color-
interpolation:linearRGB; stroke:rgb(11,11,138);">
<path d="M792 361 L800 361" style="fill:none;" />
</g>
I would like to use Batik to construct a java.awt.Color object from
the "stroke:rgb(11,11,138);" information but I understand that the
color information could be described in many different ways as CSS
allows.
I am already parsing the path and width data from an SVGDocument with
code like
NodeList paths = group.getElementsByTagName("path");
for (int j = 0; j < paths.getLength(); j++)
{
SVGOMPathElement path = (SVGOMPathElement) paths.item(j);
if (path != null)
{
String pathData = path.getAttribute("d");
ExtendedGeneralPath egp = (ExtendedGeneralPath)
AWTPathProducer.createShape(new StringReader(pathData), 0);
String widthData = path.getAttribute("stroke-width");
}
}
What's the best way to get the color information into a
java.awt.Color object using Batik? Something like
CSSUtilities.getComputedStyle() looks tempting but I cannot seem to
make it fit.
Any help would be appreciated.
Regards, James.
Re: How do I parse style information to get Java Color?
Posted by James Balnaves <li...@balnaves.com>.
Thanks Thomas. That was the crucial step I was missing, it works
nicely now. Thanks Cameron and Tonny for the property computation help.
On Feb 3, 2006, at 11:57 AM, thomas.deweese@kodak.com wrote:
> Hi James,
>
> Ahh, you need to boot the CSS DOM:
> http://wiki.apache.org/xmlgraphics-batik/
> BootSvgAndCssDom
>
> James Balnaves <li...@balnaves.com> wrote on 02/03/2006 11:28:52 AM:
>
>> Thanks for the suggestion. Unfortunately I get identical results
>> using this method, a null pointer exception on the
>> getPropertyCSSValue
>> (). Is there something else I need to do to calculate the stroke?
>> Perhaps I am missing something in the setup. I am creating the
>> document from a file stream using the following snippet I found in
>> the docs somewhere:
>>
>> String parser =
>> XMLResourceDescriptor.getXMLParserClassName();
>> SAXSVGDocumentFactory f = new SAXSVGDocumentFactory
>> (parser);
>> SVGDocument doc = (SVGDocument)f.createDocument
>> ("http:internal", is);
>>
>> After that I use getElementsByTagName() to find the paths, which I am
>> then using as the element in your example code. Does anything look
>> out of place?
>>
>> Thanks, James.
>>
>> On Feb 2, 2006, at 8:36 PM, Tonny Kohar wrote:
>>
>>> Hi,
>>>
>>> On Thu, 2006-02-02 at 15:26 -0500, James Balnaves wrote:
>>>> When I try this, the CSSStyleDeclaration ends up containing an
>>>> "element" member of type SVGOMPathElement that has attributes
>>>> "fill"
>>>> and "d", but the CSSStyleDeclaration "values" member HashMap is
>>>> empty.
>>>> When I call style.getPropertyCSSValue("stroke") I get a null
>>>> pointer
>>>> exception, even though this value exists up in the main svg
>>>> element.
>>>
>>> How about something like this
>>> ViewCSS viewCSS =
>>> (ViewCSS)element.getOwnerDocument().getDocumentElement();
>>> CSSStyleDeclaration computedStyle =
>>> viewCSS.getComputedStyle(element,null);
>>> SVGPaint svgPaint =
>>> (SVGPaint)computedStyle.getPropertyCSSValue
>>> (SVGConstants.SVG_STROKE_ATTRIBUTE);
>>> Color color = null;
>>> if (svgPaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
>>> RGBColor rgbColor = svgPaint.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);
>>> } else if (svgPaint.getPaintType() ==
>>> SVGPaint.SVG_PAINTTYPE_CURRENTCOLOR) {
>>> // this is the color keyword eg: black, blue, green,
>>> aliceblue,etc
>>> // please check with the SVG / CSS Dom for the exact RGB value
>>> for
>>> this
>>> color keyword
>>> } else if (svgPaint.getPaintType() ==
>>> SVGPaint....) {
>>>
>>
>> ---------------------------------------------------------------------
>> 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
>
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: How do I parse style information to get Java Color?
Posted by th...@kodak.com.
Hi James,
Ahh, you need to boot the CSS DOM:
http://wiki.apache.org/xmlgraphics-batik/BootSvgAndCssDom
James Balnaves <li...@balnaves.com> wrote on 02/03/2006 11:28:52 AM:
> Thanks for the suggestion. Unfortunately I get identical results
> using this method, a null pointer exception on the getPropertyCSSValue
> (). Is there something else I need to do to calculate the stroke?
> Perhaps I am missing something in the setup. I am creating the
> document from a file stream using the following snippet I found in
> the docs somewhere:
>
> String parser =
> XMLResourceDescriptor.getXMLParserClassName();
> SAXSVGDocumentFactory f = new SAXSVGDocumentFactory
> (parser);
> SVGDocument doc = (SVGDocument)f.createDocument
> ("http:internal", is);
>
> After that I use getElementsByTagName() to find the paths, which I am
> then using as the element in your example code. Does anything look
> out of place?
>
> Thanks, James.
>
> On Feb 2, 2006, at 8:36 PM, Tonny Kohar wrote:
>
> > Hi,
> >
> > On Thu, 2006-02-02 at 15:26 -0500, James Balnaves wrote:
> >> When I try this, the CSSStyleDeclaration ends up containing an
> >> "element" member of type SVGOMPathElement that has attributes "fill"
> >> and "d", but the CSSStyleDeclaration "values" member HashMap is
> >> empty.
> >> When I call style.getPropertyCSSValue("stroke") I get a null pointer
> >> exception, even though this value exists up in the main svg element.
> >
> > How about something like this
> > ViewCSS viewCSS =
> > (ViewCSS)element.getOwnerDocument().getDocumentElement();
> > CSSStyleDeclaration computedStyle =
> > viewCSS.getComputedStyle(element,null);
> > SVGPaint svgPaint =
> > (SVGPaint)computedStyle.getPropertyCSSValue
> > (SVGConstants.SVG_STROKE_ATTRIBUTE);
> > Color color = null;
> > if (svgPaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
> > RGBColor rgbColor = svgPaint.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);
> > } else if (svgPaint.getPaintType() ==
> > SVGPaint.SVG_PAINTTYPE_CURRENTCOLOR) {
> > // this is the color keyword eg: black, blue, green, aliceblue,etc
> > // please check with the SVG / CSS Dom for the exact RGB value for
> > this
> > color keyword
> > } else if (svgPaint.getPaintType() ==
> > SVGPaint....) {
> >
>
> ---------------------------------------------------------------------
> 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
Re: How do I parse style information to get Java Color?
Posted by James Balnaves <li...@balnaves.com>.
Thanks for the suggestion. Unfortunately I get identical results
using this method, a null pointer exception on the getPropertyCSSValue
(). Is there something else I need to do to calculate the stroke?
Perhaps I am missing something in the setup. I am creating the
document from a file stream using the following snippet I found in
the docs somewhere:
String parser =
XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory
(parser);
SVGDocument doc = (SVGDocument)f.createDocument
("http:internal", is);
After that I use getElementsByTagName() to find the paths, which I am
then using as the element in your example code. Does anything look
out of place?
Thanks, James.
On Feb 2, 2006, at 8:36 PM, Tonny Kohar wrote:
> Hi,
>
> On Thu, 2006-02-02 at 15:26 -0500, James Balnaves wrote:
>> When I try this, the CSSStyleDeclaration ends up containing an
>> "element" member of type SVGOMPathElement that has attributes "fill"
>> and "d", but the CSSStyleDeclaration "values" member HashMap is
>> empty.
>> When I call style.getPropertyCSSValue("stroke") I get a null pointer
>> exception, even though this value exists up in the main svg element.
>
> How about something like this
> ViewCSS viewCSS =
> (ViewCSS)element.getOwnerDocument().getDocumentElement();
> CSSStyleDeclaration computedStyle =
> viewCSS.getComputedStyle(element,null);
> SVGPaint svgPaint =
> (SVGPaint)computedStyle.getPropertyCSSValue
> (SVGConstants.SVG_STROKE_ATTRIBUTE);
> Color color = null;
> if (svgPaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
> RGBColor rgbColor = svgPaint.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);
> } else if (svgPaint.getPaintType() ==
> SVGPaint.SVG_PAINTTYPE_CURRENTCOLOR) {
> // this is the color keyword eg: black, blue, green, aliceblue,etc
> // please check with the SVG / CSS Dom for the exact RGB value for
> this
> color keyword
> } else if (svgPaint.getPaintType() ==
> SVGPaint....) {
>
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: How do I parse style information to get Java Color?
Posted by Tonny Kohar <to...@kiyut.com>.
Hi,
On Thu, 2006-02-02 at 15:26 -0500, James Balnaves wrote:
> When I try this, the CSSStyleDeclaration ends up containing an
> "element" member of type SVGOMPathElement that has attributes "fill"
> and "d", but the CSSStyleDeclaration "values" member HashMap is empty.
> When I call style.getPropertyCSSValue("stroke") I get a null pointer
> exception, even though this value exists up in the main svg element.
How about something like this
ViewCSS viewCSS =
(ViewCSS)element.getOwnerDocument().getDocumentElement();
CSSStyleDeclaration computedStyle =
viewCSS.getComputedStyle(element,null);
SVGPaint svgPaint =
(SVGPaint)computedStyle.getPropertyCSSValue(SVGConstants.SVG_STROKE_ATTRIBUTE);
Color color = null;
if (svgPaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
RGBColor rgbColor = svgPaint.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);
} else if (svgPaint.getPaintType() ==
SVGPaint.SVG_PAINTTYPE_CURRENTCOLOR) {
// this is the color keyword eg: black, blue, green, aliceblue,etc
// please check with the SVG / CSS Dom for the exact RGB value for this
color keyword
} else if (svgPaint.getPaintType() ==
SVGPaint....) {
Regards
Tonny Kohar
--
Sketsa
SVG Graphics Editor
http://www.kiyut.com
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: How do I parse style information to get Java Color?
Posted by James Balnaves <li...@balnaves.com>.
When I try this, the CSSStyleDeclaration ends up containing an
"element" member of type SVGOMPathElement that has attributes "fill"
and "d", but the CSSStyleDeclaration "values" member HashMap is
empty. When I call style.getPropertyCSSValue("stroke") I get a null
pointer exception, even though this value exists up in the main svg
element.
Any ideas? Do I have to search all the way up the tree for the stroke
attribute? (although getComputedStyle() on the SVG element does not
seem to work either).
I have included the SVG file I am trying to work with below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/
2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg fill-opacity="1" xmlns:xlink="http://www.w3.org/1999/xlink"
color-rendering="auto" color-interpolation="auto" text-
rendering="auto" stroke="black" stroke-linecap="square" stroke-
miterlimit="10" shape-rendering="auto" stroke-opacity="1"
fill="black" stroke-dasharray="none" font-weight="normal" stroke-
width="1" xmlns="http://www.w3.org/2000/svg" font-
family="'Helvetica'" font-style="normal" stroke-
linejoin="miter" font-size="12" stroke-dashoffset="0" image-
rendering="auto">
<!--Generated by the Batik Graphics2D SVG Generator-->
<defs id="genericDefs" />
<g>
<g id="Page 0">
<g stroke-linecap="round" text-rendering="optimizeLegibility"
image-rendering="optimizeQuality" color-rendering="optimizeQuality"
stroke-linejoin="round" color-interpolation="linearRGB">
<path fill="none" d="M146 123 L146 122" />
</g>
</g>
</g>
</svg>
On Feb 1, 2006, at 5:19 PM, Cameron McCormack wrote:
> Hi James.
>
> James Balnaves:
>> I would like to use Batik to construct a java.awt.Color object from
>> the "stroke:rgb(11,11,138);" information but I understand that the
>> color information could be described in many different ways as CSS
>> allows.
>
> Something like this would do it, using the standard SVG/CSS DOM
> interfaces (untested):
>
> Element path = ...; // the path element
> Document doc = p.getOwnerDocument();
> SVGSVGElement svg = (SVGSVGElement) doc.getDocumentElement();
> CSSStyleDeclaration style = svg.getComputedStyle(path, "");
> SVGPaint stroke = (SVGPaintValue) style.getPropertyCSSValue
> ("stroke");
> // assuming that it's a solid colour:
> RGBColor rgb = stroke.getRgbColor();
> float r = rgb.getRed().getFloatValue(CSSValue.CSS_NUMBER);
> float g = rgb.getGreen().getFloatValue(CSSValue.CSS_NUMBER);
> float b = rgb.getBlue().getFloatValue(CSSValue.CSS_NUMBER);
> Color c = new Color(r / 255, g / 255, b / 255);
Re: How do I parse style information to get Java Color?
Posted by Cameron McCormack <ca...@aka.mcc.id.au>.
Hi James.
James Balnaves:
> I would like to use Batik to construct a java.awt.Color object from
> the "stroke:rgb(11,11,138);" information but I understand that the
> color information could be described in many different ways as CSS
> allows.
Something like this would do it, using the standard SVG/CSS DOM
interfaces (untested):
Element path = ...; // the path element
Document doc = p.getOwnerDocument();
SVGSVGElement svg = (SVGSVGElement) doc.getDocumentElement();
CSSStyleDeclaration style = svg.getComputedStyle(path, "");
SVGPaint stroke = (SVGPaintValue) style.getPropertyCSSValue("stroke");
// assuming that it's a solid colour:
RGBColor rgb = stroke.getRgbColor();
float r = rgb.getRed().getFloatValue(CSSValue.CSS_NUMBER);
float g = rgb.getGreen().getFloatValue(CSSValue.CSS_NUMBER);
float b = rgb.getBlue().getFloatValue(CSSValue.CSS_NUMBER);
Color c = new Color(r / 255, g / 255, b / 255);
--
Cameron McCormack ICQ: 26955922
cam (at) mcc.id.au MSN: cam (at) mcc.id.au
http://mcc.id.au/ JBR: heycam (at) jabber.org
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org