How to convert RGB color to HEX - Swift 5
func convertRGBToHEX(hexStr: String) -> UIColor {
let getHEX = hexStr.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var int = UInt64()
Scanner(string: getHEX).scanHexInt64(&int)
let a, r, g, b: UInt64
switch getHEX.count {
case 3:
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
case 6:
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8:
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
(a, r, g, b) = (255, 0, 0, 0)
}
return UIColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
}
// Call
convertRGBToHEX(hexStr: "FAFDF7")
Video tutorials on programming language on YouTube
watch now!
Возврат к списку