1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import SwiftUI
extension Color { static let cloloABC = Color(hex: "#FFFFFF", alpha: 0.5)
init(hex: String, alpha: CGFloat = 1.0) { var hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted) if hex.hasPrefix("#") { hex = String(hex.dropFirst()) } assert(hex.count == 3 || hex.count == 6 || hex.count == 8, "Invalid hex code used. hex count is #(3, 6, 8).") var int: UInt64 = 0 Scanner(string: hex).scanHexInt64(&int) let r, g, b: UInt64 switch hex.count { case 3: (r, g, b) = ((int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17) case 6: (r, g, b) = (int >> 16, int >> 8 & 0xFF, int & 0xFF) case 8: (r, g, b) = (int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF) default: (r, g, b) = (1, 1, 0) }
self.init( .sRGB, red: Double(r) / 255, green: Double(g) / 255, blue: Double(b) / 255, opacity: Double(alpha) ) } }
|