Created
May 18, 2018 01:27
-
-
Save rtimpone/ede67e37b61b51aead796a60c89f2770 to your computer and use it in GitHub Desktop.
Create a UIColor from hex values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIColor { | |
convenience init(hex: String) { | |
let trimmedString = hex.replacingOccurrences(of: "#", with: "") | |
var uint32Value: UInt32 = 0 | |
Scanner(string: trimmedString).scanHexInt32(&uint32Value) | |
let intValue = Int(uint32Value) | |
self.init(hex: intValue) | |
} | |
convenience init(hex: Int) { | |
let r = CGFloat((hex >> 16) & 0xff) / 255 | |
let g = CGFloat((hex >> 08) & 0xff) / 255 | |
let b = CGFloat((hex >> 00) & 0xff) / 255 | |
self.init(red: r, green: g, blue: b, alpha: 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment