Skip to content

Instantly share code, notes, and snippets.

@Igor-Palaguta
Created October 26, 2015 09:22
Show Gist options
  • Save Igor-Palaguta/23a7b9a540a6933d1d60 to your computer and use it in GitHub Desktop.
Save Igor-Palaguta/23a7b9a540a6933d1d60 to your computer and use it in GitHub Desktop.
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)
}
}
@Igor-Palaguta
Copy link
Author

Support colors with same code

@AliSoftware
Copy link

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)

@AliSoftware
Copy link

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