- 十六进制色值转换成
UIColor
- 获取颜色的 RGBA 值
- 使用 0 ~ 255 的色值初始化颜色
- 获取
UIColor
的十六进制色值
- 生成随机色
public extension UIColor {
struct RGBA {
var r: UInt8
var g: UInt8
var b: UInt8
var a: UInt8
}
static func hexStringToUInt32(hex: String) -> UInt32? {
let hexString = hex.replacingOccurrences(of: "#", with: "")
let scanner = Scanner(string: hexString)
var result: UInt64 = 0
if scanner.scanHexInt64(&result) {
return result > UInt32.max ? UInt32.max : UInt32(result)
} else {
return nil
}
}
static func getRGBA(from: UInt32) -> RGBA {
func getbyte(_ value: UInt32) -> UInt8 {
return UInt8(value & 0xFF)
}
let r = getbyte(from >> 24)
let g = getbyte(from >> 16)
let b = getbyte(from >> 8)
let a = getbyte(from)
return RGBA(r: r, g: g, b: b, a: a)
}
var rgba: RGBA? {
let numberOfComponents = self.cgColor.numberOfComponents
if numberOfComponents == 1 {
return nil
} else if numberOfComponents == 2 {
guard let rgb = self.cgColor.components?[0],
let a = self.cgColor.components?[1] else { return nil }
return RGBA(r: UInt8(rgb * 255),
g: UInt8(rgb * 255),
b: UInt8(rgb * 255),
a: UInt8(a * 255))
} else if numberOfComponents == 4 {
guard let r = self.cgColor.components?[0],
let g = self.cgColor.components?[1],
let b = self.cgColor.components?[2],
let a = self.cgColor.components?[3] else { return nil }
return RGBA(r: UInt8(r * 255),
g: UInt8(g * 255),
b: UInt8(b * 255),
a: UInt8(a * 255))
} else {
return nil
}
}
convenience init?(hexColor: String) {
var hex = hexColor.trimmingCharacters(in: .whitespacesAndNewlines).replacingOccurrences(of: "#", with: "")
let legalCharacter = hex.uppercased().map { String($0) }.filter { c -> Bool in
let c1 = c > "9" && c < "A"
let c2 = c < "0"
let c3 = c > "F"
return c1 || c2 || c3
}.count == 0
guard legalCharacter else { return nil }
if hex.count == 3 {
hex = hex.map { String($0) }.map { $0 + $0 }.joined().appending("FF")
} else if hex.count == 4 {
hex = hex.map { String($0) }.map { $0 + $0 }.joined()
} else if hex.count == 6 {
hex = hex.appending("FF")
} else if hex.count == 8 {
} else {
return nil
}
guard let colorValue = Self.hexStringToUInt32(hex: hex) else { return nil }
let rgba = Self.getRGBA(from: colorValue)
self.init(red: CGFloat(rgba.r) / 255,
green: CGFloat(rgba.g) / 255,
blue: CGFloat(rgba.b) / 255,
alpha: CGFloat(rgba.a) / 255)
}
convenience init(red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8) {
self.init(red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: CGFloat(alpha) / 255.0)
}
convenience init(red: UInt8, green: UInt8, blue: UInt8, alpha: CGFloat) {
self.init(red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: alpha)
}
var hexValue: String? {
guard let rgba = rgba else { return nil }
return String(format: "%02X%02X%02X%02X", rgba.r,rgba.g,rgba.b,rgba.a)
}
static var random: UIColor {
let r = CGFloat.random(in: 0...1)
let g = CGFloat.random(in: 0...1)
let b = CGFloat.random(in: 0...1)
let a = CGFloat.random(in: 0...1)
return UIColor(red: r, green: g, blue: b, alpha: a)
}
}