expo-modules-core 58.0.29 → 58.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/ExpoModulesCore.podspec +6 -8
- package/android/build.gradle +2 -2
- package/android/prebuilt/metadata.json +1 -1
- package/android/prebuilt/native-libs.tar.xz +0 -0
- package/android/src/main/java/expo/modules/adapters/react/apploader/RNHeadlessAppLoader.kt +2 -3
- package/ios/Benchmarks/FunctionBenchmarks.swift +2 -14
- package/ios/Core/AppContext.swift +29 -2
- package/ios/Core/Arguments/Convertibles.swift +1 -24
- package/ios/Core/Coding/JavaScriptCodable+Color.swift +887 -0
- package/ios/Core/Coding/JavaScriptCodable+URL.swift +55 -0
- package/ios/Core/Convertibles/Convertibles+Color.swift +10 -686
- package/ios/Core/Events/EventEmitter.swift +194 -1
- package/ios/Core/ExpoModulesMacros.swift +50 -8
- package/ios/Core/ExpoPackage.swift +29 -0
- package/ios/Core/Logging/Logger.swift +8 -2
- package/ios/Core/ModulesProvider.swift +19 -0
- package/ios/Core/Records/Record.swift +18 -6
- package/ios/Core/SharedObjects/SharedObjectRegistry.swift +28 -16
- package/ios/Core/Views/SwiftUI/Convertibles+SwiftUI.swift +2 -67
- package/ios/Core/Views/SwiftUI/SwiftUIHostingView.swift +1 -0
- package/ios/Core/Views/SwiftUI/SwiftUIViewHost.swift +100 -39
- package/ios/Core/Views/SwiftUI/SwiftUIVirtualView.swift +1 -0
- package/ios/ReactDelegates/ExpoReactDelegate.swift +1 -1
- package/ios/ReactDelegates/ExpoRootViewController.swift +22 -0
- package/package.json +4 -4
- package/prebuilds/output/debug/xcframeworks/ExpoModulesCore.tar.gz +0 -0
- package/prebuilds/output/debug/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
- package/prebuilds/output/headers/xcframeworks/ExpoModulesCoreHeaders.tar.gz +0 -0
- package/prebuilds/output/release/xcframeworks/ExpoModulesCore.tar.gz +0 -0
- package/prebuilds/output/release/xcframeworks/ExpoModulesWorklets.tar.gz +0 -0
- package/spm.config.json +4 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Copyright 2022-present 650 Industries. All rights reserved.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// The conversions live with the `JavaScriptDecodable` and `JavaScriptEncodable` conformances in
|
|
4
|
+
// `JavaScriptCodable+Color.swift`.
|
|
4
5
|
|
|
5
6
|
extension UIColor: Convertible {
|
|
6
7
|
public static func convert(from value: Any?, appContext: AppContext) throws -> Self {
|
|
@@ -8,84 +9,8 @@ extension UIColor: Convertible {
|
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
public static func convert(from value: Any?) throws -> Self {
|
|
11
|
-
// swiftlint:disable force_cast
|
|
12
|
-
|
|
13
|
-
if let namedColorComponents = namedColors[value] {
|
|
14
|
-
return try uiColorWithComponents(namedColorComponents) as! Self
|
|
15
|
-
}
|
|
16
|
-
return try colorFromString(value) as! Self
|
|
17
|
-
}
|
|
18
|
-
if let components = value as? [Double] {
|
|
19
|
-
return try uiColorWithComponents(components) as! Self
|
|
20
|
-
}
|
|
21
|
-
if let value = value as? Int {
|
|
22
|
-
return try colorFromArgb(UInt64(value)) as! Self
|
|
23
|
-
}
|
|
24
|
-
if let value = value as? Double {
|
|
25
|
-
return try colorFromArgb(UInt64(value)) as! Self
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Handle `PlatformColor` and `DynamicColorIOS`
|
|
29
|
-
if let opaqueValue = value as? [String: Any] {
|
|
30
|
-
if let semanticName = opaqueValue["semantic"] as? String,
|
|
31
|
-
let color = resolveNamedColor(name: semanticName)
|
|
32
|
-
{
|
|
33
|
-
return color as! Self
|
|
34
|
-
}
|
|
35
|
-
if let semanticArray = opaqueValue["semantic"] as? [String] {
|
|
36
|
-
for semanticName in semanticArray {
|
|
37
|
-
if let color = resolveNamedColor(name: semanticName) {
|
|
38
|
-
return color as! Self
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if let appearances = opaqueValue["dynamic"] as? [String: Any],
|
|
43
|
-
let lightColor = try appearances["light"].map({ try UIColor.convert(from: $0) }),
|
|
44
|
-
let darkColor = try appearances["dark"].map({ try UIColor.convert(from: $0) })
|
|
45
|
-
{
|
|
46
|
-
let highContrastLightColor = try appearances["highContrastLight"].map({ try UIColor.convert(from: $0) })
|
|
47
|
-
let highContrastDarkColor = try appearances["highContrastDark"].map({ try UIColor.convert(from: $0) })
|
|
48
|
-
|
|
49
|
-
#if os(iOS) || os(tvOS)
|
|
50
|
-
let color = UIColor { (traitCollection: UITraitCollection) -> UIColor in
|
|
51
|
-
if traitCollection.userInterfaceStyle == .dark {
|
|
52
|
-
if traitCollection.accessibilityContrast == .high, let highContrastDarkColor {
|
|
53
|
-
return highContrastDarkColor
|
|
54
|
-
}
|
|
55
|
-
return darkColor
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if traitCollection.accessibilityContrast == .high, let highContrastLightColor {
|
|
59
|
-
return highContrastLightColor
|
|
60
|
-
}
|
|
61
|
-
return lightColor
|
|
62
|
-
}
|
|
63
|
-
#elseif os(macOS)
|
|
64
|
-
let color = NSColor(name: nil) { (appearance: NSAppearance) -> NSColor in
|
|
65
|
-
let isDarkMode = appearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
|
|
66
|
-
let isHighContrast = NSWorkspace.shared.accessibilityDisplayShouldIncreaseContrast
|
|
67
|
-
|
|
68
|
-
if isDarkMode {
|
|
69
|
-
if isHighContrast, let highContrastDarkColor = highContrastDarkColor {
|
|
70
|
-
return highContrastDarkColor
|
|
71
|
-
}
|
|
72
|
-
return darkColor
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if isHighContrast, let highContrastLightColor = highContrastLightColor {
|
|
76
|
-
return highContrastLightColor
|
|
77
|
-
}
|
|
78
|
-
return lightColor
|
|
79
|
-
}
|
|
80
|
-
#endif
|
|
81
|
-
return color as! Self
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if let color = value as? Self {
|
|
85
|
-
return color
|
|
86
|
-
}
|
|
87
|
-
throw Conversions.ConvertingException<UIColor>(value)
|
|
88
|
-
// swiftlint:enable force_cast
|
|
12
|
+
// swiftlint:disable:next force_cast
|
|
13
|
+
return try uiColorFromAny(value) as! Self
|
|
89
14
|
}
|
|
90
15
|
|
|
91
16
|
public static func convertResult(_ result: Any, appContext: AppContext) throws -> Any {
|
|
@@ -96,39 +21,14 @@ extension UIColor: Convertible {
|
|
|
96
21
|
}
|
|
97
22
|
}
|
|
98
23
|
|
|
99
|
-
extension UIColor {
|
|
100
|
-
public var string: String {
|
|
101
|
-
var r: CGFloat = 0
|
|
102
|
-
var g: CGFloat = 0
|
|
103
|
-
var b: CGFloat = 0
|
|
104
|
-
var a: CGFloat = 0
|
|
105
|
-
|
|
106
|
-
#if os(macOS)
|
|
107
|
-
getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
108
|
-
return String(
|
|
109
|
-
format: "#%02x%02x%02x%02x", Int((r * 255).rounded()), Int((g * 255).rounded()),
|
|
110
|
-
Int((b * 255).rounded()), Int((a * 255).rounded()))
|
|
111
|
-
#else
|
|
112
|
-
if getRed(&r, green: &g, blue: &b, alpha: &a) {
|
|
113
|
-
return String(
|
|
114
|
-
format: "#%02x%02x%02x%02x", Int((r * 255).rounded()), Int((g * 255).rounded()),
|
|
115
|
-
Int((b * 255).rounded()), Int((a * 255).rounded()))
|
|
116
|
-
}
|
|
117
|
-
#endif
|
|
118
|
-
return "#00000000"
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
24
|
extension CGColor: Convertible {
|
|
123
25
|
public static func convert(from value: Any?, appContext: AppContext) throws -> Self {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
// swiftlint:enable force_cast
|
|
26
|
+
return try convert(from: value)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public static func convert(from value: Any?) throws -> Self {
|
|
30
|
+
// swiftlint:disable:next force_cast
|
|
31
|
+
return try cgColorFromAny(value) as! Self
|
|
132
32
|
}
|
|
133
33
|
|
|
134
34
|
public static func convertResult(_ result: Any, appContext: AppContext) throws -> Any {
|
|
@@ -137,579 +37,3 @@ extension CGColor: Convertible {
|
|
|
137
37
|
return try UIColor.convertResult(uiColor, appContext: appContext)
|
|
138
38
|
}
|
|
139
39
|
}
|
|
140
|
-
|
|
141
|
-
// MARK: - Platform Color Helpers
|
|
142
|
-
|
|
143
|
-
private func resolveNamedColor(name: String) -> UIColor? {
|
|
144
|
-
return UIColor(named: name) ?? uiColorFromSemanticName(name: name)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
private func uiColorFromSemanticName(name: String) -> UIColor? {
|
|
148
|
-
let selector: Selector
|
|
149
|
-
if name.hasSuffix("Color") {
|
|
150
|
-
selector = Selector(name)
|
|
151
|
-
} else {
|
|
152
|
-
selector = Selector("\(name)Color")
|
|
153
|
-
}
|
|
154
|
-
guard UIColor.responds(to: selector) else {
|
|
155
|
-
return nil
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return UIColor.perform(selector).takeUnretainedValue() as? UIColor
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private func uiColorWithComponents(_ components: [Double]) throws -> UIColor {
|
|
162
|
-
guard components.count >= 3 else {
|
|
163
|
-
throw InvalidColorComponentsException(components.count)
|
|
164
|
-
}
|
|
165
|
-
let alpha = components.count > 3 ? components[3] : 1.0
|
|
166
|
-
return UIColor(red: components[0], green: components[1], blue: components[2], alpha: alpha)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// MARK: - Color String Parsing
|
|
170
|
-
|
|
171
|
-
/// Parses a color string (hex, rgb, hsl, hwb) into a UIColor.
|
|
172
|
-
private func colorFromString(_ value: String) throws -> UIColor {
|
|
173
|
-
let input = value.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
174
|
-
let lowercased = input.lowercased()
|
|
175
|
-
|
|
176
|
-
if lowercased.hasPrefix("rgb") {
|
|
177
|
-
return try colorFromRGBString(lowercased)
|
|
178
|
-
}
|
|
179
|
-
if lowercased.hasPrefix("hsl") {
|
|
180
|
-
return try colorFromHSLString(lowercased)
|
|
181
|
-
}
|
|
182
|
-
if lowercased.hasPrefix("hwb") {
|
|
183
|
-
return try colorFromHWBString(lowercased)
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
var hexStr = input.replacingOccurrences(of: "#", with: "")
|
|
187
|
-
|
|
188
|
-
if hexStr.count == 6 { hexStr += "FF" }
|
|
189
|
-
if hexStr.count == 3 { hexStr += "F" }
|
|
190
|
-
|
|
191
|
-
// Expand short form (supported by Web)
|
|
192
|
-
if hexStr.count == 4 {
|
|
193
|
-
let chars = Array(hexStr)
|
|
194
|
-
hexStr = [
|
|
195
|
-
String(repeating: chars[0], count: 2),
|
|
196
|
-
String(repeating: chars[1], count: 2),
|
|
197
|
-
String(repeating: chars[2], count: 2),
|
|
198
|
-
String(repeating: chars[3], count: 2),
|
|
199
|
-
].joined(separator: "")
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
var rgba: UInt64 = 0
|
|
203
|
-
|
|
204
|
-
guard hexStr.range(of: #"^[0-9a-fA-F]{8}$"#, options: .regularExpression) != nil,
|
|
205
|
-
Scanner(string: hexStr).scanHexInt64(&rgba)
|
|
206
|
-
else {
|
|
207
|
-
throw InvalidHexColorException(input)
|
|
208
|
-
}
|
|
209
|
-
return try colorFromRgba(rgba)
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
private func colorFromArgb(_ argb: UInt64) throws -> UIColor {
|
|
213
|
-
guard argb <= UInt32.max else {
|
|
214
|
-
throw HexColorOverflowException(argb)
|
|
215
|
-
}
|
|
216
|
-
let alpha = CGFloat((argb >> 24) & 0xff) / 255.0
|
|
217
|
-
let red = CGFloat((argb >> 16) & 0xff) / 255.0
|
|
218
|
-
let green = CGFloat((argb >> 8) & 0xff) / 255.0
|
|
219
|
-
let blue = CGFloat(argb & 0xff) / 255.0
|
|
220
|
-
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
private func colorFromRgba(_ rgba: UInt64) throws -> UIColor {
|
|
224
|
-
guard rgba <= UInt32.max else {
|
|
225
|
-
throw HexColorOverflowException(rgba)
|
|
226
|
-
}
|
|
227
|
-
let red = CGFloat((rgba >> 24) & 0xff) / 255.0
|
|
228
|
-
let green = CGFloat((rgba >> 16) & 0xff) / 255.0
|
|
229
|
-
let blue = CGFloat((rgba >> 8) & 0xff) / 255.0
|
|
230
|
-
let alpha = CGFloat(rgba & 0xff) / 255.0
|
|
231
|
-
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// MARK: - CSS Color Function Regex Patterns
|
|
235
|
-
|
|
236
|
-
// These are built from RegexBuilder primitives only. Composing regex-literal fragments
|
|
237
|
-
// (e.g. /hsla?\(\s*/) inside a builder miscompiles on some OS runtime versions
|
|
238
|
-
// (observed on the iOS 27.0 beta simulator) and the patterns silently stop matching.
|
|
239
|
-
|
|
240
|
-
/// A CSS number like `-12`, `+1.5`, `.5` or `100`.
|
|
241
|
-
nonisolated(unsafe) private let cssNumber = Regex {
|
|
242
|
-
Optionally {
|
|
243
|
-
CharacterClass.anyOf("-+")
|
|
244
|
-
}
|
|
245
|
-
ZeroOrMore(.digit)
|
|
246
|
-
Optionally {
|
|
247
|
-
"."
|
|
248
|
-
}
|
|
249
|
-
OneOrMore(.digit)
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
nonisolated(unsafe) private let cssNumberOrPercent = Regex {
|
|
253
|
-
cssNumber
|
|
254
|
-
Optionally {
|
|
255
|
-
"%"
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
nonisolated(unsafe) private let commaSeparator = Regex {
|
|
260
|
-
ZeroOrMore(.whitespace)
|
|
261
|
-
","
|
|
262
|
-
ZeroOrMore(.whitespace)
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
nonisolated(unsafe) private let rgbCommaRegex = Regex {
|
|
266
|
-
"rgb"
|
|
267
|
-
Optionally {
|
|
268
|
-
"a"
|
|
269
|
-
}
|
|
270
|
-
"("
|
|
271
|
-
ZeroOrMore(.whitespace)
|
|
272
|
-
Capture {
|
|
273
|
-
cssNumberOrPercent
|
|
274
|
-
}
|
|
275
|
-
commaSeparator
|
|
276
|
-
Capture {
|
|
277
|
-
cssNumberOrPercent
|
|
278
|
-
}
|
|
279
|
-
commaSeparator
|
|
280
|
-
Capture {
|
|
281
|
-
cssNumberOrPercent
|
|
282
|
-
}
|
|
283
|
-
Optionally {
|
|
284
|
-
commaSeparator
|
|
285
|
-
Capture {
|
|
286
|
-
cssNumberOrPercent
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
ZeroOrMore(.whitespace)
|
|
290
|
-
")"
|
|
291
|
-
}.ignoresCase()
|
|
292
|
-
|
|
293
|
-
nonisolated(unsafe) private let rgbSpaceRegex = Regex {
|
|
294
|
-
"rgb"
|
|
295
|
-
Optionally {
|
|
296
|
-
"a"
|
|
297
|
-
}
|
|
298
|
-
"("
|
|
299
|
-
ZeroOrMore(.whitespace)
|
|
300
|
-
Capture {
|
|
301
|
-
cssNumberOrPercent
|
|
302
|
-
}
|
|
303
|
-
OneOrMore(.whitespace)
|
|
304
|
-
Capture {
|
|
305
|
-
cssNumberOrPercent
|
|
306
|
-
}
|
|
307
|
-
OneOrMore(.whitespace)
|
|
308
|
-
Capture {
|
|
309
|
-
cssNumberOrPercent
|
|
310
|
-
}
|
|
311
|
-
Optionally {
|
|
312
|
-
ZeroOrMore(.whitespace)
|
|
313
|
-
"/"
|
|
314
|
-
ZeroOrMore(.whitespace)
|
|
315
|
-
Capture {
|
|
316
|
-
cssNumberOrPercent
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
ZeroOrMore(.whitespace)
|
|
320
|
-
")"
|
|
321
|
-
}.ignoresCase()
|
|
322
|
-
|
|
323
|
-
nonisolated(unsafe) private let hslCommaRegex = Regex {
|
|
324
|
-
"hsl"
|
|
325
|
-
Optionally {
|
|
326
|
-
"a"
|
|
327
|
-
}
|
|
328
|
-
"("
|
|
329
|
-
ZeroOrMore(.whitespace)
|
|
330
|
-
Capture {
|
|
331
|
-
cssNumber
|
|
332
|
-
}
|
|
333
|
-
commaSeparator
|
|
334
|
-
Capture {
|
|
335
|
-
cssNumber
|
|
336
|
-
}
|
|
337
|
-
"%"
|
|
338
|
-
commaSeparator
|
|
339
|
-
Capture {
|
|
340
|
-
cssNumber
|
|
341
|
-
}
|
|
342
|
-
"%"
|
|
343
|
-
ZeroOrMore(.whitespace)
|
|
344
|
-
Optionally {
|
|
345
|
-
","
|
|
346
|
-
ZeroOrMore(.whitespace)
|
|
347
|
-
Capture {
|
|
348
|
-
cssNumberOrPercent
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
ZeroOrMore(.whitespace)
|
|
352
|
-
")"
|
|
353
|
-
}.ignoresCase()
|
|
354
|
-
|
|
355
|
-
nonisolated(unsafe) private let hslSpaceRegex = Regex {
|
|
356
|
-
"hsl"
|
|
357
|
-
Optionally {
|
|
358
|
-
"a"
|
|
359
|
-
}
|
|
360
|
-
"("
|
|
361
|
-
ZeroOrMore(.whitespace)
|
|
362
|
-
Capture {
|
|
363
|
-
cssNumber
|
|
364
|
-
}
|
|
365
|
-
OneOrMore(.whitespace)
|
|
366
|
-
Capture {
|
|
367
|
-
cssNumber
|
|
368
|
-
}
|
|
369
|
-
"%"
|
|
370
|
-
OneOrMore(.whitespace)
|
|
371
|
-
Capture {
|
|
372
|
-
cssNumber
|
|
373
|
-
}
|
|
374
|
-
"%"
|
|
375
|
-
ZeroOrMore(.whitespace)
|
|
376
|
-
Optionally {
|
|
377
|
-
"/"
|
|
378
|
-
ZeroOrMore(.whitespace)
|
|
379
|
-
Capture {
|
|
380
|
-
cssNumberOrPercent
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
ZeroOrMore(.whitespace)
|
|
384
|
-
")"
|
|
385
|
-
}.ignoresCase()
|
|
386
|
-
|
|
387
|
-
nonisolated(unsafe) private let hwbRegex = Regex {
|
|
388
|
-
"hwb("
|
|
389
|
-
ZeroOrMore(.whitespace)
|
|
390
|
-
Capture {
|
|
391
|
-
cssNumber
|
|
392
|
-
}
|
|
393
|
-
OneOrMore(.whitespace)
|
|
394
|
-
Capture {
|
|
395
|
-
cssNumber
|
|
396
|
-
}
|
|
397
|
-
"%"
|
|
398
|
-
OneOrMore(.whitespace)
|
|
399
|
-
Capture {
|
|
400
|
-
cssNumber
|
|
401
|
-
}
|
|
402
|
-
"%"
|
|
403
|
-
ZeroOrMore(.whitespace)
|
|
404
|
-
Optionally {
|
|
405
|
-
"/"
|
|
406
|
-
ZeroOrMore(.whitespace)
|
|
407
|
-
Capture {
|
|
408
|
-
cssNumberOrPercent
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
ZeroOrMore(.whitespace)
|
|
412
|
-
")"
|
|
413
|
-
}.ignoresCase()
|
|
414
|
-
|
|
415
|
-
// MARK: - CSS Color Function Parsers
|
|
416
|
-
|
|
417
|
-
private func parseRgbComponent(_ value: Substring) -> CGFloat {
|
|
418
|
-
if value.hasSuffix("%") {
|
|
419
|
-
let num = Double(value.dropLast()) ?? 0
|
|
420
|
-
return CGFloat(min(max(num / 100.0, 0), 1))
|
|
421
|
-
}
|
|
422
|
-
let num = Double(value) ?? 0
|
|
423
|
-
return CGFloat(min(max(num / 255.0, 0), 1))
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
private func parseCssAlpha(_ value: Substring?) -> CGFloat {
|
|
427
|
-
guard let value else {
|
|
428
|
-
return 1.0
|
|
429
|
-
}
|
|
430
|
-
if value.hasSuffix("%") {
|
|
431
|
-
let num = Double(value.dropLast()) ?? 0
|
|
432
|
-
return CGFloat(min(max(num / 100.0, 0), 1))
|
|
433
|
-
}
|
|
434
|
-
let num = Double(value) ?? 0
|
|
435
|
-
return CGFloat(min(max(num, 0), 1))
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
private func hueToRgb(_ p: CGFloat, _ q: CGFloat, _ t: CGFloat) -> CGFloat {
|
|
439
|
-
var t = t
|
|
440
|
-
if t < 0 { t += 1 }
|
|
441
|
-
if t > 1 { t -= 1 }
|
|
442
|
-
if t < 1.0 / 6.0 { return p + (q - p) * 6.0 * t }
|
|
443
|
-
if t < 1.0 / 2.0 { return q }
|
|
444
|
-
if t < 2.0 / 3.0 { return p + (q - p) * (2.0 / 3.0 - t) * 6.0 }
|
|
445
|
-
return p
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
private func hslToUIColor(h: CGFloat, s: CGFloat, l: CGFloat, a: CGFloat) -> UIColor {
|
|
449
|
-
let hue =
|
|
450
|
-
((h.truncatingRemainder(dividingBy: 360) + 360).truncatingRemainder(dividingBy: 360)) / 360.0
|
|
451
|
-
let r: CGFloat
|
|
452
|
-
let g: CGFloat
|
|
453
|
-
let b: CGFloat
|
|
454
|
-
if s == 0 {
|
|
455
|
-
r = l
|
|
456
|
-
g = l
|
|
457
|
-
b = l
|
|
458
|
-
} else {
|
|
459
|
-
let q = l < 0.5 ? l * (1 + s) : l + s - l * s
|
|
460
|
-
let p = 2 * l - q
|
|
461
|
-
r = hueToRgb(p, q, hue + 1.0 / 3.0)
|
|
462
|
-
g = hueToRgb(p, q, hue)
|
|
463
|
-
b = hueToRgb(p, q, hue - 1.0 / 3.0)
|
|
464
|
-
}
|
|
465
|
-
return UIColor(
|
|
466
|
-
red: min(max(r, 0), 1),
|
|
467
|
-
green: min(max(g, 0), 1),
|
|
468
|
-
blue: min(max(b, 0), 1),
|
|
469
|
-
alpha: a
|
|
470
|
-
)
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
private func hwbToUIColor(h: CGFloat, w: CGFloat, b: CGFloat, a: CGFloat) -> UIColor {
|
|
474
|
-
let ww = min(max(w, 0), 1 as CGFloat)
|
|
475
|
-
let bb = min(max(b, 0), 1 as CGFloat)
|
|
476
|
-
let sum = ww + bb
|
|
477
|
-
let white = sum > 1 ? ww / sum : ww
|
|
478
|
-
let black = sum > 1 ? bb / sum : bb
|
|
479
|
-
let hue =
|
|
480
|
-
((h.truncatingRemainder(dividingBy: 360) + 360).truncatingRemainder(dividingBy: 360)) / 360.0
|
|
481
|
-
let r = hueToRgb(0, 1, hue + 1.0 / 3.0) * (1 - white - black) + white
|
|
482
|
-
let g = hueToRgb(0, 1, hue) * (1 - white - black) + white
|
|
483
|
-
let bl = hueToRgb(0, 1, hue - 1.0 / 3.0) * (1 - white - black) + white
|
|
484
|
-
return UIColor(
|
|
485
|
-
red: min(max(r, 0), 1),
|
|
486
|
-
green: min(max(g, 0), 1),
|
|
487
|
-
blue: min(max(bl, 0), 1),
|
|
488
|
-
alpha: a
|
|
489
|
-
)
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
private func colorFromRGBString(_ rgbString: String) throws -> UIColor {
|
|
493
|
-
if let match = rgbString.wholeMatch(of: rgbCommaRegex) ?? rgbString.wholeMatch(of: rgbSpaceRegex) {
|
|
494
|
-
let r = parseRgbComponent(match.1)
|
|
495
|
-
let g = parseRgbComponent(match.2)
|
|
496
|
-
let b = parseRgbComponent(match.3)
|
|
497
|
-
let a = parseCssAlpha(match.4)
|
|
498
|
-
return UIColor(red: r, green: g, blue: b, alpha: a)
|
|
499
|
-
}
|
|
500
|
-
throw InvalidRGBColorException(rgbString)
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
private func colorFromHSLString(_ hslString: String) throws -> UIColor {
|
|
504
|
-
if let match = hslString.wholeMatch(of: hslCommaRegex) ?? hslString.wholeMatch(of: hslSpaceRegex) {
|
|
505
|
-
let h = CGFloat(Double(match.1) ?? 0)
|
|
506
|
-
let s = CGFloat(min(max((Double(match.2) ?? 0) / 100.0, 0), 1))
|
|
507
|
-
let l = CGFloat(min(max((Double(match.3) ?? 0) / 100.0, 0), 1))
|
|
508
|
-
let a = parseCssAlpha(match.4)
|
|
509
|
-
return hslToUIColor(h: h, s: s, l: l, a: a)
|
|
510
|
-
}
|
|
511
|
-
throw InvalidHSLColorException(hslString)
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
private func colorFromHWBString(_ hwbString: String) throws -> UIColor {
|
|
515
|
-
if let match = hwbString.wholeMatch(of: hwbRegex) {
|
|
516
|
-
let h = CGFloat(Double(match.1) ?? 0)
|
|
517
|
-
let w = CGFloat((Double(match.2) ?? 0) / 100.0)
|
|
518
|
-
let b = CGFloat((Double(match.3) ?? 0) / 100.0)
|
|
519
|
-
let a = parseCssAlpha(match.4)
|
|
520
|
-
return hwbToUIColor(h: h, w: w, b: b, a: a)
|
|
521
|
-
}
|
|
522
|
-
throw InvalidHWBColorException(hwbString)
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
/// Color components for named colors following the [CSS3/SVG specification](https://www.w3.org/TR/css-color-3/#svg-color)
|
|
526
|
-
/// and additionally the transparent color.
|
|
527
|
-
private let namedColors: [String: [Double]] = [
|
|
528
|
-
"aliceblue": [240, 248, 255, 255],
|
|
529
|
-
"antiquewhite": [250, 235, 215, 255],
|
|
530
|
-
"aqua": [0, 255, 255, 255],
|
|
531
|
-
"aquamarine": [127, 255, 212, 255],
|
|
532
|
-
"azure": [240, 255, 255, 255],
|
|
533
|
-
"beige": [245, 245, 220, 255],
|
|
534
|
-
"bisque": [255, 228, 196, 255],
|
|
535
|
-
"black": [0, 0, 0, 255],
|
|
536
|
-
"blanchedalmond": [255, 235, 205, 255],
|
|
537
|
-
"blue": [0, 0, 255, 255],
|
|
538
|
-
"blueviolet": [138, 43, 226, 255],
|
|
539
|
-
"brown": [165, 42, 42, 255],
|
|
540
|
-
"burlywood": [222, 184, 135, 255],
|
|
541
|
-
"cadetblue": [95, 158, 160, 255],
|
|
542
|
-
"chartreuse": [127, 255, 0, 255],
|
|
543
|
-
"chocolate": [210, 105, 30, 255],
|
|
544
|
-
"coral": [255, 127, 80, 255],
|
|
545
|
-
"cornflowerblue": [100, 149, 237, 255],
|
|
546
|
-
"cornsilk": [255, 248, 220, 255],
|
|
547
|
-
"crimson": [220, 20, 60, 255],
|
|
548
|
-
"cyan": [0, 255, 255, 255],
|
|
549
|
-
"darkblue": [0, 0, 139, 255],
|
|
550
|
-
"darkcyan": [0, 139, 139, 255],
|
|
551
|
-
"darkgoldenrod": [184, 134, 11, 255],
|
|
552
|
-
"darkgray": [169, 169, 169, 255],
|
|
553
|
-
"darkgreen": [0, 100, 0, 255],
|
|
554
|
-
"darkgrey": [169, 169, 169, 255],
|
|
555
|
-
"darkkhaki": [189, 183, 107, 255],
|
|
556
|
-
"darkmagenta": [139, 0, 139, 255],
|
|
557
|
-
"darkolivegreen": [85, 107, 47, 255],
|
|
558
|
-
"darkorange": [255, 140, 0, 255],
|
|
559
|
-
"darkorchid": [153, 50, 204, 255],
|
|
560
|
-
"darkred": [139, 0, 0, 255],
|
|
561
|
-
"darksalmon": [233, 150, 122, 255],
|
|
562
|
-
"darkseagreen": [143, 188, 143, 255],
|
|
563
|
-
"darkslateblue": [72, 61, 139, 255],
|
|
564
|
-
"darkslategray": [47, 79, 79, 255],
|
|
565
|
-
"darkslategrey": [47, 79, 79, 255],
|
|
566
|
-
"darkturquoise": [0, 206, 209, 255],
|
|
567
|
-
"darkviolet": [148, 0, 211, 255],
|
|
568
|
-
"deeppink": [255, 20, 147, 255],
|
|
569
|
-
"deepskyblue": [0, 191, 255, 255],
|
|
570
|
-
"dimgray": [105, 105, 105, 255],
|
|
571
|
-
"dimgrey": [105, 105, 105, 255],
|
|
572
|
-
"dodgerblue": [30, 144, 255, 255],
|
|
573
|
-
"firebrick": [178, 34, 34, 255],
|
|
574
|
-
"floralwhite": [255, 250, 240, 255],
|
|
575
|
-
"forestgreen": [34, 139, 34, 255],
|
|
576
|
-
"fuchsia": [255, 0, 255, 255],
|
|
577
|
-
"gainsboro": [220, 220, 220, 255],
|
|
578
|
-
"ghostwhite": [248, 248, 255, 255],
|
|
579
|
-
"gold": [255, 215, 0, 255],
|
|
580
|
-
"goldenrod": [218, 165, 32, 255],
|
|
581
|
-
"gray": [128, 128, 128, 255],
|
|
582
|
-
"green": [0, 128, 0, 255],
|
|
583
|
-
"greenyellow": [173, 255, 47, 255],
|
|
584
|
-
"grey": [128, 128, 128, 255],
|
|
585
|
-
"honeydew": [240, 255, 240, 255],
|
|
586
|
-
"hotpink": [255, 105, 180, 255],
|
|
587
|
-
"indianred": [205, 92, 92, 255],
|
|
588
|
-
"indigo": [75, 0, 130, 255],
|
|
589
|
-
"ivory": [255, 255, 240, 255],
|
|
590
|
-
"khaki": [240, 230, 140, 255],
|
|
591
|
-
"lavender": [230, 230, 250, 255],
|
|
592
|
-
"lavenderblush": [255, 240, 245, 255],
|
|
593
|
-
"lawngreen": [124, 252, 0, 255],
|
|
594
|
-
"lemonchiffon": [255, 250, 205, 255],
|
|
595
|
-
"lightblue": [173, 216, 230, 255],
|
|
596
|
-
"lightcoral": [240, 128, 128, 255],
|
|
597
|
-
"lightcyan": [224, 255, 255, 255],
|
|
598
|
-
"lightgoldenrodyellow": [250, 250, 210, 255],
|
|
599
|
-
"lightgray": [211, 211, 211, 255],
|
|
600
|
-
"lightgreen": [144, 238, 144, 255],
|
|
601
|
-
"lightgrey": [211, 211, 211, 255],
|
|
602
|
-
"lightpink": [255, 182, 193, 255],
|
|
603
|
-
"lightsalmon": [255, 160, 122, 255],
|
|
604
|
-
"lightseagreen": [32, 178, 170, 255],
|
|
605
|
-
"lightskyblue": [135, 206, 250, 255],
|
|
606
|
-
"lightslategray": [119, 136, 153, 255],
|
|
607
|
-
"lightslategrey": [119, 136, 153, 255],
|
|
608
|
-
"lightsteelblue": [176, 196, 222, 255],
|
|
609
|
-
"lightyellow": [255, 255, 224, 255],
|
|
610
|
-
"lime": [0, 255, 0, 255],
|
|
611
|
-
"limegreen": [50, 205, 50, 255],
|
|
612
|
-
"linen": [250, 240, 230, 255],
|
|
613
|
-
"magenta": [255, 0, 255, 255],
|
|
614
|
-
"maroon": [128, 0, 0, 255],
|
|
615
|
-
"mediumaquamarine": [102, 205, 170, 255],
|
|
616
|
-
"mediumblue": [0, 0, 205, 255],
|
|
617
|
-
"mediumorchid": [186, 85, 211, 255],
|
|
618
|
-
"mediumpurple": [147, 112, 219, 255],
|
|
619
|
-
"mediumseagreen": [60, 179, 113, 255],
|
|
620
|
-
"mediumslateblue": [123, 104, 238, 255],
|
|
621
|
-
"mediumspringgreen": [0, 250, 154, 255],
|
|
622
|
-
"mediumturquoise": [72, 209, 204, 255],
|
|
623
|
-
"mediumvioletred": [199, 21, 133, 255],
|
|
624
|
-
"midnightblue": [25, 25, 112, 255],
|
|
625
|
-
"mintcream": [245, 255, 250, 255],
|
|
626
|
-
"mistyrose": [255, 228, 225, 255],
|
|
627
|
-
"moccasin": [255, 228, 181, 255],
|
|
628
|
-
"navajowhite": [255, 222, 173, 255],
|
|
629
|
-
"navy": [0, 0, 128, 255],
|
|
630
|
-
"oldlace": [253, 245, 230, 255],
|
|
631
|
-
"olive": [128, 128, 0, 255],
|
|
632
|
-
"olivedrab": [107, 142, 35, 255],
|
|
633
|
-
"orange": [255, 165, 0, 255],
|
|
634
|
-
"orangered": [255, 69, 0, 255],
|
|
635
|
-
"orchid": [218, 112, 214, 255],
|
|
636
|
-
"palegoldenrod": [238, 232, 170, 255],
|
|
637
|
-
"palegreen": [152, 251, 152, 255],
|
|
638
|
-
"paleturquoise": [175, 238, 238, 255],
|
|
639
|
-
"palevioletred": [219, 112, 147, 255],
|
|
640
|
-
"papayawhip": [255, 239, 213, 255],
|
|
641
|
-
"peachpuff": [255, 218, 185, 255],
|
|
642
|
-
"peru": [205, 133, 63, 255],
|
|
643
|
-
"pink": [255, 192, 203, 255],
|
|
644
|
-
"plum": [221, 160, 221, 255],
|
|
645
|
-
"powderblue": [176, 224, 230, 255],
|
|
646
|
-
"purple": [128, 0, 128, 255],
|
|
647
|
-
"rebeccapurple": [102, 51, 153, 255],
|
|
648
|
-
"red": [255, 0, 0, 255],
|
|
649
|
-
"rosybrown": [188, 143, 143, 255],
|
|
650
|
-
"royalblue": [65, 105, 225, 255],
|
|
651
|
-
"saddlebrown": [139, 69, 19, 255],
|
|
652
|
-
"salmon": [250, 128, 114, 255],
|
|
653
|
-
"sandybrown": [244, 164, 96, 255],
|
|
654
|
-
"seagreen": [46, 139, 87, 255],
|
|
655
|
-
"seashell": [255, 245, 238, 255],
|
|
656
|
-
"sienna": [160, 82, 45, 255],
|
|
657
|
-
"silver": [192, 192, 192, 255],
|
|
658
|
-
"skyblue": [135, 206, 235, 255],
|
|
659
|
-
"slateblue": [106, 90, 205, 255],
|
|
660
|
-
"slategray": [112, 128, 144, 255],
|
|
661
|
-
"slategrey": [112, 128, 144, 255],
|
|
662
|
-
"snow": [255, 250, 250, 255],
|
|
663
|
-
"springgreen": [0, 255, 127, 255],
|
|
664
|
-
"steelblue": [70, 130, 180, 255],
|
|
665
|
-
"tan": [210, 180, 140, 255],
|
|
666
|
-
"teal": [0, 128, 128, 255],
|
|
667
|
-
"thistle": [216, 191, 216, 255],
|
|
668
|
-
"tomato": [255, 99, 71, 255],
|
|
669
|
-
"transparent": [0, 0, 0, 0],
|
|
670
|
-
"turquoise": [64, 224, 208, 255],
|
|
671
|
-
"violet": [238, 130, 238, 255],
|
|
672
|
-
"wheat": [245, 222, 179, 255],
|
|
673
|
-
"white": [255, 255, 255, 255],
|
|
674
|
-
"whitesmoke": [245, 245, 245, 255],
|
|
675
|
-
"yellow": [255, 255, 0, 255],
|
|
676
|
-
"yellowgreen": [154, 205, 50, 255],
|
|
677
|
-
].mapValues { $0.map { $0 / 255.0 } }
|
|
678
|
-
|
|
679
|
-
// MARK: - Color Conversion Exceptions
|
|
680
|
-
|
|
681
|
-
internal class InvalidHexColorException: GenericException<String>, @unchecked Sendable {
|
|
682
|
-
override var reason: String {
|
|
683
|
-
"Provided hex color '\(param)' is invalid"
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
internal class InvalidRGBColorException: GenericException<String>, @unchecked Sendable {
|
|
688
|
-
override var reason: String {
|
|
689
|
-
"Provided rgb color string '\(param)' is invalid"
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
internal class InvalidHSLColorException: GenericException<String>, @unchecked Sendable {
|
|
694
|
-
override var reason: String {
|
|
695
|
-
"Provided hsl color string '\(param)' is invalid"
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
internal class InvalidHWBColorException: GenericException<String>, @unchecked Sendable {
|
|
700
|
-
override var reason: String {
|
|
701
|
-
"Provided hwb color string '\(param)' is invalid"
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
internal class HexColorOverflowException: GenericException<UInt64>, @unchecked Sendable {
|
|
706
|
-
override var reason: String {
|
|
707
|
-
"Provided hex color '\(param)' would result in an overflow"
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
internal class InvalidColorComponentsException: GenericException<Int>, @unchecked Sendable {
|
|
712
|
-
override var reason: String {
|
|
713
|
-
"Color components array must contain at least 3 values (red, green, blue), but got \(param)"
|
|
714
|
-
}
|
|
715
|
-
}
|