-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColor.swift
384 lines (307 loc) · 9.96 KB
/
Color.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//
// Copyright (c) Vatsal Manot
//
import Swift
import SwiftUI
extension Color {
public static var almostClear: Color {
Color.black.opacity(0.0001)
}
}
extension Color {
/// A color for placeholder text in controls or text fields or text views.
public static var placeholderText: Color {
#if os(iOS) || os(macOS) || os(tvOS)
return .init(.placeholderText)
#else
return .gray // FIXME
#endif
}
}
#if os(iOS) || os(macOS) || os(tvOS)
extension Color {
public static var systemRed: Color {
.init(.systemRed)
}
public static var systemGreen: Color {
.init(.systemGreen)
}
public static var systemBlue: Color {
.init(.systemBlue)
}
public static var systemOrange: Color {
.init(.systemOrange)
}
public static var systemYellow: Color {
.init(.systemYellow)
}
public static var systemPink: Color {
.init(.systemPink)
}
public static var systemPurple: Color {
.init(.systemPurple)
}
public static var systemTeal: Color {
.init(.systemTeal)
}
public static var systemIndigo: Color {
.init(.systemIndigo)
}
public static var systemGray: Color {
.init(.systemGray)
}
}
#endif
#if os(iOS) || targetEnvironment(macCatalyst)
extension Color {
public static var brown: Color {
return .init(.brown)
}
public static var indigo: Color {
.init(.systemIndigo)
}
public static var teal: Color {
.init(.systemTeal)
}
}
extension Color {
public static let systemGray2: Color = Color(.systemGray2)
public static let systemGray3: Color = Color(.systemGray3)
public static let systemGray4: Color = Color(.systemGray4)
public static let systemGray5: Color = Color(.systemGray5)
public static let systemGray6: Color = Color(.systemGray6)
}
#endif
#if os(iOS) || os(macOS) || os(tvOS) || targetEnvironment(macCatalyst)
/// Foreground colors for static text and related elements.
extension Color {
/// The color for text labels that contain primary content.
public static var label: Color {
#if os(macOS)
return .init(.labelColor)
#else
return .init(.label)
#endif
}
/// The color for text labels that contain secondary content.
public static var secondaryLabel: Color {
#if os(macOS)
return .init(.secondaryLabelColor)
#else
return .init(.secondaryLabel)
#endif
}
/// The color for text labels that contain tertiary content.
public static var tertiaryLabel: Color {
#if os(macOS)
return .init(.tertiaryLabelColor)
#else
return .init(.tertiaryLabel)
#endif
}
/// The color for text labels that contain quaternary content.
public static var quaternaryLabel: Color {
#if os(macOS)
return .init(.quaternaryLabelColor)
#else
return .init(.quaternaryLabel)
#endif
}
}
#endif
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
extension Color {
/// A foreground color for standard system links.
public static var link: Color {
return .init(.link)
}
/// A forground color for separators (thin border or divider lines).
public static var separator: Color {
return .init(.separator)
}
/// A forground color intended to look similar to `Color.separated`, but is guaranteed to be opaque, so it will.
public static var opaqueSeparator: Color {
return .init(.opaqueSeparator)
}
}
#endif
#if os(iOS) || targetEnvironment(macCatalyst)
extension Color {
/// The color for the main background of your interface.
public static var systemBackground: Color {
.init(.systemBackground)
}
/// The color for content layered on top of the main background.
public static var secondarySystemBackground: Color {
return .init(.secondarySystemBackground)
}
/// The color for content layered on top of secondary backgrounds.
public static var tertiarySystemBackground: Color {
return .init(.tertiarySystemBackground)
}
/// The color for the main background of your grouped interface.
public static var systemGroupedBackground: Color {
.init(.systemGroupedBackground)
}
/// The color for content layered on top of the main background of your grouped interface.
public static var secondarySystemGroupedBackground: Color {
return .init(.secondarySystemGroupedBackground)
}
/// The color for content layered on top of secondary backgrounds of your grouped interface.
public static var tertiarySystemGroupedBackground: Color {
return .init(.tertiarySystemGroupedBackground)
}
}
/// Fill colors for UI elements.
/// These are meant to be used over the background colors, since their alpha component is less than 1.
extension Color {
/// A color appropriate for filling thin and small shapes.
///
/// Example: The track of a slider.
public static var systemFill: Color {
.init(.systemFill)
}
/// A color appropriate for filling medium-size shapes.
///
/// Example: The background of a switch.
public static var secondarySystemFill: Color {
return .init(.secondarySystemFill)
}
/// A color appropriate for filling large shapes.
///
/// Examples: Input fields, search bars, buttons.
public static var tertiarySystemFill: Color {
return .init(.tertiarySystemFill)
}
/// A color appropriate for filling large areas containing complex content.
///
/// Example: Expanded table cells.
public static var quaternarySystemFill: Color {
return .init(.quaternarySystemFill)
}
}
#endif
extension Color {
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
/// A color that adapts to the preferred color scheme.
///
/// - Parameters:
/// - light: The preferred color for a light color scheme.
/// - dark: The preferred color for a dark color scheme.
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, *)
public static func adaptable(
light: @escaping @autoclosure () -> Color,
dark: @escaping @autoclosure () -> Color
) -> Color {
Color(
UIColor.adaptable(
light: UIColor(light()),
dark: UIColor(dark())
)
)
}
#endif
}
extension Color {
public init(
cube256 colorSpace: RGBColorSpace,
red: Int,
green: Int,
blue: Int,
opacity: Double = 1.0
) {
self.init(
colorSpace,
red: Double(red) / 255.0,
green: Double(green) / 255.0,
blue: Double(blue) / 255.0,
opacity: opacity
)
}
}
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
extension Color {
/// Creates a color from a hexadecimal color code.
///
/// - Parameter hexadecimal: A hexadecimal representation of the color.
///
/// - Returns: A `Color` from the given color code. Returns `nil` if the code is invalid.
public init!(hexadecimal string: String) {
var string: String = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
if string.hasPrefix("#") {
_ = string.removeFirst()
}
if !string.count.isMultiple(of: 2), let last = string.last {
string.append(last)
}
if string.count > 8 {
string = String(string.prefix(8))
}
let scanner = Scanner(string: string)
var color: UInt64 = 0
scanner.scanHexInt64(&color)
if string.count == 2 {
let mask = 0xFF
let g = Int(color) & mask
let gray = Double(g) / 255.0
self.init(.sRGB, red: gray, green: gray, blue: gray, opacity: 1)
} else if string.count == 4 {
let mask = 0x00FF
let g = Int(color >> 8) & mask
let a = Int(color) & mask
let gray = Double(g) / 255.0
let alpha = Double(a) / 255.0
self.init(.sRGB, red: gray, green: gray, blue: gray, opacity: alpha)
} else if string.count == 6 {
let mask = 0x0000FF
let r = Int(color >> 16) & mask
let g = Int(color >> 8) & mask
let b = Int(color) & mask
let red = Double(r) / 255.0
let green = Double(g) / 255.0
let blue = Double(b) / 255.0
self.init(.sRGB, red: red, green: green, blue: blue, opacity: 1)
} else if string.count == 8 {
let mask = 0x000000FF
let r = Int(color >> 24) & mask
let g = Int(color >> 16) & mask
let b = Int(color >> 8) & mask
let a = Int(color) & mask
let red = Double(r) / 255.0
let green = Double(g) / 255.0
let blue = Double(b) / 255.0
let alpha = Double(a) / 255.0
self.init(.sRGB, red: red, green: green, blue: blue, opacity: alpha)
} else {
return nil
}
}
/// Creates a color from a 6-digit hexadecimal color code.
public init(hexadecimal6: Int) {
let red = Double((hexadecimal6 & 0xFF0000) >> 16) / 255.0
let green = Double((hexadecimal6 & 0x00FF00) >> 8) / 255.0
let blue = Double(hexadecimal6 & 0x0000FF) / 255.0
self.init(red: red, green: green, blue: blue)
}
}
// MARK: - Auxiliary Implementation -
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
extension UIColor {
class func adaptable(
light: @escaping @autoclosure () -> UIColor,
dark: @escaping @autoclosure () -> UIColor
) -> UIColor {
UIColor { traitCollection in
switch traitCollection.userInterfaceStyle {
case .light:
return light()
case .dark:
return dark()
default:
return light()
}
}
}
}
#endif
#endif