How can I modify a UIColor's hue, brightness and saturation?

s6luwJ0A3I picture s6luwJ0A3I · Mar 15, 2013 · Viewed 13.2k times · Source

Lets say I have a UIColor

UIColor *color = [UIColor redColor];

Now I want to modify the saturation/hue/brigthness, how do I do that? I did read the documentation but i'm still really confused

I want to modify the UIColor I made ([UIColor redColor]) not initiate a new color with some preferences. How do I modify it retaining the original. I do know about thecolorWithHue:saturation:brightness:alpha: method, I need to update an existing color's properties, keeping the red color.

Answer

nielsbot picture nielsbot · Mar 15, 2013

You can call getHue:saturation:brightness:alpha: on your color, then adjust the values, then create a new color with your adjusted components using +[UIColor colorWithHue:saturation:brightness:alpha:]

CGFloat hue, saturation, brightness, alpha ;
BOOL ok = [ <color> getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha ] ;
if ( !ok ) { 
    // handle error 
}
// ... adjust components..

UIColor * newColor = [ UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha ] ;