Search code examples
iphonecocoa-touchcocoaios5uikit

Why brightness of [UIColor whiteColor] and blackColor are equal


Let my show you some results of getting hue, saturation and brightness of three UIColors.

[[UIColor redColor] getHue:&hue
                saturation:&saturation
                 brightness:&brightness
                 alpha:&alpha];

hue = 1.0 saturatino = 1.0 brightness = 1.0 alpha = 0.0

[[UIColor whiteColor] getHue:&hue
                  saturation:&saturation
                  brightness:&brightness
                       alpha:&alpha];

hue = 0.0 saturatino = 0.0 brightness = 0.0 alpha = 0.0

[[UIColor blackColor] getHue:&hue
                  saturation:&saturation
                  brightness:&brightness
                       alpha:&alpha];

hue = 0.0 saturatino = 0.0 brightness = 0.0 alpha = 0.0

Can anyone explain why hue, saturation, brightness of white and black color are equal? Why alpha is equals zero?

What I wanted to do in my project is generate 'darker' color from a given color by changing it brightness:

brightness = brightness * 0.8;

It works fine for any color, but it produces black color from white color. (Although I would expect a grey color).


Solution

  • The reason is because +whiteColor and +blackColor both return colors in the greyscale colorspace, which is not compatible with the HSB colorspace. As such, -getHue:saturation:brightness:alpha: is actually not modifying the parameters. I think you'll find you have them all set to 0.0 before calling that method. If you check the return value of -getHue:saturation:brightness:alpha: it will tell you if it successfully converted to HSB.