Adjusted Brightness of Color from UIColor
suggest changeThe below code example will give you an adjusted version of that color where a higher percentage will be brighter and a lower percentage will be darker.
Objective-C
+ (UIColor *)adjustedColorForColor:(UIColor *)c : (double)percent
{
if (percent < 0) percent = 0;
CGFloat r, g, b, a;
if ([c getRed:&r green:&g blue:&b alpha:&a])
return [UIColor colorWithRed:MAX(r * percent, 0.0)
green:MAX(g * percent, 0.0)
blue:MAX(b * percent, 0.0)
alpha:a];
return nil;
}
Swift
func adjustedColorForColor(c: UIColor, percent: CGFloat) -> UIColor? {
let percent = max(percent, 0)
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
// getRed fails for colors that are not in the RGB color space, such as pattern colors.
if c.getRed(&r, green: &g, blue: &b, alpha: &a) {
return UIColor(red: max(r * percent, 0.0), green: max(g * percent, 0.0), blue: max(b * percent, 0.0), alpha: a)
}
return nil
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents