Re: Comparing NSColor instances
Re: Comparing NSColor instances
- Subject: Re: Comparing NSColor instances
- From: Dave Camp <email@hidden>
- Date: Mon, 5 Nov 2007 07:51:09 -0800
On Nov 4, 2007, at 6:58 PM, 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.
If accuracy is important (might not be depending on your application),
you should convert to a larger color space for your comparisons, maybe
Lab or Xyz. I don't think Generic RGB is terribly large.
Also, someone on the Quartz or ColorSync lists might be able to
suggest a good solution to your problem.
Dave
_______________________________________________
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