CocoaDev

Edit AllPages

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSColor_Class/Reference/Reference.html

An NSColor object represents a color, which is defined in a color space, each point of which has a set of components (such as red, green, and blue) that uniquely define a color. —- A Quick Introduction (Not intended as a substitute for reading the docs linked to, above.) http://goo.gl/OeSCu

Drawing With Colors

So, if you’re looking in the NSView documentation for something like setDrawingColor or setStrokeColor, stop. What you’re looking for is actually in the docs for NSColor (look for the instance methods drawSwatchInRect:, set, setFill and setStroke) and/or you can refer to HowToDraw and CocoaDrawing.

Dragging and Dropping Colors

*So where would you expect to find the correct method for dragging a color from a view and dropping into a color well?

Maybe an instance method of NSColor? Nope.

(why would you expect to find it there? does NSString have dragging methods?)

Well then, how about in NSView? It has a dragImage instance method, so is that where they put it? Good guess, but you’re wrong again.

(you mean dragImage:at:offset:event:pasteboard:source:slideBack: ? that just sets an image to represent the view’s pasteboard data during drags. it has nothing to do with what’s acutally dragged or placed on the pasteboard.)

Okay …, maybe it’s a NSColorWell method? Sorry, but you’re getting closer!

(that would limit color dragging to NSColorWells.)

(it’s an NSColorPanel class method, exactly where it belongs.) So, why would an NSColor class method be the wrong place then?


Thought I’d share a little trick to get a hexadecimal representation of a color (i.e., for use in HTML). It’s pretty simple code, but it’s the shortest way I could figure out to do this. If there’s any easier way to do this, please post it.

@implementation NSColor(hexColor)

@end

–RobinHP

—- You probably want roundf([col redComponent] * 255), etc. instead of (int)([col redComponent] * 255), to be as accurate as possible.

You may prefer this solution: (anyway, the forced colorUsingColorSpaceName parameter should not be a part of your function)

-mmw


Watch out for long lines when you’re using code tags. Code doesn’t autowrap on CocoaDev.


You may prefer this solution: (anyway, the forced colorUsingColorSpaceName parameter should not be a part of your function)

But this isn’t a solution, it’s solving a different problem. And why not convert to RGB in the function? Since HTML hex strings imply RGB, it makes perfect sense that a category on NSColor, which supports a variety of different colour spaces, should do this. If you are going to make assertions, back them up with explanations.