Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Making new UIColors easier
//Extension to make it easy for UIColor init
extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
let newRed = CGFloat(red)/255
let newGreen = CGFloat(green)/255
let newBlue = CGFloat(blue)/255
self.init(red: newRed, green: newGreen, blue: newBlue, alpha: 1.0)
}
}
//With the extension
let newSwiftColor = UIColor(red: 255, green: 165, blue: 0)
//With fancy Swift Numeric Literals
let fancySwiftColor = UIColor(red: 0xFF, green: 0xA5, blue: 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment