|
| 1 | +// |
| 2 | +// CGColor+hex.swift |
| 3 | +// |
| 4 | + |
| 5 | +import CoreGraphics |
| 6 | +import Foundation |
| 7 | + |
| 8 | +extension CGColor { |
| 9 | + func fromHex(_ hex: String) -> Self? { |
| 10 | + let r, g, b, a: CGFloat |
| 11 | + |
| 12 | + if hex.hasPrefix("#") { |
| 13 | + let start = hex.index(hex.startIndex, offsetBy: 1) |
| 14 | + let hexColor = String(hex[start...]) |
| 15 | + |
| 16 | + if hexColor.count == 8 { |
| 17 | + let scanner = Scanner(string: hexColor) |
| 18 | + var hexNumber: UInt64 = 0 |
| 19 | + |
| 20 | + if scanner.scanHexInt64(&hexNumber) { |
| 21 | + r = CGFloat((hexNumber & 0xFF00_0000) >> 24) / 255 |
| 22 | + g = CGFloat((hexNumber & 0x00FF_0000) >> 16) / 255 |
| 23 | + b = CGFloat((hexNumber & 0x0000_FF00) >> 8) / 255 |
| 24 | + a = CGFloat(hexNumber & 0x0000_00FF) / 255 |
| 25 | + |
| 26 | + return type(of: self).init(srgbRed: r, green: g, blue: b, alpha: a) |
| 27 | + } |
| 28 | + } |
| 29 | + if hexColor.count == 6 { |
| 30 | + let scanner = Scanner(string: hexColor) |
| 31 | + var hexNumber: UInt64 = 0 |
| 32 | + |
| 33 | + if scanner.scanHexInt64(&hexNumber) { |
| 34 | + r = CGFloat((hexNumber & 0x00FF_0000) >> 16) / 255 |
| 35 | + g = CGFloat((hexNumber & 0x0000_FF00) >> 8) / 255 |
| 36 | + b = CGFloat(hexNumber & 0x0000_00FF) / 255 |
| 37 | + |
| 38 | + return type(of: self).init(srgbRed: r, green: g, blue: b, alpha: 1.0) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + static func fromHexSequence(_ hex: String) -> [CGColor] { |
| 46 | + var colors: [CGColor] = [] |
| 47 | + |
| 48 | + var start = hex.startIndex |
| 49 | + while start <= hex.endIndex { |
| 50 | + if let end = hex.index(start, offsetBy: 5, limitedBy: hex.endIndex) { |
| 51 | + let slice = String(hex[start ... end]) |
| 52 | + let scanner = Scanner(string: slice) |
| 53 | + var hexNumber: UInt64 = 0 |
| 54 | + if scanner.scanHexInt64(&hexNumber) { |
| 55 | + let r, g, b: CGFloat |
| 56 | + r = CGFloat((hexNumber & 0x00FF_0000) >> 16) / 255 |
| 57 | + // print(CGFloat((hexNumber & 0x00ff0000) >> 16)) |
| 58 | + g = CGFloat((hexNumber & 0x0000_FF00) >> 8) / 255 |
| 59 | + // print(CGFloat((hexNumber & 0x0000ff00) >> 8)) |
| 60 | + b = CGFloat(hexNumber & 0x0000_00FF) / 255 |
| 61 | + // print(CGFloat(hexNumber & 0x000000ff)) |
| 62 | + colors.append(CGColor(srgbRed: r, green: g, blue: b, alpha: 1.0)) |
| 63 | + } |
| 64 | + start = hex.index(start, offsetBy: 6) |
| 65 | + } else { |
| 66 | + // The index increment to get the next slice wasn't able to get an additional |
| 67 | + // 6 characters, so quit and be done with what we've captured. |
| 68 | + break |
| 69 | + } |
| 70 | + } |
| 71 | + return colors |
| 72 | + } |
| 73 | + |
| 74 | + // MARK: - Computed Properties |
| 75 | + |
| 76 | + var toHex: String? { |
| 77 | + toHex() |
| 78 | + } |
| 79 | + |
| 80 | + // MARK: - From CGColor to String |
| 81 | + |
| 82 | + func toHex(alpha: Bool = false) -> String? { |
| 83 | + guard let components = components, components.count >= 3 else { |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + let r = Float(components[0]) |
| 88 | + let g = Float(components[1]) |
| 89 | + let b = Float(components[2]) |
| 90 | + var a = Float(1.0) |
| 91 | + |
| 92 | + if components.count >= 4 { |
| 93 | + a = Float(components[3]) |
| 94 | + } |
| 95 | + |
| 96 | + if alpha { |
| 97 | + return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255)) |
| 98 | + } else { |
| 99 | + return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255)) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments