Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SvgColourServer with translucent color? #5

Closed
QuantumConcepts opened this issue Jun 19, 2013 · 5 comments
Closed

SvgColourServer with translucent color? #5

QuantumConcepts opened this issue Jun 19, 2013 · 5 comments

Comments

@QuantumConcepts
Copy link

Please forgive me if this is not an issue but a misunderstanding.

I've opened an SVG and then set all Children (who are ISvgStyleable) to have a fill of 25% transparent black:

private static void RefillSvg(SvgDocument svg, Color color)
{
    SvgPaintServer fill = new SvgColourServer
    {
        Colour = color
    };

    RefillSvgElements(svg.Children, fill);
}

private static void RefillSvgElements(SvgElementCollection elements, SvgPaintServer fill)
{
    elements.OfType<ISvgStylable>().ForEach(o => o.Fill = fill);

    foreach (SvgElement element in elements)
        RefillSvgElements(element.Children, fill);
}

This is called like this:

RefillSvg(svg, Color.FromArgb((int)(255 * .25f), Color.Black));

The resulting PNG, after saving is completely transparent. E.g.:

private static void Sample()
{
    SvgDocument svg = GetSvg();
    Bitmap bitmap = new Bitmap((int)svg.Width.Value, (int)svg.Height.Value, PixelFormat.Format32bppArgb);
    SvgRenderer svgRenderer = SvgRenderer.FromImage(bitmap);

    RefillSvg(svg, Color.FromArgb((int)(255 * .25f), Color.Black));

    bitmap.Save(@"C:\Sample.png", ImageFormat.Png);
}

Are alpha channels not supported or am I missing something? Thanks!

@tebjan
Copy link
Contributor

tebjan commented Jun 19, 2013

Hello,

svg has an extra attribute for the opacity value, or to be precise a few:
http://www.w3.org/TR/SVG/masking.html#ObjectAndGroupOpacityProperties

Its a float in the range 0..1 where 0 is fully transparent and 1 fully opaque.

So instead of o.Fill, you should write a value to o.FillOpacity. There is also o.Opacity (overall opacity) and o.StrokeOpacity (opacity for strokes).

@QuantumConcepts
Copy link
Author

I've set the FIllOpacity and Opacity on all SvgVisualElements to 1 already (did not include that part in my sample code, sorry).

I looked through the source and it appears that the SvgColourServer attempts to extract and re-calculate the alpha channel to use for the brush (https://github.com/vvvv/SVG/blob/master/Source/Painting/SvgColourServer.cs line 27).

I'm going to run some tests and see what's happening here. But it seems like a Color instance with an alpha channel is meant to be supported.

@tebjan
Copy link
Contributor

tebjan commented Jun 20, 2013

Hm... Yes, there seems to be some work in that direction. But its as far as i know not in the official svg spec. To control the transparency you have to set the opacity attributes. In your case the FillOpacity to 0.25f.

But anyways the code looks somehow correct. But there could be a problem with the division. Since both values are Integer, i think its always 0 because Colour.A is smaller than 255. it should be:

 int alpha = (int)((opacity * (this.Colour.A/255.0f) ) * 255);

I fixed that by 457a82f. If you can compile the code you can check that right away. Otherwise you have to wait for the next Nuget package.

@tebjan
Copy link
Contributor

tebjan commented Jun 20, 2013

Nuget 1.0.1 is now also available.

@QuantumConcepts
Copy link
Author

I think what I was trying to do would result in deeper-nested elements becoming more and more transparent, so it's not right anyway. I appreciate the attempted fix, though.

What I'm doing is programatically recoloring SVGs. So what I'll do when I need to make one translucent is move all of the SVG's child elements into a new group element with the opacity/fill set appropriately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant