-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathNSColor+HSL.swift
More file actions
143 lines (115 loc) · 3.98 KB
/
Copy pathNSColor+HSL.swift
File metadata and controls
143 lines (115 loc) · 3.98 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
import Cocoa
import Defaults
// swiftlint:disable identifier_name
// identifier_name is disabled because color science math uses conventional single-letter
// variable names (h, s, b, l, r, g) that would be misleading if renamed.
public struct HSBComponents { let h, s, b: CGFloat }
public struct HSLComponents { let h, s, l: CGFloat }
extension NSColor {
/*
* HSB
*/
public final func toHSBComponents() -> HSBComponents {
var h: CGFloat = 0.0
var s: CGFloat = 0.0
var b: CGFloat = 0.0
guard let rgbaColor = usingColorSpace(Defaults[.colorSpace]) else {
fatalError("Could not convert color to RGBA.")
}
if toHexString() == NSColor.black.toHexString() {
return HSBComponents(h: 0.0, s: 0.0, b: 0.0)
} else if toHexString() == NSColor.white.toHexString() {
return HSBComponents(h: 0.0, s: 0.0, b: 1.0)
}
rgbaColor.getHue(&h, saturation: &s, brightness: &b, alpha: nil)
h = h.truncatingRemainder(dividingBy: 1.0)
return HSBComponents(h: h, s: s, b: b)
}
/**
Get the hsb values of this color in 8-bit format.
- returns: An NSColor as an 8-bit hsb string.
*/
func toHSBString(style: CopyFormat = .css) -> String {
let HSB = toHSBComponents()
let hue = Int(round(HSB.h * 360))
let saturation = Int(round(HSB.s * 100))
let brightness = Int(round(HSB.b * 100))
let hsbString: String
switch style {
case .css:
hsbString = String(format: "hsb(%d, %d%%, %d%%)", hue, saturation, brightness)
case .design:
hsbString = String(format: "hsb(%d, %d, %d)", hue, saturation, brightness)
case .swiftUI:
hsbString = String(format: "Color(hue: %.5g, saturation: %.5g, brightness: %.5g)", HSB.h, HSB.s, HSB.b)
case .unformatted:
hsbString = String(format: "%d, %d, %d", hue, saturation, brightness)
}
return hsbString
}
/*
* HSL
*/
public final func toHSLComponents() -> HSLComponents {
var h: CGFloat = 0.0
var s: CGFloat = 0.0
var l: CGFloat = 0.0
let RGB = toRGBAComponents()
let r = RGB.r
let g = RGB.g
let b = RGB.b
if toHexString() == NSColor.black.toHexString() {
return HSLComponents(h: 0.0, s: 0.0, l: 0.0)
} else if toHexString() == NSColor.white.toHexString() {
return HSLComponents(h: 0.0, s: 0.0, l: 1.0)
}
let min = Swift.min(Swift.min(r, g), b)
let max = Swift.max(Swift.max(r, g), b)
let delta = max - min
if max == min {
h = 0
} else if r == max {
h = (g - b) / delta
} else if g == max {
h = 2 + (b - r) / delta
} else {
h = 4 + (r - g) / delta
}
h = Swift.min(h * 60, 360)
if h < 0 {
h += 360
}
h /= 360
l = (min + max) / 2
if max == min {
s = 0
} else if l <= 0.5 {
s = delta / (max + min)
} else {
s = delta / (2 - max - min)
}
return HSLComponents(h: h, s: s, l: l)
}
/**
Get the hsl values of this color in 8-bit format.
- returns: An NSColor as an 8-bit hsl string.
*/
func toHSLString(style: CopyFormat = .css) -> String {
let HSL = toHSLComponents()
let hue = Int(round(HSL.h * 360))
let saturation = Int(round(HSL.s * 100))
let lightness = Int(round(HSL.l * 100))
let formatString: NSString
switch style {
case .css:
formatString = "hsl(%d, %d%%, %d%%)"
case .design, .swiftUI:
formatString = "hsl(%d, %d, %d)"
case .unformatted:
formatString = "%d, %d, %d"
}
let hslString = NSString(format: formatString, hue, saturation, lightness)
return hslString as String
}
}
// swiftlint:enable identifier_name