Skip to content

Instantly share code, notes, and snippets.

@nickffox
Created January 18, 2017 15:10
Show Gist options
  • Save nickffox/3a326f80eebb12ae6c12e5d85917923b to your computer and use it in GitHub Desktop.
Save nickffox/3a326f80eebb12ae6c12e5d85917923b to your computer and use it in GitHub Desktop.
convenience init(hexString: String) {
let hexString = hexString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
let scanner = NSScanner(string: hexString)
if (hexString.hasPrefix("#")) {
scanner.scanLocation = 1
}
var color: UInt32 = 0
scanner.scanHexInt(&color)
let mask = 0x000000FF
let r = Int(color >> 16) & mask
let g = Int(color >> 8) & mask
let b = Int(color) & mask
let red = CGFloat(r) / 255.0
let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255.0
self.init(red:red, green:green, blue:blue, alpha:1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment