130

I'd like to check the color set for a background on a UIImageView. I've tried:

if(myimage.backgroundColor == [UIColor greenColor]){
...}
else{
...}

but that doesn't work, even when I know the color is green, it always falls into the else part.

Also, is there a way to output the current color in the debug console.

p [myimage backgroundColor]

and

po [myimage backgroundColor]

don't work.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
4thSpace
  • 43,672
  • 97
  • 296
  • 475

20 Answers20

177

Have you tried [myColor isEqual:someOtherColor] ?

Steven Canfield
  • 7,312
  • 5
  • 35
  • 28
  • Thanks. What is the difference with isEqualTo? Also, do you know how to display it in the debugger? – 4thSpace Jun 09 '09 at 15:19
  • 100
    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
  • 2
    Good point Zoul, might be more useful to point to a solution not just the problem though =D – pfrank May 02 '14 at 20:29
  • 11
    Good point Pfrank, might be more useful to point to a solution not just the problem though =D – Albert Renshaw May 29 '15 at 22:45
  • Did not always work for me when comparing a button.tintColor to the color I had set it to. Had to do with rounding. If you run into this see my answer below. – Pbk Jun 11 '15 at 08:51
  • As for a solution, I have not tested it yet, but perhaps getting the `CGColor` of the UIColor will fix the comparison issue, since that should just get right down into the values of the colors and not their types. – Albert Renshaw Jun 28 '16 at 02:17
78

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];
}
Stunner
  • 12,025
  • 12
  • 86
  • 145
samvermette
  • 40,269
  • 27
  • 112
  • 144
  • 2
    Nice, but there is a leak I think. You never release the CGColorCreate in the middle. – borrrden Sep 27 '12 at 02:38
  • This is a perfect solution! – alones Nov 26 '12 at 08:03
  • Can you explain what's going on with the carats? I don't think I've seen that syntax before. – Victor Engel Jan 20 '14 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 http://www.raywenderlich.com/9328/creating-a-diner-app-using-blocks-part-1 – JRG-Developer Feb 10 '14 at 19:11
  • 1
    I use blocks all the tine, but I've never seen syntax quite like this before. I guess maybe I've not used a block that returned an object. It's what's on the left side of the = that I was posting about. – Victor Engel Feb 12 '14 at 15:39
  • Great solution, but personally I dislike the use of blocks in this way, so I modified it just to be a function. It has to accept the colorSpace as an argument of course, but it works fine and I'm sure it's more efficient if you're doing this often. – Echelon Jan 06 '15 at 12:41
65

This might be a bit too late, but CoreGraphics has an easier API to achieve this:

CGColorEqualToColor(myColor.CGColor, [UIColor clearColor].CGColor)

Like the documentation says:

Indicates whether two colors are equal. Two colors are equal if they have equal color spaces and numerically equal color components.

This solves a lot trouble and leaking/custom algorithms.

DZenBot
  • 4,806
  • 2
  • 25
  • 27
  • 5
    `[UIColor isEqual:]` does the same thing as this, does it not? Mark's answer is still the only one that checks equivalency despite color space well. – mattsven Nov 16 '14 at 14:19
  • 9
    This doesn't help any more than `UIColor isEqual:` if the colours are in different colour spaces. – Echelon Jan 06 '15 at 12:42
  • The question is regarding UIColor comparison, not CGColor comparison. – Sean Vikoren Feb 17 '17 at 00:23
  • More accurate than all other solutions :) +1 from me – Nirav Gadhiya Jul 03 '17 at 16:43
  • WRONG. 2 black colors and it prints NO NSColor *color1 = [NSColor blackColor]; NSColor *color2 = [NSColor colorWithDeviceWhite:0 alpha:1]; NSLog(@"isEqual %d", CGColorEqualToColor(color1.CGColor, color2.CGColor)); – Marek H Dec 15 '21 at 14:57
8

samvermette's solution translated to swift:

extension UIColor {
    func isEqualToColor(otherColor : UIColor) -> Bool {
        if self == otherColor {
            return true
        }

        let colorSpaceRGB = CGColorSpaceCreateDeviceRGB()
        let convertColorToRGBSpace : ((color : UIColor) -> UIColor?) = { (color) -> UIColor? in
            if CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) == CGColorSpaceModel.Monochrome {
                let oldComponents = CGColorGetComponents(color.CGColor)
                let components : [CGFloat] = [ oldComponents[0], oldComponents[0], oldComponents[0], oldComponents[1] ]
                let colorRef = CGColorCreate(colorSpaceRGB, components)
                let colorOut = UIColor(CGColor: colorRef!)
                return colorOut
            }
            else {
                return color;
            }
        }

        let selfColor = convertColorToRGBSpace(color: self)
        let otherColor = convertColorToRGBSpace(color: otherColor)

        if let selfColor = selfColor, otherColor = otherColor {
            return selfColor.isEqual(otherColor)
        }
        else {
            return false
        }
    }
}
serenn
  • 1,828
  • 1
  • 15
  • 11
raf
  • 2,569
  • 20
  • 19
7
#import "UIColor-Expanded.h"
//https://github.com/thetaplab/uicolor-utilities

//RGB distance
CGFloat distance = sqrtf(powf((clr0.red - clr1.red), 2) + powf((clr0.green - clr1.green), 2) + powf((clr0.blue - clr1.blue), 2) );
if(distance<=minDistance){
....
}else{
...
}
7

This UIColor extension works fine provided that the compared colors can be converted into RGB format, which should be most of the cases.

public extension UIColor {

    static func == (l: UIColor, r: UIColor) -> Bool {
        var l_red = CGFloat(0); var l_green = CGFloat(0); var l_blue = CGFloat(0); var l_alpha = CGFloat(0)
        guard l.getRed(&l_red, green: &l_green, blue: &l_blue, alpha: &l_alpha) else { return false }
        var r_red = CGFloat(0); var r_green = CGFloat(0); var r_blue = CGFloat(0); var r_alpha = CGFloat(0)
        guard r.getRed(&r_red, green: &r_green, blue: &r_blue, alpha: &r_alpha) else { return false }
        return l_red == r_red && l_green == r_green && l_blue == r_blue && l_alpha == r_alpha
    }
}

At least with this extension:

UIColor.whiteColor == UIColor(hex: "#FFFFFF") // true
UIColor.black == UIColor(red: 0, green: 0, blue: 0, alpha: 1) // true

Both comparisons would return false if compared using the native UColor.isEqual(...)

boherna
  • 659
  • 8
  • 7
5

I wrote this category. If isEqual: does return NO, it will test if further comparison of different components might still match. If possible, different models are still compared.

@implementation UIColor (Matching)


-(BOOL)matchesColor:(UIColor *)color error:(NSError *__autoreleasing *)error
{
    UIColor *lhs = self;
    UIColor *rhs = color;

    if([lhs isEqual:rhs]){ // color model and values are the same
        return YES;
    }

    CGFloat red1, red2, green1, alpha1, green2, blue1, blue2, alpha2;
    BOOL lhsSuccess = [lhs getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
    BOOL rhsSuccess = [rhs getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];
    if((!lhsSuccess && rhsSuccess) || (lhsSuccess && !rhsSuccess)){ // one is RGBA, one color not.
        CGFloat r,g,b,a;
        if(!lhsSuccess){ // lhs color could be a monochrome
            const CGFloat *components = CGColorGetComponents(lhs.CGColor);
            if([lhs _colorSpaceModel] == kCGColorSpaceModelMonochrome){
                r = g = b = components[0];
                a = components[1];

                return r == red2 && g == green2 && b == blue2 && a == alpha2;
            }
        } else {  // rhs color could be a monochrome
            const CGFloat *components = CGColorGetComponents(rhs.CGColor);

            if([rhs _colorSpaceModel] == kCGColorSpaceModelMonochrome){
                r = g = b = components[0];
                a = components[1];
                return r == red1 && g == green1 && b == blue1 && a == alpha1;
            }
        }


        NSError *aError = [[NSError alloc] initWithDomain:@"UIColorComparision" code:-11111 userInfo:[self _colorComparisionErrorUserInfo]];
        *error = aError;
        return  NO;
    } else if (!lhsSuccess && !rhsSuccess){ // both not RGBA, lets try HSBA
        CGFloat hue1,saturation1,brightness1;
        CGFloat hue2,saturation2,brightness2;

        lhsSuccess = [lhs getHue:&hue1 saturation:&saturation1 brightness:&brightness1 alpha:&alpha1];
        rhsSuccess = [lhs getHue:&hue2 saturation:&saturation2 brightness:&brightness2 alpha:&alpha2];
        if((!lhsSuccess && rhsSuccess) || (lhsSuccess && !rhsSuccess)){
            NSError *aError = [[NSError alloc] initWithDomain:@"UIColorComparision" code:-11111 userInfo:[self _colorComparisionErrorUserInfo]];
            *error = aError;
            return  NO;
        } else if(!lhsSuccess && !rhsSuccess){ // both not HSBA, lets try monochrome
            CGFloat white1, white2;

            lhsSuccess = [lhs getWhite:&white1 alpha:&alpha1];
            rhsSuccess = [rhs getWhite:&white2 alpha:&alpha2];
            if((!lhsSuccess && rhsSuccess) || (lhsSuccess && !rhsSuccess)){
                NSError *aError = [[NSError alloc] initWithDomain:@"UIColorComparision" code:-11111 userInfo:[self _colorComparisionErrorUserInfo]];
                *error = aError;
                return  NO;
            } else {
                return white1 == white2 && alpha1 == alpha2;
            }

        } else {
            return hue1 == hue2 && saturation1 == saturation2 && brightness1 == brightness2 && alpha1 == alpha2;
        }

    } else {
        return (red1 == red2 && green1 == green2 && blue1 == blue2 && alpha1 == alpha2);

    }
}

-(NSDictionary *)_colorComparisionErrorUserInfo{

    NSDictionary *userInfo = @{
                               NSLocalizedDescriptionKey: NSLocalizedString(@"Comparision failed.", nil),
                               NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"The colors models are incompatible. Or the color is a pattern.", nil),

                               };
    return userInfo;
}

- (CGColorSpaceModel)_colorSpaceModel {
    return CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
}

@end

UIColor *green1 = [UIColor greenColor];
UIColor *green2 = [UIColor colorWithRed:0 green:1 blue:0 alpha:1];
UIColor *yellow = [UIColor yellowColor];
UIColor *grey1  = [UIColor colorWithWhite:2.0/3.0 alpha:1];
UIColor *grey2  = [UIColor lightGrayColor];

NSError *error1, *error2, *error3, *error4, *error5;

BOOL match1 = [green1 matchesColor:green2 error:&error1];   // YES
BOOL match2 = [green1 matchesColor:yellow error:&error2];   // NO
BOOL match3 = [green1 matchesColor:grey1 error:&error3];    // NO
BOOL match4 = [grey1 matchesColor:grey2 error:&error4];     // YES
BOOL match5 = [grey1 matchesColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]
                            error:&error5];                 // NO, Error
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
2

When you're comparing myimage.backgroundColor == [UIColor greenColor] like this if you havent change the backgroundColor to green before that statement it is not working.

I had same problem in my color game and i solved that by using simple difference equation in RGB colors you can quick take a look that short code sample ColorProcess from here

its like victors answer

GFloat distance = sqrtf(powf((clr0.red - clr1.red), 2) + powf((clr0.green - clr1.green), 2) + powf((clr0.blue - clr1.blue), 2) );
if(distance<=minDistance){
....
}else{
…
}

Instead of that code sample you can use

include "UIColorProcess.h"

..

float distance = [UIColorProcess findDistanceBetweenTwoColor:[UIColor redColor] secondColor:[UIColor blueColor]];

and of course if it returns 0 that means you are comparing too similar color. return range is something like (0.0f - 1.5f)..

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
mert
  • 1,090
  • 13
  • 26
  • color.red/blue/green aren't supported for all different types of color classes, check the docs – pfrank May 02 '14 at 20:30
1

Some weird rounding errors can occur. That can be the reason a object set to a color and the color you set it to do not match exactly.

This is how I solved it:

private func compareColors (c1:UIColor, c2:UIColor) -> Bool{
    // some kind of weird rounding made the colors unequal so had to compare like this

    var red:CGFloat = 0
    var green:CGFloat  = 0
    var blue:CGFloat = 0
    var alpha:CGFloat  = 0
    c1.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

    var red2:CGFloat = 0
    var green2:CGFloat  = 0
    var blue2:CGFloat = 0
    var alpha2:CGFloat  = 0
    c2.getRed(&red2, green: &green2, blue: &blue2, alpha: &alpha2)

    return (Int(green*255) == Int(green2*255))

}

This code can be improved by not only comparing 1 but comparing all the components. Eg red+green+blue+alpha == red2+green2+blue2+alpha2

Pbk
  • 2,004
  • 14
  • 11
  • 1
    Colors aren't the same if compared by green component only – pronebird Oct 22 '16 at 15:42
  • This is the only answer that mentions and attempts to address potential rounding errors that can crop up while comparing colors that are set via different means; so, while it only focuses on green, the example (as the author of the answer mentions) can be generalized. As such, and since rounding was in fact the problem I was running into, I did find this answer helpful. – Matt Wagner Oct 31 '17 at 02:10
1

I'm using this extension which is working for me in all cases.

/***** UIColor Extension to Compare colors as string *****/
@interface UIColor (compare)
- (BOOL)compareWithColor:(UIColor *)color;
@end

@implementation UIColor(compare)
- (BOOL)compareWithColor:(UIColor *)color {
    return ([[[CIColor colorWithCGColor:self.CGColor] stringRepresentation] isEqualToString:[[CIColor colorWithCGColor:color.CGColor] stringRepresentation]]);
}
@end
/**** End ****/

Hope helps some one.

Note: #ffffff does equal [UIColor whiteColor] by this extension

arunit21
  • 670
  • 1
  • 11
  • 25
1

I have a similar but polished and cleaner answer that is easy to read and use:

extension UIColor {
   var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
       var red = CGFloat.zero
       var green = CGFloat.zero
       var blue = CGFloat.zero
       var alpha = CGFloat.zero
    
       guard getRed(&red, green: &green, blue: &blue, alpha: &alpha) else {
           debugPrint("color could not be retrieved")
           return (1.0, 1.0, 1.0, 1.0)
       }
       return (red, green, blue, alpha)
   }

   static func == (lhs: UIColor, rhs: UIColor) -> Bool {
       return  lhs.rgba == rhs.rgba
   }
}

you can use it like this: (quote from boherna)

UIColor.whiteColor == UIColor(hex: "#FFFFFF") // true
UIColor.black == UIColor(red: 0, green: 0, blue: 0, alpha: 1) // true
div-ane
  • 41
  • 7
0

What about:

+(BOOL)color:(UIColor *)color1 matchesColor:(UIColor *)color2
{
    CGFloat red1, red2, green1, green2, blue1, blue2, alpha1, alpha2;
    [color1 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
    [color2 getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];

    return (red1 == red2 && green1 == green2 && blue1 == blue2 && alpha1 == alpha2);
}
dooleyo
  • 872
  • 1
  • 9
  • 13
  • this might not work for edge cases as a pattern color. from doc *If the color is in a compatible color space, the color is converted into RGB format and its components are returned to your application. If the color is not in a compatible color space, the parameters are unchanged.* So you could instantiate each float with `-1` and if any still has this value after calling `getRed:green:blue:alpha:`, you know the comparison failed. (or don't omit the returned boolean as it will tell you if it was called successfully.) – vikingosegundo Mar 30 '14 at 00:40
  • 1
    I just checked: although it is possible to check monochrome against RGBA colors in theory, it won't work with your code, as `getRed:free:blue:alpha:` wont succeed on a monochrome color. So your code won't yield another result than `is equal:`. please see my answer how to deal with it. – vikingosegundo Mar 30 '14 at 02:20
  • +1 for thoroughness! The above code works for my simple case, but it would appear it is no silver bullet. Good stuff. – dooleyo Mar 31 '14 at 18:18
0

Here is an extension to switch to the RGC Space Color in Swift:

extension UIColor {

func convertColorToRGBSpaceColor() -> UIColor {
    let colorSpaceRGB = CGColorSpaceCreateDeviceRGB()
    let oldComponents = CGColorGetComponents(self.CGColor)
    let components = [oldComponents[0], oldComponents[0], oldComponents[0], oldComponents[1]]
    let colorRef = CGColorCreate(colorSpaceRGB, components)
    let convertedColor = UIColor(CGColor: colorRef!)
    return convertedColor
}

}

RyanPliske
  • 391
  • 3
  • 7
0

Extension to UIColor, using Swift 2.2 features. Note however that because the R G B A values are compared, and these are CGFloat, rounding errors can make that the colours are non returned as equal if they are not exactly the same (e.g. they have not been originally created using the exact same properties in the init(...)!).

/**
 Extracts the RGBA values of the colors and check if the are the same.
 */

public func isEqualToColorRGBA(color : UIColor) -> Bool {
    //local type used for holding converted color values
    typealias colorType = (red : CGFloat, green : CGFloat, blue : CGFloat, alpha : CGFloat)
    var myColor         : colorType = (0,0,0,0)
    var otherColor      : colorType = (0,0,0,0)
    //getRed returns true if color could be converted so if one of them failed we assume that colors are not equal
    guard getRed(&myColor.red, green: &myColor.green, blue: &myColor.blue, alpha: &myColor.alpha) &&
        color.getRed(&otherColor.red, green: &otherColor.green, blue: &otherColor.blue, alpha: &otherColor.alpha)
        else {
            return false
    }
    log.debug("\(myColor) = \(otherColor)")
    //as of Swift 2.2 (Xcode 7.3.1), tuples up to arity 6 can be compared with == so this works nicely
    return myColor == otherColor
}
HixField
  • 3,538
  • 1
  • 28
  • 54
0

UIColor extension

- (CGFloat)accuracyCompareWith:(UIColor *)color {
    CIColor *c1 = [[CIColor alloc] initWithColor:self];
    CIColor *c2 = [[CIColor alloc] initWithColor:color];

    BOOL hasAlpha = c1.numberOfComponents == 4 && c2.numberOfComponents == 4;
    NSInteger numberOfComponents = hasAlpha ? 4 : 3;

    CGFloat colorMax = 1.0;
    CGFloat p = colorMax / 100.0;

    CGFloat redP = fabs(c1.red / p - c2.red / p);
    CGFloat greenP = fabs(c1.green / p - c2.green / p);
    CGFloat blueP = fabs(c1.blue / p - c2.blue / p);
    CGFloat alphaP = 0;

    if (hasAlpha)
        alphaP = fabs(c1.alpha / p - c2.alpha / p);

    return (redP + greenP + blueP + alphaP) / (CGFloat)numberOfComponents;
}
uranpro
  • 85
  • 1
  • 7
0

I have converted raf's answer to Swift 4 (lots of changes in the CGColor API), removed force unwrapping and decreased indentation thanks to generous use of guard:

@extension UIColor {
    func isEqualToColor(otherColor: UIColor) -> Bool {
        if self == otherColor {
            return true
        }
        let colorSpaceRGB = CGColorSpaceCreateDeviceRGB()
        let convertColorToRGBSpace: ((UIColor) -> UIColor?) = { (color) -> UIColor? in
            guard color.cgColor.colorSpace?.model == .monochrome else {
                return color
            }
            guard let oldComponents = color.cgColor.components else {
                return nil
            }
            let newComponents = [oldComponents[0], oldComponents[0], oldComponents[0], oldComponents[1]]
            guard let colorRef = CGColor(colorSpace: colorSpaceRGB, components: newComponents) else {
                    return nil
            }
            return UIColor(cgColor: colorRef)
        } 

        guard let selfColor = convertColorToRGBSpace(self), 
              let otherColor = convertColorToRGBSpace(otherColor) else {
            return false
        }
        return selfColor.isEqual(otherColor)
    }
}
dsapalo
  • 1,819
  • 2
  • 19
  • 37
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
0

Why not add the extension with equatable protocol? This answer is using the solution of Nicolas Miari. So, if you like this answer, please be welcome to like his answer (second from the top)

The comment of Zoul: 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]

static func == (lhs: UIColor, rhs: UIColor) -> Bool {

    let colorSpaceRGB = CGColorSpaceCreateDeviceRGB()
    let convertColorToRGBSpace: ((UIColor) -> UIColor?) = { (color) -> UIColor? in
        guard color.cgColor.colorSpace?.model == .monochrome else {
            return color
        }
        guard let oldComponents = color.cgColor.components else {
            return nil
        }
        let newComponents = [oldComponents[0], oldComponents[0], oldComponents[0], oldComponents[1]]
        guard let colorRef = CGColor(colorSpace: colorSpaceRGB, components: newComponents) else {
            return nil
        }
        return UIColor(cgColor: colorRef)
    }

    guard let selfColor = convertColorToRGBSpace(lhs),
        let otherColor = convertColorToRGBSpace(rhs) else {
            return false
    }
    return selfColor.isEqual(otherColor)
}
Illya Krit
  • 925
  • 1
  • 8
  • 9
0

While @samvermette's answer is very good, I've found that it can sometimes lead to false negatives when comparing different color types (in my case UIDeviceRGBColor to UICachedDeviceWhiteColor). I fixed it by explicitly creating the color in the `else', too:

- (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
        {
            const CGFloat *oldComponents = CGColorGetComponents(color.CGColor);
            CGFloat components[4] = {oldComponents[0], oldComponents[1], oldComponents[2], oldComponents[3]};
            CGColorRef colorRef = CGColorCreate(colorSpaceRGB, components);
            UIColor *color = [UIColor colorWithCGColor:colorRef];
            CGColorRelease(colorRef);
            return color;
        }
    };

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

    return [selfColor isEqual:otherColor];
}
AXE
  • 8,335
  • 6
  • 25
  • 32
0

You have to use

BOOL equalColors = CGColorEqualToColor(uiColor1.CGColor, uiColor2.CGColor));

Documentation here.

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
-1
if([myimage.backgroundColor isEqual:[UIColor greenColor]])
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200