Gradient helper: Adjusting color brightness

F0616

Threw this together today to help with my gradients chapter. The idea is that you work with UIColors and tweak the brightness for a contrast.

Thought it might be useful to others, so I decided to add it to a post. Let me know if it’s at all handy.


CGFloat Clamp(CGFloat a, 
    CGFloat min, CGFloat max)
{
    return fmin(fmax(min, a), max);
}

UIColor *ScaleColorBrightness(UIColor *color, CGFloat amount)
{
    CGFloat h, s, v, a;
    [color getHue:&h saturation:&s brightness:&v alpha:&a];
    CGFloat v1 = Clamp(v * amount, 0, 1);
    return [UIColor colorWithHue:h saturation:s brightness:v1 alpha:a];
}

Comments are closed.