Skip to content

Instantly share code, notes, and snippets.

@Revolucent
Last active July 18, 2017 02:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Revolucent/ea3959b9175c27ee98cf to your computer and use it in GitHub Desktop.
Save Revolucent/ea3959b9175c27ee98cf to your computer and use it in GitHub Desktop.
Convert a six-digit hex literal into a color
extension UIColor {
convenience init(hex: UInt, alpha: CGFloat = 1.0) {
let red = CGFloat((hex & 0xFF0000) >> 0x10) / 255.0
let green = CGFloat((hex & 0x00FF00) >> 0x8) / 255.0
let blue = CGFloat(hex & 0x0000FF) / 255.0
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
}
@Revolucent
Copy link
Author

Pretty straightforward:

let color = UIColor(hex: 0xEFEFEF, alpha: 0.6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment