Posted by: Paul Lefebvre
by Seth Willits
Continuing the article about drawing a simple rectangle with a Quartz shadow, in this article we take a look at axial gradients. The code for this is actually a major pain to explain (I admit I don’t know everything there is to know about it as well), so, sorry, but I’m going to pull a “just look at the code if you want” here.
What I’ve done is add on an extension to the Graphics class which takes a bunch of parameters.
Sub DrawAxialGradient(Extends g as Graphics, c as Canvas, vertical as Boolean, color1 as Color, color2 as Color, Left as Integer, Top as Integer, Width as Integer, Height as Integer)
The canvas parameter is actually optional, but you won’t be able to see why in the example project. Right now, because of the way Canvases are implemented it’s a pain in the butt to deal with because the context you’re drawing into is actually the window’s. That is, a Canvas isn’t a “real” control, it’s just an abstract way to look at a certain rectangle in the window. That’s a problem because if you’re trying to draw into a canvas, you need to know about the window the canvas is in so you can translate the coordinates and clip appropriately. If you’re not drawing in canvas, you pass in nil and it should just do the right thing.
The vertical boolean parameter specifies if the axial shading is vertical or horizontal (there’s no start point and end point definition in this implementation, it’s a bit hardcoded). Color1 and Color2 are the two (not, not three or more in this implementation), and the next four parameters are the bounds of the gradient.
It’s relatively limiting (better than what RB has!), but that’s mostly because to do everything you can in Quartz, you really have to do it all yourself, or I’d really have to spend more time to abstract it out and provide a few different methods and ways of doing things. Have look around the code and you should see why it’d actually be a little tricky.
Download Quartz Gradients REALbasic project
Originally published by ResExcellence
Reprinted with permission

