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 Ludwig Reiter <Lu...@intevation.de> on 2007/02/02 15:27:25 UTC

Parsing svg color to java Color

Hi all,

is there an easy way in the code of batik to parse an svg color attribute
string to a java color object?
I know there must be some code, but I haven't find it yet.

Regards.
     Ludwig Reiter
-- 
View this message in context: http://www.nabble.com/Parsing-svg-color-to-java-Color-tf3161121.html#a8767809
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: Parsing svg color to java Color

Posted by Sujesh Babu N <su...@gmail.com>.
I think , the following code snippet will be helpful.

 public static CSSOMComputedStyle getComputedStyle(Element element) {
  ViewCSS viewCSS = (ViewCSS) element.getOwnerDocument()
    .getDocumentElement();
  CSSOMComputedStyle computedStyle = (CSSOMComputedStyle) viewCSS
    .getComputedStyle(element, null);
  return computedStyle;
 }

 CSSOMComputedStyle computedStyle = getComputedStyle(element);
 String type=SVGConstants.SVG_FILL_ATTRIBUTE;
 if (computedStyle.getPropertyCSSValue(type) instanceof SVGPaint) {
  SVGPaint svgPaint = (SVGPaint) computedStyle
     .getPropertyCSSValue(type);
 }else{
  SVGColor SVGclr = (SVGColor) computedStyle
     .getPropertyCSSValue(type);
 }


best regards
Sujesh





On 2/5/07, Ludwig Reiter <Lu...@intevation.de> wrote:
>
>
> 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#a8804006
> Sent from the Batik - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>

RE: Parsing svg color to java Color

Posted by Ludwig Reiter <Lu...@intevation.de>.
Hi,

okay, that solves the problem. Thank you.

Ludwig Reiter 


Bishop, Michael W. CONTR J9C880 wrote:
> 
> 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
> 

-- 
View this message in context: http://www.nabble.com/Parsing-svg-color-to-java-Color-tf3161121.html#a8823243
Sent from the Batik - Users mailing list archive at Nabble.com.


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


RE: Parsing svg color to java Color

Posted by "Bishop, Michael W. CONTR J9C880" <Mi...@je.jfcom.mil>.
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:Ludwig.Reiter@intevation.de] 
Sent: Monday, February 05, 2007 5:37 AM
To: batik-users@xmlgraphics.apache.org
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: 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: Parsing svg color to java Color

Posted by Ludwig Reiter <Lu...@intevation.de>.
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#a8804006
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: Parsing svg color to java Color

Posted by Tonny Kohar <to...@kiyut.com>.
Hi,

On Fri, 2007-02-02 at 06:27 -0800, Ludwig Reiter wrote:
> Hi all,
> 
> is there an easy way in the code of batik to parse an svg color attribute
> string to a java color object?
> I know there must be some code, but I haven't find it yet.

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;
    }

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