Skip to content

Instantly share code, notes, and snippets.

@dimkagithub
Last active November 6, 2021 06:52
Show Gist options
  • Save dimkagithub/db593e438532e71a5af8b02f3230e676 to your computer and use it in GitHub Desktop.
Save dimkagithub/db593e438532e71a5af8b02f3230e676 to your computer and use it in GitHub Desktop.
UIColor extension
extension UIColor {
func setLighter(by percentage: CGFloat = 30.0) -> UIColor? {
return self.adjust(by: abs(percentage) )
}
func setDarker(by percentage: CGFloat = 30.0) -> UIColor? {
return self.adjust(by: -1 * abs(percentage) )
}
func adjust(by percentage: CGFloat = 30.0) -> UIColor? {
var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, alpha: CGFloat = 0.0
if self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) {
return UIColor(red: min(red + percentage / 100, 1.0),
green: min(green + percentage / 100, 1.0),
blue: min(blue + percentage / 100, 1.0),
alpha: alpha)
} else {
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment