Is there an easy way to convert UIColor
to a hexadecimal value ?
Or do we have to get the RGB components with CGColorGetComponents
and then work it
This is how I do did it with Swift 3:
extension UIColor {
var hex:String{
get{
var red:CGFloat = 0
var blue:CGFloat = 0
var green:CGFloat = 0
var alpha:CGFloat = 0
self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
let rgb:Int = (Int)(red*255)<<16 | (Int)(green*255)<<8 | (Int)(blue*255)<<0
return String.localizedStringWithFormat("#%06x", rgb)
}
}
}
Or you can make it a method. I just like how color.hex
looks vs color.hex()
.