-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColor-extension.swift
130 lines (113 loc) · 5 KB
/
Color-extension.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
//
// Color-extension.swift
// Color-extension
//
// Created by Philipp on 19.08.21.
//
import SwiftUI
import UIKit
/// A representation of a color that adapts to a given context.
///
/// This extension implements the new semantic colors introduced in iOS 15.
///
extension Color {
#if (TARGET_IOS_MAJOR_13 || TARGET_IOS_MAJOR_14)
@available(iOS, introduced: 13, obsoleted: 15.0,
message: "Backport not necessary as of iOS 15", renamed: "SwiftUI.Color.mint")
public static var mint: Color {
Color(UIColor(dynamicProvider: { trait in
if trait.userInterfaceStyle == .light {
return UIColor(red: 0, green: 199/255.0, blue: 190/255.0, alpha: 1)
} else {
return UIColor(red: 99/255.0, green: 230/255.0, blue: 226/255.0, alpha: 1)
}
}))
}
@available(iOS, introduced: 13, obsoleted: 15.0,
message: "Backport not necessary as of iOS 15", renamed: "SwiftUI.Color.red")
public static var teal: Color {
Color(UIColor(dynamicProvider: { trait in
if trait.userInterfaceStyle == .light {
return UIColor(red: 48/255.0, green: 176/255.0, blue: 199/255.0, alpha: 1)
} else {
return UIColor(red: 64/255.0, green: 200/255.0, blue: 224/255.0, alpha: 1)
}
}))
}
@available(iOS, introduced: 13, obsoleted: 15.0,
message: "Backport not necessary as of iOS 15", renamed: "SwiftUI.Color.cyan")
public static var cyan: Color {
Color(UIColor(dynamicProvider: { trait in
if trait.userInterfaceStyle == .light {
return UIColor(red: 50/255.0, green: 173/255.0, blue: 230/255.0, alpha: 1)
} else {
return UIColor(red: 100/255.0, green: 210/255.0, blue: 255/255.0, alpha: 1)
}
}))
}
@available(iOS, introduced: 13, obsoleted: 15.0,
message: "Backport not necessary as of iOS 15", renamed: "SwiftUI.Color.indigo")
public static var indigo: Color {
Color(UIColor(dynamicProvider: { trait in
if trait.userInterfaceStyle == .light {
return UIColor(red: 88/255.0, green: 86/255.0, blue: 214/255.0, alpha: 1)
} else {
return UIColor(red: 93/255.0, green: 92/255.0, blue: 230/255.0, alpha: 1)
}
}))
}
@available(iOS, introduced: 13, obsoleted: 15.0,
message: "Backport not necessary as of iOS 15", renamed: "SwiftUI.Color.brown")
public static var brown: Color {
Color(UIColor(dynamicProvider: { trait in
if trait.userInterfaceStyle == .light {
return UIColor(red: 162/255.0, green: 132/255.0, blue: 93.5/255.0, alpha: 1)
} else {
return UIColor(red: 172/255.0, green: 142/255.0, blue: 104/255.0, alpha: 1)
}
}))
}
#endif
}
extension Color {
#if (TARGET_IOS_MAJOR_13 || TARGET_IOS_MAJOR_14)
@available(iOS, introduced: 13, obsoleted: 15.0,
message: "Backport not necessary as of iOS 15", renamed: "SwiftUI.Color(cgColor:)")
public init(cgColor color: CGColor) {
self = Color(UIColor(cgColor: color))
}
@available(iOS, introduced: 13, obsoleted: 15.0,
message: "Backport not necessary as of iOS 15", renamed: "SwiftUI.Color(uiColor:)")
public init(uiColor color: UIColor) {
self = Color(color)
}
#endif
#if TARGET_IOS_MAJOR_13
/// A Core Graphics representation of the color, if available.
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.Color.cgColor")
public var cgColor: CGColor? {
let mirror = Mirror(reflecting: self)
// Handle linear RGB
if let red = mirror.descendant("provider", "base", "linearRed") as? Float,
let green = mirror.descendant("provider", "base", "linearGreen") as? Float,
let blue = mirror.descendant("provider", "base", "linearBlue") as? Float,
let alpha = mirror.descendant("provider", "base", "opacity") as? Float {
return CGColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))
}
// Handle P3
if String(describing: type(of: mirror.descendant("provider", "base")!)) == "DisplayP3",
let red = mirror.descendant("provider", "base", "red") as? CGFloat,
let green = mirror.descendant("provider", "base", "green") as? CGFloat,
let blue = mirror.descendant("provider", "base", "blue") as? CGFloat,
let alpha = mirror.descendant("provider", "base", "opacity") as? Float {
let colorSpace = CGColorSpace(name: CGColorSpace.displayP3)
return CGColor(colorSpace: colorSpace!,
components: [red, green, blue, CGFloat(alpha)])
}
// All dynamic colors using UIDynamicSystemColor or UIDynamicProviderColor
// won't return a value
return nil
}
#endif
}