-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathNSColor+Init.swift
More file actions
44 lines (37 loc) · 1.43 KB
/
Copy pathNSColor+Init.swift
File metadata and controls
44 lines (37 loc) · 1.43 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
import Cocoa
extension NSColor {
// r, g, b, a are the public parameter labels for this init (NSColor(r:g:b:a:)) and cannot be renamed.
// swiftlint:disable:next identifier_name
convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat = 1) {
if (r > 1) || (g > 1) || (b > 1) {
self.init(red: r / 255, green: g / 255, blue: b / 255, alpha: a)
} else {
self.init(red: r, green: g, blue: b, alpha: a)
}
}
/**
Create an NSColor with a string hex value.
- parameter hex: The hex color, i.e. "FF0072" or "#FF0072".
- parameter alpha: The opacity of the color, value between [0,1]. Optional. Default: 1
*/
convenience init(hex: String, alpha: CGFloat = 1) {
var hex = hex.replacingOccurrences(of: "#", with: "")
guard hex.count == 3 || hex.count == 6 else {
fatalError("Hex characters must be either 3 or 6 characters.")
}
if hex.count == 3 {
let tmp = hex
hex = ""
for char in tmp {
hex += String([char, char])
}
}
let scanner = Scanner(string: hex)
var rgb: UInt64 = 0
scanner.scanHexInt64(&rgb)
let red = CGFloat((rgb >> 16) & 0xFF) / 255
let green = CGFloat((rgb >> 8) & 0xFF) / 255
let blue = CGFloat(rgb & 0xFF) / 255
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
}