1-
21import SwiftUI
32
43extension ColorValue . ColorVariant {
@@ -13,8 +12,49 @@ extension ColorValue.ColorVariant {
1312}
1413
1514extension Color {
15+ // Dictionary of standard HTML/CSS color names
16+ static let namedColors : [ String : UInt32 ] = [
17+ // Basic colors
18+ " black " : 0x000000 ,
19+ " white " : 0xFFFFFF ,
20+ " red " : 0xFF0000 ,
21+ " green " : 0x008000 ,
22+ " blue " : 0x0000FF ,
23+ " yellow " : 0xFFFF00 ,
24+ " cyan " : 0x00FFFF ,
25+ " magenta " : 0xFF00FF ,
26+ " transparent " : 0x0000_0000 ,
27+
28+ // Extended color set
29+ " aqua " : 0x00FFFF ,
30+ " darkblue " : 0x00008B ,
31+ " darkgray " : 0xA9A9A9 ,
32+ " darkgrey " : 0xA9A9A9 ,
33+ " darkgreen " : 0x006400 ,
34+ " darkkhaki " : 0xBDB76B ,
35+ " darkorange " : 0xFF8C00 ,
36+ " darkorchid " : 0x9932CC ,
37+ " darkred " : 0x8B0000 ,
38+ " darksalmon " : 0xE9967A ,
39+ " darkviolet " : 0x9400D3 ,
40+ " gray " : 0x808080 ,
41+ " grey " : 0x808080 ,
42+ " lightgray " : 0xD3D3D3 ,
43+ " lightgrey " : 0xD3D3D3 ,
44+ " lightgreen " : 0x90EE90 ,
45+ " lightyellow " : 0xFFFFE0 ,
46+ " lime " : 0x00FF00 ,
47+ " navy " : 0x000080 ,
48+ " olive " : 0x808000 ,
49+ " orange " : 0xFFA500 ,
50+ " pink " : 0xFFC0CB ,
51+ " purple " : 0x800080 ,
52+ " silver " : 0xC0C0C0 ,
53+ " teal " : 0x008080 ,
54+ ]
55+
1656 // Existing hex parser (if not present, add this)
17- init ? ( hex: String ) {
57+ init ? ( fromHex hex: String ) {
1858 let hex = hex. trimmingCharacters ( in: CharacterSet . alphanumerics. inverted)
1959 var int : UInt64 = 0
2060 Scanner ( string: hex) . scanHexInt64 ( & int)
@@ -38,22 +78,49 @@ extension Color {
3878 )
3979 }
4080
81+ // Initialize from named color or hex value
82+ init ? ( fromCSSName name: String ) {
83+ let lowerCaseName = name. lowercased ( )
84+
85+ // Check if it's a named color
86+ if let hexValue = Color . namedColors [ lowerCaseName] {
87+ let r = ( hexValue >> 16 ) & 0xFF
88+ let g = ( hexValue >> 8 ) & 0xFF
89+ let b = hexValue & 0xFF
90+ let a = ( hexValue >> 24 ) & 0xFF
91+
92+ self . init (
93+ . sRGB,
94+ red: Double ( r) / 255 ,
95+ green: Double ( g) / 255 ,
96+ blue: Double ( b) / 255 ,
97+ opacity: hexValue > 0xFFFFFF ? Double ( a) / 255 : 1.0
98+ )
99+ } else if let hexColor = Color ( fromHex: name) {
100+ self = hexColor
101+ } else {
102+ return nil
103+ }
104+ }
105+
41106 // New initializer for ColorValue
42107 init ( value: ColorValue ) {
43108 switch value {
44109 case let . string( str) :
45- // Handle string colors (hex or asset catalog)
46- if let uiColor = UIColor ( named : str) {
110+ // Handle string colors (hex, named, or asset catalog)
111+ if let uiColor = UIColor . systemColor ( name : str) {
47112 self = Color ( uiColor)
48- } else if let hexColor = Color ( hex: str) { // Hex color
113+ } else if let namedColor = Color ( fromCSSName: str) { // Named CSS color
114+ self = namedColor
115+ } else if let hexColor = Color ( fromHex: str) { // Hex color
49116 self = hexColor
50117 } else {
51118 self = . clear // Fallback
52119 }
53120 case let . semantic( names) :
54121 // Use first valid system color
55122 for name in names. semantic {
56- if let uiColor = UIColor . systemColor ( named : name) {
123+ if let uiColor = UIColor . systemColor ( name : name) {
57124 self = Color ( uiColor)
58125 return
59126 }
@@ -70,87 +137,91 @@ extension Color {
70137}
71138
72139extension UIColor {
73- static func systemColor( named name: String ) -> UIColor ? {
74- switch name {
140+ private static let systemColorMap : [ String : UIColor ] = [
75141 // Backgrounds
76- case " systemBackground " : return . systemBackground
77- case " secondarySystemBackground " : return . secondarySystemBackground
78- case " tertiarySystemBackground " : return . tertiarySystemBackground
79- case " systemGroupedBackground " : return . systemGroupedBackground
80- case " secondarySystemGroupedBackground " : return . secondarySystemGroupedBackground
81- case " tertiarySystemGroupedBackground " : return . tertiarySystemGroupedBackground
142+ " systemBackground " : . systemBackground,
143+ " secondarySystemBackground " : . secondarySystemBackground,
144+ " tertiarySystemBackground " : . tertiarySystemBackground,
145+ " systemGroupedBackground " : . systemGroupedBackground,
146+ " secondarySystemGroupedBackground " : . secondarySystemGroupedBackground,
147+ " tertiarySystemGroupedBackground " : . tertiarySystemGroupedBackground,
148+
82149 // Labels
83- case " label " : return . label
84- case " secondaryLabel " : return . secondaryLabel
85- case " tertiaryLabel " : return . tertiaryLabel
86- case " quaternaryLabel " : return . quaternaryLabel
87- case " placeholderText " : return . placeholderText
150+ " label " : . label,
151+ " secondaryLabel " : . secondaryLabel,
152+ " tertiaryLabel " : . tertiaryLabel,
153+ " quaternaryLabel " : . quaternaryLabel,
154+ " placeholderText " : . placeholderText,
155+
88156 // Fill colors
89- case " systemFill " : return . systemFill
90- case " secondarySystemFill " : return . secondarySystemFill
91- case " tertiarySystemFill " : return . tertiarySystemFill
92- case " quaternarySystemFill " : return . quaternarySystemFill
157+ " systemFill " : . systemFill,
158+ " secondarySystemFill " : . secondarySystemFill,
159+ " tertiarySystemFill " : . tertiarySystemFill,
160+ " quaternarySystemFill " : . quaternarySystemFill,
161+
93162 // Standard colors
94- case " systemRed " : return . systemRed
95- case " systemBlue " : return . systemBlue
96- case " systemGreen " : return . systemGreen
97- case " systemOrange " : return . systemOrange
98- case " systemYellow " : return . systemYellow
99- case " systemPink " : return . systemPink
100- case " systemPurple " : return . systemPurple
101- case " systemTeal " : return . systemTeal
102- case " systemIndigo " : return . systemIndigo
163+ " systemRed " : . systemRed,
164+ " systemBlue " : . systemBlue,
165+ " systemGreen " : . systemGreen,
166+ " systemOrange " : . systemOrange,
167+ " systemYellow " : . systemYellow,
168+ " systemPink " : . systemPink,
169+ " systemPurple " : . systemPurple,
170+ " systemTeal " : . systemTeal,
171+ " systemIndigo " : . systemIndigo,
172+
103173 // Grays
104- case " systemGray " : return . systemGray
105- case " systemGray2 " : return . systemGray2
106- case " systemGray3 " : return . systemGray3
107- case " systemGray4 " : return . systemGray4
108- case " systemGray5 " : return . systemGray5
109- case " systemGray6 " : return . systemGray6
110- // Default
111- default : return nil
112- }
174+ " systemGray " : . systemGray,
175+ " systemGray2 " : . systemGray2,
176+ " systemGray3 " : . systemGray3,
177+ " systemGray4 " : . systemGray4,
178+ " systemGray5 " : . systemGray5,
179+ " systemGray6 " : . systemGray6,
180+ ]
181+
182+ static func systemColor( name: String ) -> UIColor ? {
183+ return systemColorMap [ name]
113184 }
114185}
115186
116- extension Color {
117- // Backgrounds
118- static let systemBackground = Color ( UIColor . systemBackground)
119- static let secondarySystemBackground = Color ( UIColor . secondarySystemBackground)
120- static let tertiarySystemBackground = Color ( UIColor . tertiarySystemBackground)
121- static let systemGroupedBackground = Color ( UIColor . systemGroupedBackground)
122- static let secondarySystemGroupedBackground = Color ( UIColor . secondarySystemGroupedBackground)
123- static let tertiarySystemGroupedBackground = Color ( UIColor . tertiarySystemGroupedBackground)
124-
125- // Labels
126- static let label = Color ( UIColor . label)
127- static let secondaryLabel = Color ( UIColor . secondaryLabel)
128- static let tertiaryLabel = Color ( UIColor . tertiaryLabel)
129- static let quaternaryLabel = Color ( UIColor . quaternaryLabel)
130- static let placeholderText = Color ( UIColor . placeholderText)
131-
132- // Fill colors
133- static let systemFill = Color ( UIColor . systemFill)
134- static let secondarySystemFill = Color ( UIColor . secondarySystemFill)
135- static let tertiarySystemFill = Color ( UIColor . tertiarySystemFill)
136- static let quaternarySystemFill = Color ( UIColor . quaternarySystemFill)
137-
138- // Standard colors
139- static let systemRed = Color ( UIColor . systemRed)
140- static let systemBlue = Color ( UIColor . systemBlue)
141- static let systemGreen = Color ( UIColor . systemGreen)
142- static let systemOrange = Color ( UIColor . systemOrange)
143- static let systemYellow = Color ( UIColor . systemYellow)
144- static let systemPink = Color ( UIColor . systemPink)
145- static let systemPurple = Color ( UIColor . systemPurple)
146- static let systemTeal = Color ( UIColor . systemTeal)
147- static let systemIndigo = Color ( UIColor . systemIndigo)
148-
149- // Grays
150- static let systemGray = Color ( UIColor . systemGray)
151- static let systemGray2 = Color ( UIColor . systemGray2)
152- static let systemGray3 = Color ( UIColor . systemGray3)
153- static let systemGray4 = Color ( UIColor . systemGray4)
154- static let systemGray5 = Color ( UIColor . systemGray5)
155- static let systemGray6 = Color ( UIColor . systemGray6)
156- }
187+ // extension Color {
188+ // // Backgrounds
189+ // static let systemBackground = Color(UIColor.systemBackground)
190+ // static let secondarySystemBackground = Color(UIColor.secondarySystemBackground)
191+ // static let tertiarySystemBackground = Color(UIColor.tertiarySystemBackground)
192+ // static let systemGroupedBackground = Color(UIColor.systemGroupedBackground)
193+ // static let secondarySystemGroupedBackground = Color(UIColor.secondarySystemGroupedBackground)
194+ // static let tertiarySystemGroupedBackground = Color(UIColor.tertiarySystemGroupedBackground)
195+
196+ // // Labels
197+ // static let label = Color(UIColor.label)
198+ // static let secondaryLabel = Color(UIColor.secondaryLabel)
199+ // static let tertiaryLabel = Color(UIColor.tertiaryLabel)
200+ // static let quaternaryLabel = Color(UIColor.quaternaryLabel)
201+ // static let placeholderText = Color(UIColor.placeholderText)
202+
203+ // // Fill colors
204+ // static let systemFill = Color(UIColor.systemFill)
205+ // static let secondarySystemFill = Color(UIColor.secondarySystemFill)
206+ // static let tertiarySystemFill = Color(UIColor.tertiarySystemFill)
207+ // static let quaternarySystemFill = Color(UIColor.quaternarySystemFill)
208+
209+ // // Standard colors
210+ // static let systemRed = Color(UIColor.systemRed)
211+ // static let systemBlue = Color(UIColor.systemBlue)
212+ // static let systemGreen = Color(UIColor.systemGreen)
213+ // static let systemOrange = Color(UIColor.systemOrange)
214+ // static let systemYellow = Color(UIColor.systemYellow)
215+ // static let systemPink = Color(UIColor.systemPink)
216+ // static let systemPurple = Color(UIColor.systemPurple)
217+ // static let systemTeal = Color(UIColor.systemTeal)
218+ // static let systemIndigo = Color(UIColor.systemIndigo)
219+
220+ // // Grays
221+ // static let systemGray = Color(UIColor.systemGray)
222+ // static let systemGray2 = Color(UIColor.systemGray2)
223+ // static let systemGray3 = Color(UIColor.systemGray3)
224+ // static let systemGray4 = Color(UIColor.systemGray4)
225+ // static let systemGray5 = Color(UIColor.systemGray5)
226+ // static let systemGray6 = Color(UIColor.systemGray6)
227+ // }
0 commit comments