Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 654
s6luwJ0A3I Avatar asked Mar 15 '13 09:03

s6luwJ0A3I


People also ask

Why can't I change the Hue saturation on Photoshop?

Photoshop's Hue/Saturation tool is designed to work with 8-bit images. When you try to use the tool on an image that is not 8-bit, you will see an error message. The fix for this is to open your image in Photoshop and go to Image > Mode and make sure that your image is in 8-bit mode.


2 Answers

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 ] ;
like image 132
nielsbot Avatar answered Nov 15 '22 14:11

nielsbot


Here is swift UIColor extension you might find useful:

extension UIColor {

    func modified(withAdditionalHue hue: CGFloat, additionalSaturation: CGFloat, additionalBrightness: CGFloat) -> UIColor {

        var currentHue: CGFloat = 0.0
        var currentSaturation: CGFloat = 0.0
        var currentBrigthness: CGFloat = 0.0
        var currentAlpha: CGFloat = 0.0

        if self.getHue(&currentHue, saturation: &currentSaturation, brightness: &currentBrigthness, alpha: &currentAlpha){
            return UIColor(hue: currentHue + hue,
                           saturation: currentSaturation + additionalSaturation,
                           brightness: currentBrigthness + additionalBrightness,
                           alpha: currentAlpha)
        } else {
            return self
        }
    }
}
like image 30
ambientlight Avatar answered Nov 15 '22 15:11

ambientlight