您可以使用 UIColor 初始化程序创建 UIColor,然后使用 UIColor getRed 方法提取 rgb 颜色。然后你只需要将组件值乘以 255 并使用字符串基数初始化器从它们创建一个六进制表示:
extension UIColor {
var rgb: (red: CGFloat, green: CGFloat, blue: CGFloat)? {
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0
guard getRed(&r, green: &g, blue: &b, alpha: nil) else { return nil }
return (r,g,b)
}
var hexa: String? {
guard let (r,g,b) = rgb else { return nil }
return "#" + UInt8(r*255).hexa + UInt8(g*255).hexa + UInt8(b*255).hexa
}
}
extension UInt8 {
var hexa: String {
let value = String(self, radix: 16, uppercase: true)
return (self < 16 ? "0": "") + value
}
}
let red = UIColor(hue: 1, saturation: 1, brightness: 1, alpha: 1)
let white = UIColor(hue: 1, saturation: 0, brightness: 1, alpha: 1)
let black = UIColor(hue: 1, saturation: 0, brightness: 0, alpha: 1)
red.hexa // "#FF0000" red
white.hexa // "#ffffff" white
black.hexa // "#000000" black
现在,如果您真的想避免使用 UIColor api,您可以使用这篇文章作为从 HSB 转换为 RGB 的参考:
func toHex<F: BinaryFloatingPoint>(h: F, s: F, b: F) -> String {
var red, green, blue, i, f, p, q, t: F
i = (h * 6).rounded(.down)
f = h * 6 - i
p = b * (1 - s)
q = b * (1 - f * s)
t = b * (1 - (1 - f) * s)
switch h * 360 {
case 0..<60, 360: red = b; green = t; blue = p
case 60..<120: red = q; green = b; blue = p
case 120..<180: red = p; green = b; blue = t
case 180..<240: red = p; green = q; blue = b
case 240..<300: red = t; green = p; blue = b
case 300..<360: red = b; green = p; blue = q
default: fatalError()
return "#" + UInt8(red * 255).hexa + UInt8(green * 255).hexa + UInt8(blue * 255).hexa
}
toHex(h: 1, s: 1, b: 1) // "#FF0000" red
toHex(h: 1, s: 0, b: 1) // "#FFFFFF" white
toHex(h: 1, s: 0, b: 0) // "#000000" black
toHex(h: 0.8333, s: 1, b: 0.5) // "#7F007F" purple