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 Tina Frieling <ti...@gmx.net> on 2005/07/10 16:35:55 UTC

Setting Style Attribute

Hello,

I have made a small SVG-Document and want to change the fill-color of a specific element dynamically. I have given this Element an ID-Attribut in 
SVG to retrive it via getElementById()-Method. My problem is the fill-Attribute now. As you surely know in SVG you can set this attribute either as an 
attribute itself (as in fill="#000000") or as part of an style-Attribute (as in style="fill:#000000"). whereas the style-attribute seems to have priority over 
the fill-Attribute. To retrieve the fill-style I use 

Value colorValue = CSSUtilities.getComputedStyle(backgroundColorElement,SVGCSSEngine.FILL_INDEX);

which seems to work for both kinds of fill-attributes. To set a new fill-style is what I could not figure out. To set the fill-style as an attribute could be 
done via DOM with setAttribute() on the element, but since it has lower priority this won't change the style if there is an style attribute. So I could not 
find how to set this style-attribute correcly via batik. At least I tried the following, but it doesn't realy work. The resulting color  was wrong:

if (backgroundColorElement instanceof CSSStylableElement) {
  CSSStylableElement bceStylable = (CSSStylableElement)backgroundColorElement;
  StyleMap sm = bceStylable.getComputedStyleMap(null);
  if (sm != null) {
    float[] rgbColors = Color.WHITE.getRGBColorComponents(null);
    sm.putValue(SVGCSSEngine.FILL_INDEX,new RGBColorValue(
           new FloatValue(
              CSSPrimitiveValue.CSS_RGBCOLOR,
              rgbColors[0]), new FloatValue(
              CSSPrimitiveValue.CSS_RGBCOLOR,
              rgbColors[1]), new FloatValue(
              CSSPrimitiveValue.CSS_RGBCOLOR,
              rgbColors[2])));
     bceStylable.setComputedStyleMap(null, sm);
  }
}

How can I set the fill-part of the style-attribute wothou parsing it for myself ?

Greetings




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


Re: Setting Style Attribute

Posted by Tina Frieling <ti...@gmx.net>.
On Sun, 10 Jul 2005 15:53:41 -0400, Thomas DeWeese wrote:

>    See my first response: 'setProperty'.  Turning a simple JDK
>Color object into a CSS Color String is trivial and left as
>an exercise for the reader.

:-) 

Many thanks for your help Thomas.





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


Re: Setting Style Attribute

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Tina,

Tina Frieling wrote:

> But do you also know hot to set a new Color-Value in a similar 
> easy way (see my example how I do this at the moment in my first 
> mail)?

    See my first response: 'setProperty'.  Turning a simple JDK
Color object into a CSS Color String is trivial and left as
an exercise for the reader.

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


Re: Setting Style Attribute

Posted by Tina Frieling <ti...@gmx.net>.
On Sun, 10 Jul 2005 14:07:06 -0400, Thomas DeWeese wrote:

>    The 'getCSSvalue' version returns a CSSValue which for fill should
>be an 'SVGPaint' object, and for simple colors it will be an
>SVGColor (also css.RGBColor).  I would look at or just use the
>code in batik.bridge.PaintServer.  It has a bunch of static
>methods that can be used for this purpose.
>

These methods in PaintServer will do what I need, thanks Thomas. But do you also know hot to set a new Color-Value in a similar easy way (see my 
example how I do this at the moment in my first mail) ?



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


Re: Setting Style Attribute

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Tina,

> On Sun, 10 Jul 2005 12:52:34 -0400, Thomas DeWeese wrote:

>>	((SVGStylable)elem).getStyle().getCSSValue("fill");
>>
>>  Which returns a CSSValue which is kind of complex but gives some
>>a more 'parsed' version of the value (a CSSValue).

Tina Frieling wrote:

> Thank you for your answer. This gets me one step further in retriving 
> this value. The last step would be to have an easy way to convert this 
> String-Value to a Java (Color-)object (and vice versa). But I think I 
> can do this on my own if necessary.

    The 'getCSSvalue' version returns a CSSValue which for fill should
be an 'SVGPaint' object, and for simple colors it will be an
SVGColor (also css.RGBColor).  I would look at or just use the
code in batik.bridge.PaintServer.  It has a bunch of static
methods that can be used for this purpose.

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


Re: Setting Style Attribute

Posted by Tina Frieling <ti...@gmx.net>.
On Sun, 10 Jul 2005 12:52:34 -0400, Thomas DeWeese wrote:

>    Instead of using the internal Batik interfaces you should use
>the CSS DOM interfaces.  The basic gist of this is:
>
>	((SVGStylable)elem).getStyle().setProperty("fill", "red", "");
>
>    You can also use this to get the value of the property as a string:
>
>	((SVGStylable)elem).getStyle().getPropertyValue("fill");
>
>   or
>
>	((SVGStylable)elem).getStyle().getCSSValue("fill");
>
>   Which returns a CSSValue which is kind of complex but gives some
>a more 'parsed' version of the value (a CSSValue).
>


Hello Thomas,

Thank you for your answer. This gets me one step further in retriving this value. The last step would be to have an easy way to convert this String-
Value to a Java (Color-)object (and vice versa). But I think I can do this on my own if necessary.

Thanks.




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


Re: Setting Style Attribute

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Tina,

Tina Frieling wrote:

> I have made a small SVG-Document and want to change the fill-color of a 
> specific element dynamically. I have given this Element an ID-Attribut 
> in SVG to retrive it via getElementById()-Method. My problem is the 
> fill-Attribute now. As you surely know in SVG you can set this 
> attribute either as an attribute itself (as in fill="#000000") or as 
> part of an style-Attribute (as in style="fill:#000000"). whereas the 
> style-attribute seems to have priority over the fill-Attribute. 
> To retrieve the fill-style I use:
> 
> Value colorValue = CSSUtilities.getComputedStyle(backgroundColorElement,SVGCSSEngine.FILL_INDEX);

    Instead of using the internal Batik interfaces you should use
the CSS DOM interfaces.  The basic gist of this is:

	((SVGStylable)elem).getStyle().setProperty("fill", "red", "");

    You can also use this to get the value of the property as a string:

	((SVGStylable)elem).getStyle().getPropertyValue("fill");

   or

	((SVGStylable)elem).getStyle().getCSSValue("fill");

   Which returns a CSSValue which is kind of complex but gives some
a more 'parsed' version of the value (a CSSValue).

> which seems to work for both kinds of fill-attributes. To set a new fill-style is what I could not figure out. To set the fill-style as an attribute could be 
> done via DOM with setAttribute() on the element, but since it has lower priority this won't change the style if there is an style attribute. So I could not 
> find how to set this style-attribute correcly via batik. At least I tried the following, but it doesn't realy work. The resulting color  was wrong:
> 
> if (backgroundColorElement instanceof CSSStylableElement) {
>   CSSStylableElement bceStylable = (CSSStylableElement)backgroundColorElement;
>   StyleMap sm = bceStylable.getComputedStyleMap(null);
>   if (sm != null) {
>     float[] rgbColors = Color.WHITE.getRGBColorComponents(null);
>     sm.putValue(SVGCSSEngine.FILL_INDEX,new RGBColorValue(
>            new FloatValue(
>               CSSPrimitiveValue.CSS_RGBCOLOR,
>               rgbColors[0]), new FloatValue(
>               CSSPrimitiveValue.CSS_RGBCOLOR,
>               rgbColors[1]), new FloatValue(
>               CSSPrimitiveValue.CSS_RGBCOLOR,
>               rgbColors[2])));
>      bceStylable.setComputedStyleMap(null, sm);
>   }
> }
> 
> How can I set the fill-part of the style-attribute wothou parsing it for myself ?
> 
> Greetings
> 
> 
> 
> 
> ---------------------------------------------------------------------
> 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