-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathDynamicColor.swift
More file actions
222 lines (170 loc) · 7.5 KB
/
DynamicColor.swift
File metadata and controls
222 lines (170 loc) · 7.5 KB
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
/*
* DynamicColor
*
* Copyright 2015-present Yannick Loriot.
* http://yannickloriot.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#if os(iOS) || os(tvOS) || os(watchOS)
import UIKit
/**
Extension to manipulate colours easily.
It allows you to work hexadecimal strings and value, HSV and RGB components, derivating colours, and many more...
*/
public typealias DynamicColor = UIColor
#elseif os(OSX)
import AppKit
/**
Extension to manipulate colours easily.
It allows you to work hexadecimal strings and value, HSV and RGB components, derivating colours, and many more...
*/
public typealias DynamicColor = NSColor
#endif
public extension DynamicColor {
// MARK: - Manipulating Hexa-decimal Values and Strings
/**
Creates a color from an hex string (e.g. "#3498db").
If the given hex string is invalid the initialiser will create a black color.
- parameter hexString: A hexa-decimal color string representation.
*/
public convenience init(hexString: String) {
let hexString = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let scanner = Scanner(string: hexString)
if hexString.hasPrefix("#") {
scanner.scanLocation = 1
}
var color: UInt32 = 0
if scanner.scanHexInt32(&color) {
self.init(hex: color, useAlpha: hexString.characters.count > 7)
}
else {
self.init(hex: 0x000000)
}
}
/**
Creates a color from an hex integer (e.g. 0x3498db).
- parameter hex: A hexa-decimal UInt32 that represents a color.
- parameter alphaChannel: If true the given hex-decimal UInt32 includes the alpha channel (e.g. 0xFF0000FF).
*/
public convenience init(hex: UInt32, useAlpha alphaChannel: Bool = false) {
let mask = 0xFF
let r = Int(hex >> (alphaChannel ? 24 : 16)) & mask
let g = Int(hex >> (alphaChannel ? 16 : 8)) & mask
let b = Int(hex >> (alphaChannel ? 8 : 0)) & mask
let a = alphaChannel ? Int(hex) & mask : 255
let red = CGFloat(r) / 255
let green = CGFloat(g) / 255
let blue = CGFloat(b) / 255
let alpha = CGFloat(a) / 255
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
/**
Returns the color representation as hexadecimal string.
- returns: A string similar to this pattern "#f4003b".
*/
public final func toHexString() -> String {
return String(format:"#%06x", toHex())
}
/**
Returns the color representation as an integer.
- returns: A UInt32 that represents the hexa-decimal color.
*/
public final func toHex() -> UInt32 {
func roundToHex(_ x: CGFloat) -> UInt32 {
return UInt32(roundf(Float(x) * 255.0))
}
let rgba = toRGBAComponents()
let colorToInt = roundToHex(rgba.r) << 16 | roundToHex(rgba.g) << 8 | roundToHex(rgba.b)
return colorToInt
}
// MARK: - Identifying and Comparing Colors
/**
Returns a boolean value that indicates whether the receiver is equal to the given hexa-decimal string.
- parameter hexString: A hexa-decimal color number representation to be compared to the receiver.
- returns: true if the receiver and the string are equals, otherwise false.
*/
public func isEqual(toHexString hexString: String) -> Bool {
return self.toHexString() == hexString
}
/**
Returns a boolean value that indicates whether the receiver is equal to the given hexa-decimal integer.
- parameter hex: A UInt32 that represents the hexa-decimal color.
- returns: true if the receiver and the integer are equals, otherwise false.
*/
public func isEqual(toHex hex: UInt32) -> Bool {
return self.toHex() == hex
}
// MARK: - Querying Colors
/**
Determines if the color object is dark or light.
It is useful when you need to know whether you should display the text in black or white.
- returns: A boolean value to know whether the color is light. If true the color is light, dark otherwise.
*/
public func isLight() -> Bool {
let components = toRGBAComponents()
let brightness = ((components.r * 299) + (components.g * 587) + (components.b * 114)) / 1000
return brightness >= 0.5
}
/**
A float value representing the luminance of the current color. May vary from 0 to 1.0.
We use the formula described by W3C in WCAG 2.0. You can read more here: https://www.w3.org/TR/WCAG20/#relativeluminancedef
*/
public var luminance: CGFloat {
let components = toRGBAComponents()
let componentsArray = [components.r, components.g, components.b].map { (val) -> CGFloat in
guard val <= 0.03928 else { return pow((val + 0.055) / 1.055, 2.4) }
return val / 12.92
}
return (0.2126 * componentsArray[0]) + (0.7152 * componentsArray[1]) + (0.0722 * componentsArray[2])
}
/**
Returns a float value representing the contrast ratio between 2 colors.
We use the formula described by W3C in WCAG 2.0. You can read more here: https://www.w3.org/TR/WCAG20-TECHS/G18.html
NB: the contrast ratio is a relative value. So the contrast between Color1 and Color2 is exactly the same between Color2 and Color1.
- returns: A CGFloat representing contrast value
*/
public func contrastRatio(with otherColor: DynamicColor) -> CGFloat {
let otherLuminance = otherColor.luminance
let l1 = max(luminance, otherLuminance)
let l2 = min(luminance, otherLuminance)
return (l1 + 0.05) / (l2 + 0.05)
}
/**
Indicates if two colors are contrasting, regarding W3C's WCAG 2.0 recommendations.
You can read it here: https://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast-contrast
The acceptable contrast ratio depends on the context of display. Most of the time, the default context (.Standard) is enough.
You can look at ContrastDisplayContext for more options.
- parameter otherColor: The other color to compare with.
- parameter context: An optional context to determine the minimum acceptable contrast ratio. Default value is .Standard.
- returns: true is the contrast ratio between 2 colors exceed the minimum acceptable ratio.
*/
public func isContrasting(with otherColor: DynamicColor, inContext context: ContrastDisplayContext = .standard) -> Bool {
return self.contrastRatio(with: otherColor) > context.minimumContrastRatio
}
/**
Return a boolean value which indicates if the current color is opaque.
This is a shortcut for "color.alphaComponent == 1.0"
- returns: true if this color is opaque, false otherwise
*/
public var isOpaque: Bool {
return self.alphaComponent == 1.0
}
}