create shape with graphicpath linear gradient ruby?

For general discussion related FlowStone
Post Reply
User avatar
wlangfor@uoguelph.ca
Posts: 912
Joined: Tue Apr 03, 2018 5:50 pm
Location: North Bay, Ontario, Canada
Contact:

create shape with graphicpath linear gradient ruby?

Post 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
My youtube channel: DSPplug
My Websites: www.dspplug.com KVRaudio flowstone products
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: create shape with graphicpath linear gradient ruby?

Post 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.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: create shape with graphicpath linear gradient ruby?

Post 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.
"There lies the dog buried" (German saying translated literally)
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: create shape with graphicpath linear gradient ruby?

Post 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!)
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
wlangfor@uoguelph.ca
Posts: 912
Joined: Tue Apr 03, 2018 5:50 pm
Location: North Bay, Ontario, Canada
Contact:

Re: create shape with graphicpath linear gradient ruby?

Post 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.
My youtube channel: DSPplug
My Websites: www.dspplug.com KVRaudio flowstone products
Post Reply