Last active
June 12, 2019 09:19
-
-
Save tmspzz/e644351ee25b3a9a4ecce657d440d650 to your computer and use it in GitHub Desktop.
Dynamic Member Lookup Colors
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
@dynamicMemberLookup | |
struct Color { | |
subscript(dynamicMember member: String) -> UIColor { | |
guard let intValue = Int(member.dropFirst().dropFirst(), radix: 16) else { return .black } | |
return UIColor(hexValue: intValue) | |
} | |
} | |
extension UIColor { | |
convenience init(hexValue: Int) { | |
self.init(red: CGFloat(hexValue >> 16 & 0xFF)/255 | |
, green: CGFloat(hexValue >> 8 & 0xFF)/255 | |
, blue: CGFloat(hexValue & 0xFF)/255, alpha: 1) | |
} | |
} | |
// Color().0xFF0000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment