Skip to content

Instantly share code, notes, and snippets.

@lfarah
Created May 14, 2018 13:25
Show Gist options
  • Save lfarah/b061c1603240b82b4ed1f3ef48c616e2 to your computer and use it in GitHub Desktop.
Save lfarah/b061c1603240b82b4ed1f3ef48c616e2 to your computer and use it in GitHub Desktop.
extension UIColor {
func lighter(by percentage:CGFloat=30.0) -> UIColor? {
return self.adjust(by: abs(percentage) )
}
func darker(by percentage:CGFloat=30.0) -> UIColor? {
return self.adjust(by: -1 * abs(percentage) )
}
func adjust(by percentage:CGFloat=30.0) -> UIColor? {
var r:CGFloat=0, g:CGFloat=0, b:CGFloat=0, a:CGFloat=0;
if(self.getRed(&r, green: &g, blue: &b, alpha: &a)){
return UIColor(red: min(r + percentage/100, 1.0),
green: min(g + percentage/100, 1.0),
blue: min(b + percentage/100, 1.0),
alpha: a)
}else{
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment