Search code examples
ioscocoa-touchuikitequalityuicolor

Why does +[UIColor whiteColor] not equal another white?


This is something really strange. Comparing +[UIColor redColor] with a red I create myself gives an equal result, but comparing +[UIColor whiteColor] to another white does not.

// This test passes.
XCTAssertEqualObjects([UIColor redColor],
                      [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0],
                      @"Red should equal red.");

// While this test fails!
XCTAssertEqualObjects([UIColor whiteColor],
                      [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0],
                      @"White should equal white.");

While I'm extending UIColor with some useful additions, this fact can be really annoying.

Can somebody shed a light on this for me?


Solution

  • "UIColor" isn't always based on RGBA values.

    There are different color spaces that UIColor works with, such as CMYK colors and, in the case of white color, you can get a white color via [UIColor colorWithWhite:alpha:].

    I suspect [UIColor whiteColor] in your case is going to equal [UIColor colorWithWhite:1.0 alpha:1.0].