Skip to content

Instantly share code, notes, and snippets.

@tmspzz
Last active June 12, 2019 09:19
Show Gist options
  • Save tmspzz/e644351ee25b3a9a4ecce657d440d650 to your computer and use it in GitHub Desktop.
Save tmspzz/e644351ee25b3a9a4ecce657d440d650 to your computer and use it in GitHub Desktop.
Dynamic Member Lookup Colors
@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