Created
August 9, 2018 15:18
-
-
Save vialyx/dd50e481b325cf3ea74090ac81f89624 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.UIColor | |
extension UIColor { | |
convenience init(hex: String) { | |
// Initializer supports work with formatts: "#FFFFFF", "FFFFFF" | |
let cString: String = hex.trimmingCharacters(in: .whitespacesAndNewlines) | |
.uppercased() | |
.replacingOccurrences(of: "#", with: "") | |
guard cString.count == 6 else { | |
self.init(white: 1.0, alpha: 1.0) | |
return | |
} | |
let scanner = Scanner(string: cString) | |
var rgbValue: UInt32 = 0 | |
scanner.scanHexInt32(&rgbValue) | |
self.init( | |
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, | |
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, | |
blue: CGFloat(rgbValue & 0x0000FF) / 255.0, | |
alpha: CGFloat(1.0) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment