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

RadialGradientPaint incorrect focal point location and transition direction #44

Open
bblocher opened this issue Sep 9, 2022 · 1 comment

Comments

@bblocher
Copy link

bblocher commented Sep 9, 2022

I've found a bug where the focal point is always centered regardless of what it's set to, but it does affect the transition of the gradient. The gradient also appears to paint in the incorrect direction (Bottom/Right to Top Left), instead of (Top/Left to Bottom/Right) but I believe that's because the focal point logic is incorrect.

Here is an example of an svg that renders correctly in chrome, inkscape, etc.
marble-red

Here is an example of that same image rendered using pdfbox graphics2d
example.pdf

` private void example(Graphics2D g2d, int x, int y, int diameter) {

    var cRestore = g2d.getComposite();
    var tRestore = g2d.getTransform();
    g2d.translate(x, y);
    g2d.setColor(Color.RED);
    g2d.fillOval(0, 0, diameter, diameter);

    // Setup radial gradient
    Point2D center = new Point2D.Float(diameter / 2, diameter / 2);
    float radius = diameter / 2;
    Point2D focus = new Point2D.Float((int) (diameter * .3), (int) (diameter * .3));
    float[] dist = {0.0f, .75f, 1f};
    Color c = Color.WHITE;
    Color transparent = new Color(c.getRed(), c.getGreen(), c.getBlue(), 0);
    Color[] colors = {c, transparent, Color.BLACK};
    RadialGradientPaint p = new RadialGradientPaint(center, radius, focus, dist, colors, CycleMethod.NO_CYCLE);
    g2d.setPaint(p);
    g2d.fillOval(0, 0, diameter, diameter);

    g2d.setComposite(cRestore);
    g2d.setTransform(tRestore);
}`
rototor added a commit that referenced this issue Sep 11, 2022
non transparent case.
@rototor
Copy link
Owner

rototor commented Sep 11, 2022

No idea why, but I got no notification for this issue ...

There are two bugs. The focial point is wrong but also the transparency mask is off, see #37. I was able to fix the focial point, but regarding the transparency issue I have to find some free time to deeper investigate this, as this stuff is rather tricky...

In PDF you don't have transparent colors, i.e. something like rgba(255,0,0,0.5) for some halve transparent red is not possible in PDF. You have to created some mask (i.e. a bitmap or a form (PDF command stream) with drawing instructions) which represents the alpha channel you want. And then you perform your paint/draw operation but set the transparency mask in the PDF extended graphics state before drawing. For solid colors you can set a special transparency value in the extended graphics state. But this is not possible for gradients. There you have to create a mask which resembles that alpha. And something is wrong with the matrix transforms of the mask. If you like to investigate that deeper you are welcome. But I can not estimate at the moment when I will time for that.

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

2 participants