Pregunta

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?

¿Fue útil?

Solución

"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].

Otros consejos

One likely explanation is the color space used by the distinct color instances. White can be represented in many distinct domains, or spaces. An equality comparison may be negative if the color spaces are not equal, or if the spaces are not easy to transform to another space (e.g. transforming an absolute representation to device representation may result in a loss of information).

Color representations are complex; accurate "comparisons" deserve multiple methods to compare colors and many should include render destinations.

[UIColor whiteColor] does not produce an RGB color space object -- it is in a grayscale color space. Try

NSLog(@"color is %@", [UIColor whiteColor]);

Very annoying.

You can, of course, make a white color with RGB all equal to 1.0, but it isn't the same color object as [UIColor whiteColor].

[UIColor whiteColor] is of type UICachedDeviceWhiteColor, while [UIColor colorWithRed:green:blue:alpha:] returns UIDeviceRGBColor at runtime, therefor they are not equal.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top