Skip to content

Instantly share code, notes, and snippets.

@marcos1262
Last active October 27, 2018 14:07
Show Gist options
  • Save marcos1262/808b8deee9e1f3edc9de65773fbe4296 to your computer and use it in GitHub Desktop.
Save marcos1262/808b8deee9e1f3edc9de65773fbe4296 to your computer and use it in GitHub Desktop.
Get UIColor from RGB or Hex value
extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
convenience init(rgb: Int) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment