Re: Comparing NSColor instances
Re: Comparing NSColor instances
- Subject: Re: Comparing NSColor instances
- From: Ali Ozer <email@hidden>
- Date: Mon, 5 Nov 2007 08:48:19 -0800
The getRed:... type methods work only in colors in the named color
spaces, such as NSCalibratedRGBColorSpace.
Colors in "custom" color spaces such as [NSColorSpace
genericRGBColorSpace] respond to the getComponents: and
numberOfComponents accessors. These actually work for all color space
types which floating point components.
Ali
On Nov 4, 2007, at 18:58 , Ricky Sharp wrote:
On Nov 4, 2007, at 8:29 PM, Ricky Sharp wrote:
I have the need to compare multiple NSColor instances to see if
they all represent the same color. This is so I can correctly
support multiple selection in a new IB 3.0 plugin.
My first approach was to "normalize" all colors like so:
NSColor* normalizedColor = [aColor colorUsingColorSpace:
[NSColorSpace genericRGBColorSpace]];
Then, extract the RGB components with getRed:green:blue:alpha: so I
could do a simple comparison of them.
But, when calling getRed::::, I get the following in the Console:
*** -getRed:green:blue:alpha: not defined for the NSColor
NSCustomColorSpace Generic RGB colorspace 0 0 0 1; need to first
convert colorspace.
Also tried deviceRGBColorSpace, but got the same results.
My assumption above was that since I was using an 'RGB' color space,
that one could then call getRed:green:blue:alpha:.
I do have a working solution to where I still normalize to the same
color space, but then use numberOfComponents followed by a
getComponents: with a CGFloat array. I then compare the values in
the array.
I should beef up the code though to handle the case where calling
numberOfComponents can raise an exception (if no floating point
components exist for the color).
Also, I'm never keen on doing exact compares on floating point
values, so I may add in a bit of "slop" to the compare. On the
other hand, if I do normalize two colors to the same color space, it
should be reasonable to assume it will have identical valued
components.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden