How can I include alpha inside this UIColor extension?
extension UIColor {
convenience init(hex:Int) {
self.init(red:(hex >> 16) & 0xff, green:(hex >> 8) & 0xff, blue:hex & 0xff)
}
}
First of all, UIColor
does not have any initializers that would take integers. I am assuming you have another extension somewhere similar to this. You have to add the support for alpha there
// we cannot name the param "alpha" because of a name collision
convenience init(red: Int, green: Int, blue: Int, a: Int = 255) {
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: CGFloat(a) / 255)
}
convenience init(hex:Int) {
self.init(red:(hex >> 16) & 0xff, green:(hex >> 8) & 0xff, blue:hex & 0xff, a:(hex >> 24) & 0xff)
}
(assuming alpha is the first component).
If you want alpha to be just an additional decimal number
convenience init(red: Int, green: Int, blue: Int, a: CGFloat = 1.0) {
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: a)
}
convenience init(hex:Int, a: CGFloat = 1.0) {
self.init(red:(hex >> 16) & 0xff, green:(hex >> 8) & 0xff, blue:hex & 0xff, a: a)
}