Page 1 of 1

create shape with graphicpath linear gradient ruby?

Posted: Wed Dec 18, 2019 12:29 am
by wlangfor@uoguelph.ca
Hi all I've been using the addlines method in graphics path.

Can someone tell Me how to add a linear gradient to that?

TIA

Re: create shape with graphicpath linear gradient ruby?

Posted: Wed Dec 18, 2019 2:30 am
by trogluddite
The gradient will come from the brush that you draw the path with. Instead of a normal Ruby Brush object, you use a LinearGradientBrush or a PathGradientBrush (User Guide: 'Advanced Brushes', p.173)

The tricky thing is that a gradient brush isn't "attached" to the shape like it would be in most graphics applications. If you move or resize the path, you also have to move or resize the gradient. For a path, the easiest way usually is to call the getBounds method of the path; this returns a rectangular area which exactly fits the path...

Code: Select all

@path = GraphicsPath.new
# Fill the path with lines, curves, etc. here, making sure that its closed.

# Get the area of the path.
area = @path.getBounds

# Other gradient arguments
color1 = Color.new(255, 0, 0)
color2 = Color.new(0, 255, 0)
angle = 45  # clockwise in degrees.
scaleTransform = false # for angled gradients, use true if it looks better!

@gradient = LinearGradientBrush.new(area, color1, color2, angle, scaleTransform)

# Then, in your drawing routine
view.drawPath(@gradient, @path)

The area doesn't have to fit the path like this; if it's bigger, the path acts as a "window" that you see part of the gradient through.

Re: create shape with graphicpath linear gradient ruby?

Posted: Wed Dec 18, 2019 4:52 am
by tulamide
trogluddite wrote:The tricky thing is that a gradient brush isn't "attached" to the shape like it would be in most graphics applications. If you move or resize the path, you also have to move or resize the gradient.

That's the most important part. I had quite a few headaches before I realized it. Your image of a window is quite good. I myself imagine it as it probably is technically realized: An area is completely filled with a gradient, but an alpha mask reveals it only where you actually draw.

Re: create shape with graphicpath linear gradient ruby?

Posted: Wed Dec 18, 2019 6:06 pm
by trogluddite
tulamide wrote:Your image of a window is quite good. I myself imagine it as it probably is technically realized...

Thanks. The 'scaleTransform' argument beat me, though - I understand it conceptually in geometrical terms, but I had to give up on trying to put it into non-technical language (as did DSPr to judge by the User Guide!)

Re: create shape with graphicpath linear gradient ruby?

Posted: Sun Jan 12, 2020 7:04 pm
by wlangfor@uoguelph.ca
Yes, the scale transform is strange to say the least. In fact I had tried quite a few different methods but likely these are the only ones which I can employ. Thanks to both of you. Sorry for the late reply but I had been a way attending to something.

Thanks to you both, these are useful tips.