forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample-color.swift
executable file
·52 lines (44 loc) · 1.19 KB
/
sample-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
#!/usr/bin/swift
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Sample Color
// @raycast.mode silent
// @raycast.packageName System
//
// Optional parameters:
// @raycast.icon 🎨
//
// Documentation:
// @raycast.description Sample a color from anywhere on your screen.
// @raycast.author Jesse Claven
// @raycast.authorURL https://github.com/jesse-c
import AppKit
guard #available(OSX 10.15, *) else {
print("macOS 10.15 or greater required")
exit(1)
}
extension NSColor {
var hexAlphaString: String {
let r = lroundf(Float(redComponent) * 0xFF)
let g = lroundf(Float(greenComponent) * 0xFF)
let b = lroundf(Float(blueComponent) * 0xFF)
return String(format: "#%02lX%02lX%02lX", r, g, b)
}
}
func copyToPasteboard(_ color: String) {
NSPasteboard.general.clearContents()
NSPasteboard.general.writeObjects([color as NSPasteboardWriting])
}
let sampler = NSColorSampler()
sampler.show { selectedColor in
if let selectedColor = selectedColor {
let hexTuple = selectedColor.hexAlphaString
copyToPasteboard(hexTuple)
print("Sampled color: \(hexTuple)")
exit(0)
} else {
print("Sampled color: none")
exit(0)
}
}
RunLoop.main.run()