Skip to content

Instantly share code, notes, and snippets.

@ptrkstr
Last active December 19, 2016 22:54
Show Gist options
  • Save ptrkstr/d0a7cb5a0e0819f56b7e59b9382fb7f3 to your computer and use it in GitHub Desktop.
Save ptrkstr/d0a7cb5a0e0819f56b7e59b9382fb7f3 to your computer and use it in GitHub Desktop.
NSColor extension
extension NSColor {
/// Creates a NSColor from "#XXXXXX"/"XXXXXX" format
convenience init(hex: String, alpha: CGFloat = 1) {
// TODO: Validate hex string is in the "#XXXXXX" or "XXXXXX" format
let scanner = Scanner(string: hex)
scanner.scanLocation = hex[hex.startIndex] == "#" ? 1 : 0
var rgb: UInt32 = 0
scanner.scanHexInt32(&rgb)
self.init(red: CGFloat((rgb & 0xFF0000) >> 16)/255.0, green: CGFloat((rgb & 0xFF00) >> 8)/255.0, blue: CGFloat(rgb & 0xFF)/255.0, alpha: alpha)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment