Gradient fills in irregular shapes

Has anyone written any code for doing gradient fills in polygons or even rounded rectangles. I am quite happy in calculating and filling regular shapes such as a rectangle. I can imagine how to calculate a gradient fill for a circle. My guess would be to calculate lines of varying shade from top to bottom of the circle of varying lengths to fill from left to right of the circle.

Is there an easier way. What about an irregular polygon?

OSX by they way but XPlat would be good.

Can’t you just fill a rectangle with the gradient and then ‘cut’ out the shape you need using the mask?

That is exactly how you do it…

calculate the min and max X and Y coordinates from your polygon
create a rectangle picture that is (MaxX-MinX) by (MaxY-MinY) in size
fill in the MASK with all WHITE, and then draw on the MASK the same Polygon filled with all BLACK
Then on the main picture surface apply you gradient…

[quote=113689:@Dave S]That is exactly how you do it…

calculate the min and max X and Y coordinates from your polygon
create a rectangle picture that is (MaxX-MinX) by (MaxY-MinY) in size
fill in the MASK with all WHITE, and then draw on the MASK the same Polygon filled with all BLACK
Then on the main picture surface apply you gradient…[/quote]
Sounds like what I need. Great stuff i’ll give it a go. Thanks

If you want a fast method for OS X, then take a look at CoreGraphics functions (should be in MacOSLib and MBS), there is a clip to path function and a clip to image function. I would also suggest taking a look at either NSGradient or CGGradient as these will provide smoother and much faster gradients than drawing line by line. Not do mention, the gradient can be done in any direction.

Thanks Sam I’ll have a dabble with these