|
1 | 1 |
|
2 | 2 | import SwiftUI
|
3 | 3 |
|
| 4 | +extension ColorValue.ColorVariant { |
| 5 | + var asColorValue: ColorValue { |
| 6 | + switch self { |
| 7 | + case let .string(str): |
| 8 | + return .string(str) |
| 9 | + case let .semantic(semantic): |
| 10 | + return .semantic(semantic) |
| 11 | + } |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +extension Color { |
| 16 | + // Existing hex parser (if not present, add this) |
| 17 | + init?(hex: String) { |
| 18 | + let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted) |
| 19 | + var int: UInt64 = 0 |
| 20 | + Scanner(string: hex).scanHexInt64(&int) |
| 21 | + let a, r, g, b: UInt64 |
| 22 | + switch hex.count { |
| 23 | + case 3: // RGB (12-bit) |
| 24 | + (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17) |
| 25 | + case 6: // RGB (24-bit) |
| 26 | + (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF) |
| 27 | + case 8: // ARGB (32-bit) |
| 28 | + (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF) |
| 29 | + default: |
| 30 | + return nil |
| 31 | + } |
| 32 | + self.init( |
| 33 | + .sRGB, |
| 34 | + red: Double(r) / 255, |
| 35 | + green: Double(g) / 255, |
| 36 | + blue: Double(b) / 255, |
| 37 | + opacity: Double(a) / 255 |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + // New initializer for ColorValue |
| 42 | + init(value: ColorValue) { |
| 43 | + switch value { |
| 44 | + case let .string(str): |
| 45 | + // Handle string colors (hex or asset catalog) |
| 46 | + if let uiColor = UIColor(named: str) { |
| 47 | + self = Color(uiColor) |
| 48 | + } else if let hexColor = Color(hex: str) { // Hex color |
| 49 | + self = hexColor |
| 50 | + } else { |
| 51 | + self = .clear // Fallback |
| 52 | + } |
| 53 | + case let .semantic(names): |
| 54 | + // Use first valid system color |
| 55 | + for name in names.semantic { |
| 56 | + if let uiColor = UIColor.systemColor(named: name) { |
| 57 | + self = Color(uiColor) |
| 58 | + return |
| 59 | + } |
| 60 | + } |
| 61 | + self = .clear // Fallback |
| 62 | + case let .dynamic(dynamic): |
| 63 | + let lightColor = Color(value: dynamic.light.asColorValue) |
| 64 | + let darkColor = Color(value: dynamic.dark.asColorValue) |
| 65 | + self = Color(uiColor: UIColor { traitCollection in |
| 66 | + traitCollection.userInterfaceStyle == .dark ? UIColor(darkColor) : UIColor(lightColor) |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +extension UIColor { |
| 73 | + static func systemColor(named name: String) -> UIColor? { |
| 74 | + switch name { |
| 75 | + case "systemGreen": return .systemGreen |
| 76 | + case "systemBlue": return .systemBlue |
| 77 | + case "systemOrange": return .systemOrange |
| 78 | + case "systemRed": return .systemRed |
| 79 | + case "systemYellow": return .systemYellow |
| 80 | + case "label": return .label |
| 81 | + // Add more system colors as needed |
| 82 | + default: return nil |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
4 | 87 | extension Color {
|
5 | 88 | // Backgrounds
|
6 | 89 | static let systemBackground = Color(UIColor.systemBackground)
|
@@ -41,88 +124,4 @@ extension Color {
|
41 | 124 | static let systemGray4 = Color(UIColor.systemGray4)
|
42 | 125 | static let systemGray5 = Color(UIColor.systemGray5)
|
43 | 126 | static let systemGray6 = Color(UIColor.systemGray6)
|
44 |
| - |
45 |
| - private static let namedColors: [String: Color] = [ |
46 |
| - "accentcolor": .accentColor, |
47 |
| - "black": .black, |
48 |
| - "blue": .blue, |
49 |
| - "brown": .brown, |
50 |
| - "clear": .clear, |
51 |
| - "cyan": .cyan, |
52 |
| - "gray": .gray, |
53 |
| - "green": .green, |
54 |
| - "indigo": .indigo, |
55 |
| - "mint": .mint, |
56 |
| - "orange": .orange, |
57 |
| - "pink": .pink, |
58 |
| - "primary": .primary, |
59 |
| - "purple": .purple, |
60 |
| - "red": .red, |
61 |
| - "secondary": .secondary, |
62 |
| - "teal": .teal, |
63 |
| - "white": .white, |
64 |
| - "yellow": .yellow, |
65 |
| - // System colors |
66 |
| - "systemBackground": .systemBackground, |
67 |
| - "secondarySystemBackground": .secondarySystemBackground, |
68 |
| - "tertiarySystemBackground": .tertiarySystemBackground, |
69 |
| - "systemGroupedBackground": .systemGroupedBackground, |
70 |
| - "secondarySystemGroupedBackground": .secondarySystemGroupedBackground, |
71 |
| - "tertiarySystemGroupedBackground": .tertiarySystemGroupedBackground, |
72 |
| - "label": .label, |
73 |
| - "secondaryLabel": .secondaryLabel, |
74 |
| - "tertiaryLabel": .tertiaryLabel, |
75 |
| - "quaternaryLabel": .quaternaryLabel, |
76 |
| - "placeholderText": .placeholderText, |
77 |
| - "systemFill": .systemFill, |
78 |
| - "secondarySystemFill": .secondarySystemFill, |
79 |
| - "tertiarySystemFill": .tertiarySystemFill, |
80 |
| - "quaternarySystemFill": .quaternarySystemFill, |
81 |
| - "systemRed": .systemRed, |
82 |
| - "systemBlue": .systemBlue, |
83 |
| - "systemGreen": .systemGreen, |
84 |
| - "systemOrange": .systemOrange, |
85 |
| - "systemYellow": .systemYellow, |
86 |
| - "systemPink": .systemPink, |
87 |
| - "systemPurple": .systemPurple, |
88 |
| - "systemTeal": .systemTeal, |
89 |
| - "systemIndigo": .systemIndigo, |
90 |
| - "systemGray": .systemGray, |
91 |
| - "systemGray2": .systemGray2, |
92 |
| - "systemGray3": .systemGray3, |
93 |
| - "systemGray4": .systemGray4, |
94 |
| - "systemGray5": .systemGray5, |
95 |
| - "systemGray6": .systemGray6, |
96 |
| - ] |
97 |
| - private static func getNamedColor(_ color: String) -> Color? { |
98 |
| - let colorSanitized = color.trimmingCharacters(in: .whitespacesAndNewlines) |
99 |
| - if let namedColor = namedColors[colorSanitized] { |
100 |
| - return namedColor |
101 |
| - } |
102 |
| - return nil |
103 |
| - } |
104 |
| - |
105 |
| - init?(fromString color: String) { |
106 |
| - if let namedColor = Self.getNamedColor(color) { |
107 |
| - self = namedColor |
108 |
| - } else if let hexColor = Self(hex: color) { |
109 |
| - self = hexColor |
110 |
| - } else { |
111 |
| - return nil |
112 |
| - } |
113 |
| - } |
114 |
| - |
115 |
| - init?(hex: String) { |
116 |
| - var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines) |
117 |
| - hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "") |
118 |
| - |
119 |
| - var rgb: UInt64 = 0 |
120 |
| - guard Scanner(string: hexSanitized).scanHexInt64(&rgb) else { return nil } |
121 |
| - |
122 |
| - let red = Double((rgb & 0xFF0000) >> 16) / 255.0 |
123 |
| - let green = Double((rgb & 0x00FF00) >> 8) / 255.0 |
124 |
| - let blue = Double(rgb & 0x0000FF) / 255.0 |
125 |
| - |
126 |
| - self.init(red: red, green: green, blue: blue) |
127 |
| - } |
128 | 127 | }
|
0 commit comments