CGShadings are a pain, or well, lets just leave it at all of Core Graphics (CG). While CG is a heck of a powerful library… It always seems to take an elaborate amount of code (as well as time, if your not yet acquainted with it) to do some of the simplest little things. For a discussion on creating gradients with CGShading go here.
Now, AppKit provides a pretty decent layer of abstraction, but I find that there are still a number of holes in its coverage of those lower level APIs that could use some filling.
One of those that AppKit doesn’t cover (that it really, really should, especially given the whole resolution independence thing that’s in the works – 10.5 maybe? please?), are gradients.
When I started coding up a sourcelist (you know, the tableView with the iTunes/iPhoto/Mail styling) and also thinking of a few of the mini-projects that I had plans for, many of which could use gradients also – I figured that I might as well take a little detour and try my hand at filling in that gap.
And so, let me introduce you to my little Cocoa Obj-C Gradient class, CTGradient. It’s a small class that should provide a decent cocoa interface for CGShading.
In my case, CTGradient has gone a long way toward making the code in several of the projects I’ve been working on significantly less cluttered (and that’s always a welcome change). I no longer have lines of GCContext junk in my drawing functions nor do I have CG function callbacks proliferating my code. The few convenience methods that I tacked on help a bit with readability as well.
As it stands right now, it’s not quite finished (I expect the interface to change by a fair amount in the not to distant future – I have a few additions to make). But in any case the code is stable, so if it does what you need it to do, go ahead and make use of it!
With that I’ll end with a short list of those additions I have plans to make:
Radial Gradient (just need to figure out best method interface) Chromatic blending (blending HSB)
- CTGradientWell (counterpart to NSColorWell – with palette to boot)
- CTGradientSlider/Selecter? (view to let you add/remove/adjust color stops)
Miscellaneous methods for color stops More pre-made gradients styles
Update: Updates and other info will be maintained at svn.oofn.net.
Update: With OS 10.5’s AppKit, I would recommend using only NSGradient.