Skip to content

Instantly share code, notes, and snippets.

@adamrothman
Created July 27, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamrothman/d30fa3c6ebc015e33e143544272090ca to your computer and use it in GitHub Desktop.
Save adamrothman/d30fa3c6ebc015e33e143544272090ca to your computer and use it in GitHub Desktop.
extension UIColor {
convenience init(hex: UInt32, alpha: CGFloat = 1.0) {
self.init(
red: CGFloat((hex & 0xff0000) >> 16) / 256.0,
green: CGFloat((hex & 0x00ff00) >> 8) / 256.0,
blue: CGFloat(hex & 0x0000ff) / 256.0,
alpha: alpha
)
}
convenience init(hexString: String, alpha: CGFloat = 1.0) {
let scanner = NSScanner(string: hexString)
scanner.charactersToBeSkipped = NSCharacterSet(charactersInString: "#")
var hex: UInt32 = 0
scanner.scanHexInt(&hex)
self.init(hex: hex, alpha: alpha)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment