Skip to content

Instantly share code, notes, and snippets.

@trilliwon
Created July 29, 2020 10:26
Show Gist options
  • Save trilliwon/fd80473e8398ff5f163baa00a42babdf to your computer and use it in GitHub Desktop.
Save trilliwon/fd80473e8398ff5f163baa00a42babdf to your computer and use it in GitHub Desktop.
Construct UIColor instance from hex value
extension UIColor {
/// UIColor(hex: 0x000000)
convenience init(hex: Int, alpha: CGFloat = 1) {
let r = CGFloat((hex & 0xFF0000) >> 16) / 255
let g = CGFloat((hex & 0xFF00) >> 8) / 255
let b = CGFloat((hex & 0xFF)) / 255
self.init(red: r, green: g, blue: b, alpha: alpha)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment