Search code examples
objective-ccocoacolorsuikituicolor

Changing Color Darkness Programmatically in Objective-C


I have a image that is pure white. It is the white at the center of the Mac color wheel. I want to increase the darkness of the white programmatically. (Non programmatically would be to slide the slider down)

I need a darker white, then darker than that, etc. How would I achieve this with code?


Solution

  • Generally, when dealing with colors programmatically, I prefer to use Hue, Saturation, and Brightness. It's much easier to progress colors using increments of each parameter.

    • Hue is a float representing the percentage of 360 degrees on the color wheel (e.g. 0 to 1).
    • Saturation represents the intensity of that color and is a a float between 0 and 1, again.
    • Brightness is what you are trying to modify and will take you from pure white, to pure black. Again, a float between 0 and 1.

    UIColor provides a factory method

    - (UIColor *)initWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha
    

    Here's a link to the UIColor class reference and the above method.