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).
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.
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.
Hey, this looks really neat, Chad. I have a class very similar to this that I use in my projects, but yours has support for multiple stops, which is very cool. My only criticism on the code thus far: why are you capitalizing the names of your class methods? (+ AquaSelectedGradient, etc) Otherwise, stellar work.
<blockquote>why are you capitalizing the names of your class methods?</blockquote>
you know, I have no idea…Fixed
I’ve got a Cocoa Gradient example class here:
<a href="http://svn.toxicsoftware.com/svn/toxic_public/trunk/Samples/GradientTest/" rel="nofollow ugc">http://svn.toxicsoftware.com/svn/toxic_public/trunk/Samples/GradientTest/</a>
Good to see more people using Quartz Gradients.
BTW. Your security code is almost impossible to read.
CTGradient is most cool. Its great for making slick backgrounds for Dashboard -like panels.
Any chance you could add corner rounding to the rectangle? :-)
thanks,
Jason
Chad,
Very nice class but it does have a few memory management bugs in managing the linked list of gradient elements. I think it would be better as its own Class.
That said, however, I’m doing my best to fix it as designed.
I am also nearing completion of a gradient inspector that I’m willing to donate.
Drop me a line and let me know what the current status is and what I should do with my updates.
very best regards,
Jason
Jason- I certainly would like to hear about any bugs you may have found in my code. As soon as I can find them you can be sure I’ll issue up a patch.
A gradient inspector? cool – I would be interested to see it.
Hi Chad, just wanted to give you a heads up on a new framework we’ve just released which takes your gradient class concept to a new level. While we haven’t borrowed too much from CTGradient, some parts were originally ported from there so we’ve credited you in our UI and docs.
Our site link is <a href="http://gradientpanel.com" rel="nofollow ugc">http://gradientpanel.com</a>
Graham Cox and Jason Jobe
Is it possible to apply a gradient to the stroke of an NSBezierPath object?
Utterly brilliant, thank you so much for posting. NSGradient is fine (well, it’s a bit limited) but I don’t want to write code that will only run on Leopard – there are still plenty of Tiger machines out there .
<p>chad…I see where you contributed to the Neat program. Perhaps this is not your area of expertise but I wonder if you could tell me why the Neat Library indexes every time it is opened? Apparently Neat tech support has no idea! Some history: This constant indexing only started about 2 months ago when using ver: 4.0.1. Now, upgrading to 4.0.2, the same thing happens. I am very frustrated and I am hoping you might be able to be of some help.
Thanks…Bill Allen</p>