Skip navigation.
 
mlRe: What paradigm behind NSColor's drawing methods?
FROM : glenn andreas
DATE : Fri Feb 01 21:47:17 2008

On Feb 1, 2008, at 1:41 PM, Jerry Krinock wrote:

> I've always been amazed by -[NSColor set], which "Sets the color of 
> subsequent drawing to the color that the receiver represents".
>
> This seems absurd.  Instead of affecting the receiver, the receiver 
> is affecting some other object, and the identify of the affected 
> object is not even hinted at.
>
> Instead, I would expect there to be a method like like:
>
>    -[NSGraphicsContext setColor:(NSColor*)]
>
> and then you could also have a getter (and I cannot find a real-life 
> equivalent of this),
>
>    -(NSColor*)[NSGraphicsContext color]
>
> I've just re-read the Cocoa Drawing Guide but still can't comprehend 
> the paradigm behind NSColor's -set, -setFill, -setStroke etc.  Can 
> someone please straighten me out?  Or are these methods the 
> unfortunate legacy of some bad decision made 15 years ago?



Pretty much none of the drawing operations take a context - you don't 
do "[[[NSGraphicsContext] currentContext] fillPath: aBezierPath]" nor 
is there "[[[NSGraphicsContext currentContext] drawImage: anImage 
atPoint: aPoint fromRect: aRect ...]" nor similar for string drawing. 
Also, none of the "procedural" drawing routines (NSFillRect, etc...) 
take a graphics context.

Basically, anything that draws effects the the current context, and if 
you want to change that for setting color, you'd want to do it 
everywhere, and adding an explicit context to every single drawing 
related operation would be a major pain.

The paradigm behind NSColor is no different than any of the other 
ones.  It doesn't seem absurd, or even out of line for how other 
graphics based APIs work (after all, glSetColor() doesn't take a 
glContext either, nor did Quickdraw SetColor(), etc...).  You could 
wonder if (fill and stroke) color should be an attribute of the bezier 
path instead, like line width is (or like it was on QuickdrawGX), but 
that's a different philosophy.

And in reality, it works well as is - I've never had a problem where I 
set the color of the wrong drawing context (the only problem I've ever 
has was not remembering to set it appropriately beforehand).  What 
circumstances are you running into that not having an explicit drawing 
context parameter is a problem?


Glenn Andreas                      <email_removed>
  <http://www.gandreas.com/> wicked fun!
quadrium | prime : build, mutate, evolve, animate : the next 
generation of fractal art

Related mailsAuthorDate
mlWhat paradigm behind NSColor's drawing methods? Jerry Krinock Feb 1, 20:41
mlRe: What paradigm behind NSColor's drawing methods? glenn andreas Feb 1, 21:47
mlRe: What paradigm behind NSColor's drawing methods? Jerry Krinock Feb 1, 22:18
mlRe: What paradigm behind NSColor's drawing methods? glenn andreas Feb 1, 22:44
mlRe: What paradigm behind NSColor's drawing methods? Erik Buck Feb 1, 23:49
mlRe: What paradigm behind NSColor's drawing methods? Peter Ammon Feb 2, 01:23
mlRe: What paradigm behind NSColor's drawing methods? Erik Buck Feb 2, 03:29
mlRe: What paradigm behind NSColor's drawing methods? Ricky Sharp Feb 2, 16:04
mlRe: What paradigm behind NSColor's drawing methods? Jerry Krinock Feb 4, 19:59