CGShadings are a pain. While CG is a heck of a powerful library… It always seems to take an elaborate amount of code (as well as time, if you’re not yet acquainted with it) to do some of the simplest little things. For a discussion on creating gradients with CGShading go [here](http://www.cocoadev.com/index.pl?GradientFill).

cocoa gradient

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.

Download CTGradient
< 100 KB

And so, that said, I figure [a](http://mattgemmell.com/source/) [few](http://www.andymatuschak.org/articles/2006/01/11/making-the-hud-item-1-a-frame-themed-party) [people](http://wilshipley.com/blog/2005/07/pimp-my-code-part-3-gradient.html) might find it handy.

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](http://en.wikipedia.org/wiki/HSB_color_space))
+ 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](http://svn.oofn.net/#CTGradient).

Update: With OS 10.5’s AppKit, I would recommend using only NSGradient.