Created
May 3, 2020 12:12
-
-
Save hlung/261f713bf9a83076cb0e140e650870e1 to your computer and use it in GitHub Desktop.
Create UIColor from hex string. e.g. UIColor(hex: "EAEAEA")
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
import UIKit | |
public extension UIColor { | |
convenience init(hex: String) { | |
let r, g, b, a: CGFloat | |
var hex = hex | |
if hex.hasPrefix("#") { hex = String(hex.dropFirst()) } | |
if hex.count == 6 { | |
let scanner = Scanner(string: hex) | |
var hexNumber: UInt64 = 0 | |
if scanner.scanHexInt64(&hexNumber) { | |
r = CGFloat((hexNumber & 0xff0000) >> 16) / 255 | |
g = CGFloat((hexNumber & 0x00ff00) >> 8) / 255 | |
b = CGFloat((hexNumber & 0x0000ff)) / 255 | |
a = 1.0 | |
self.init(red: r, green: g, blue: b, alpha: a) | |
return | |
} | |
} | |
assertionFailure("Failed to parse color from string: \(hex)") | |
self.init(red: 0, green: 0, blue: 0, alpha: 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment