Compare UIColors ios

Have you tried [myColor isEqual:someOtherColor] ?

share|improve this answer
 
    
Thanks. What is the difference with isEqualTo? Also, do you know how to display it in the debugger? – 4thSpace Jun 9 ’09 at 15:19
32  
By the way: Be careful when comparing colors this way, because they have to be in the same color model to be considered equal. For instance, #ffffff does not equal [UIColor whiteColor]. –  zoul Oct 19 ’09 at 16:41
    
Good point Zoul, might be more useful to point to a solution not just the problem though =D –  pfrank May 2 at 20:29

As zoul pointed out in the comments, isEqual: will return NO when comparing colors that are in different models/spaces (for instance #FFF with [UIColor whiteColor]). I wrote this UIColor extension that converts both colors to the same color space before comparing them:

- (BOOL)isEqualToColor:(UIColor *)otherColor {
    CGColorSpaceRef colorSpaceRGB = CGColorSpaceCreateDeviceRGB();

    UIColor *(^convertColorToRGBSpace)(UIColor*) = ^(UIColor *color) {
        if(CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) == kCGColorSpaceModelMonochrome) {
            const CGFloat *oldComponents = CGColorGetComponents(color.CGColor);
            CGFloat components[4] = {oldComponents[0], oldComponents[0], oldComponents[0], oldComponents[1]};
            CGColorRef colorRef = CGColorCreate( colorSpaceRGB, components );

            UIColor *color = [UIColor colorWithCGColor:colorRef];
            CGColorRelease(colorRef);
            return color;            
    } else
            return color;
    };

    UIColor *selfColor = convertColorToRGBSpace(self);
    otherColor = convertColorToRGBSpace(otherColor);
    CGColorSpaceRelease(colorSpaceRGB);

    return [selfColor isEqual:otherColor];
}
share|improve this answer
 
2  
Nice, but there is a leak I think. You never release the CGColorCreate in the middle. –  borrrden Sep 27 ’12 at 2:38
    
This is a perfect solution! –  alones Nov 26 ’12 at 8:03
    
Worked pretty well –  htafoya Apr 5 ’13 at 19:07
    
Can you explain what’s going on with the carats? I don’t think I’ve seen that syntax before. –  Victor EngelJan 20 at 20:18
    
@VictorEngel, UIColor *(^convertColorToRGBSpace)(UIColor*) = ^(UIColor *color) ... is declaring a block. He uses it later with convertColorToRGBSpace(self). For an intro to blocks in iOS, see raywenderlich.com/9328/creating-a-diner-app-using-blocks-part-1 –  JRG-Developer Feb 10 at 19:11

Here’s a fix for the memory leak in samvermette’s solution:

- (BOOL) isEqualToColor:(UIColor *)otherColor
{
    if (self == otherColor)
        return YES;

    CGColorSpaceRef colorSpaceRGB = CGColorSpaceCreateDeviceRGB();

    UIColor *(^convertColorToRGBSpace)(UIColor*) = ^(UIColor *color)
    {
        if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) == kCGColorSpaceModelMonochrome)
        {
            const CGFloat *oldComponents = CGColorGetComponents(color.CGColor);
            CGFloat components[4] = {oldComponents[0], oldComponents[0], oldComponents[0], oldComponents[1]};
            CGColorRef colorRef = CGColorCreate(colorSpaceRGB, components);
            UIColor *color = [UIColor colorWithCGColor:colorRef];
            CGColorRelease(colorRef);
            return color;
        }
        else
            return color;
    };

    UIColor *selfColor = convertColorToRGBSpace(self);
    otherColor = convertColorToRGBSpace(otherColor);
    CGColorSpaceRelease(colorSpaceRGB);

    return [selfColor isEqual:otherColor];
}
share|improve this answer
 
1  
Why don’t you suggest this as an edit to sam’s post instead? –  Jesse Rusak May 18 ’13 at 20:03
1  
Because it was too long for a comment. –  Mark Krenek May 18 ’13 at 21:53
    
I didn’t mean a comment; you can click the “edit” or “improve this answer” link to suggest an edit to an answer. –  Jesse Rusak May 19 ’13 at 15:19
    
I submitted the edit –  Alex Reynolds Jul 10 at 23:21
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s