|
4 | 4 | //
|
5 | 5 |
|
6 | 6 | import AppKit
|
7 |
| -import Foundation |
8 | 7 |
|
9 |
| -public extension NSColor { |
10 |
| - convenience init(hex: String) { |
11 |
| - let trimHex = hex.trimmingCharacters(in: .whitespacesAndNewlines) |
12 |
| - let dropHash = String(trimHex.dropFirst()).trimmingCharacters(in: .whitespacesAndNewlines) |
13 |
| - let hexString = trimHex.starts(with: "#") ? dropHash : trimHex |
14 |
| - let ui64 = UInt64(hexString, radix: 16) |
15 |
| - let value = ui64 != nil ? Int(ui64!) : 0 |
16 |
| - // #RRGGBB |
17 |
| - var components = ( |
18 |
| - R: CGFloat((value >> 16) & 0xff) / 255, |
19 |
| - G: CGFloat((value >> 08) & 0xff) / 255, |
20 |
| - B: CGFloat((value >> 00) & 0xff) / 255, |
21 |
| - a: CGFloat(1) |
22 |
| - ) |
23 |
| - if String(hexString).count == 8 { |
24 |
| - // #RRGGBBAA |
25 |
| - components = ( |
26 |
| - R: CGFloat((value >> 24) & 0xff) / 255, |
27 |
| - G: CGFloat((value >> 16) & 0xff) / 255, |
28 |
| - B: CGFloat((value >> 08) & 0xff) / 255, |
29 |
| - a: CGFloat((value >> 00) & 0xff) / 255 |
30 |
| - ) |
31 |
| - } |
32 |
| - self.init(red: components.R, green: components.G, blue: components.B, alpha: components.a) |
33 |
| - } |
34 |
| - |
35 |
| - func toHex(alpha: Bool = false) -> String? { |
36 |
| - guard let components = cgColor.components, components.count >= 3 else { |
37 |
| - return nil |
38 |
| - } |
39 |
| - |
40 |
| - let r = Float(components[0]) |
41 |
| - let g = Float(components[1]) |
42 |
| - let b = Float(components[2]) |
43 |
| - var a = Float(1.0) |
| 8 | +extension NSColor { |
| 9 | + private static let cssColorNames: [String: String] = [ |
| 10 | + "black": "#000000", |
| 11 | + "silver": "#C0C0C0", |
| 12 | + "gray": "#808080", |
| 13 | + "white": "#FFFFFF", |
| 14 | + "maroon": "#800000", |
| 15 | + "red": "#FF0000", |
| 16 | + "purple": "#800080", |
| 17 | + "fuchsia": "#FF00FF", |
| 18 | + "green": "#008000", |
| 19 | + "lime": "#00FF00", |
| 20 | + "olive": "#808000", |
| 21 | + "yellow": "#FFFF00", |
| 22 | + "navy": "#000080", |
| 23 | + "blue": "#0000FF", |
| 24 | + "teal": "#008080", |
| 25 | + "aqua": "#00FFFF" |
| 26 | + ] |
44 | 27 |
|
45 |
| - if components.count >= 4 { |
46 |
| - a = Float(components[3]) |
| 28 | + convenience init?(css: String) { |
| 29 | + var colorString = css.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() |
| 30 | + if let hexValue = NSColor.cssColorNames[colorString] { |
| 31 | + colorString = hexValue |
47 | 32 | }
|
48 |
| - |
49 |
| - if alpha { |
50 |
| - return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255)) |
51 |
| - } else { |
52 |
| - return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255)) |
| 33 | + let r, g, b, a: CGFloat |
| 34 | + if colorString.hasPrefix("#") { |
| 35 | + let start = colorString.index(colorString.startIndex, offsetBy: 1) |
| 36 | + let hexColor = String(colorString[start...]) |
| 37 | + if hexColor.count == 6 { |
| 38 | + let scanner = Scanner(string: hexColor) |
| 39 | + var hexNumber: UInt64 = 0 |
| 40 | + if scanner.scanHexInt64(&hexNumber) { |
| 41 | + r = CGFloat((hexNumber & 0xFF0000) >> 16) / 255 |
| 42 | + g = CGFloat((hexNumber & 0x00FF00) >> 8) / 255 |
| 43 | + b = CGFloat(hexNumber & 0x0000FF) / 255 |
| 44 | + a = 1.0 |
| 45 | + self.init(red: r, green: g, blue: b, alpha: a) |
| 46 | + return |
| 47 | + } |
| 48 | + } |
53 | 49 | }
|
| 50 | + return nil |
54 | 51 | }
|
55 | 52 | }
|
0 commit comments