Created
October 26, 2015 09:22
-
-
Save Igor-Palaguta/23a7b9a540a6933d1d60 to your computer and use it in GitHub Desktop.
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 | |
extension UIColor { | |
convenience init(rgbaValue: UInt32) { | |
let red = CGFloat((rgbaValue >> 24) & 0xff) / 255.0 | |
let green = CGFloat((rgbaValue >> 16) & 0xff) / 255.0 | |
let blue = CGFloat((rgbaValue >> 8) & 0xff) / 255.0 | |
let alpha = CGFloat((rgbaValue ) & 0xff) / 255.0 | |
self.init(red: red, green: green, blue: blue, alpha: alpha) | |
} | |
} | |
extension UIColor { | |
enum {{enumName}} { | |
{% for color in colors %} | |
case {{color.name}} | |
{% endfor %} | |
var colorValue: UInt32 { | |
switch self { | |
{% for color in colors %} | |
case {{color.name}}: | |
return 0x{{color.hex}} | |
{% endfor %} | |
} | |
} | |
} | |
convenience init(named name: {{enumName}}) { | |
self.init(rgbaValue: name.colorValue) | |
} | |
} |
I think using a Dictionary<String,String>
to do the lookup instead of a huge switch/case
could improve performance (especially for people having tons of colors to generate)
BTW, we should use {{color.name|swift_identifier}}
there (just realized I made the same mistake in my own template bundled with SwiftGen)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Support colors with same code