-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (39 loc) · 1.52 KB
/
index.js
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
const { spawn } = require("child_process")
const rgbMatch = /[0-9]{1,5}, [0-9]{1,5}, [0-9]{1,5}/
const withinChannelRange = x => typeof x === "number" && x >= 0 && 255 >= x
const platform = require("os").platform()
module.exports = (r = 255, g = 255, b = 255) => {
if (!withinChannelRange(r) || !withinChannelRange(g) || !withinChannelRange(b)) {
throw new Error("RGB values are not numbers or not between 0-255.")
}
if (platform !== "darwin") {
throw new Error("This colour picker requires macOS.")
}
const sixteenBitR = (r / 255) * 65535
const sixteenBitG = (g / 255) * 65535
const sixteenBitB = (b / 255) * 65535
const child = spawn("osascript", ["-e", `choose color default color {${sixteenBitR}, ${sixteenBitG}, ${sixteenBitB}}`])
return new Promise((res, rej) => {
let done = false
child.on("error", e => {
done = true
rej(e)
})
child.stdout.on("data", part => {
const chunk = part.toString().trim()
if (chunk.match(rgbMatch)) {
const parts = chunk.split(",")
const r = Math.floor((Number(parts[0].trim()) / 65535) * 255)
const g = Math.floor((Number(parts[1].trim()) / 65535) * 255)
const b = Math.floor((Number(parts[2].trim()) / 65535) * 255)
done = true
res([r, g, b])
}
})
child.on("exit", () => {
if (!done) {
res([r, g, b])
}
})
})
}