This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcolor_linux.go
78 lines (66 loc) · 1.78 KB
/
color_linux.go
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
// +build linux,!windows,!darwin,!js
package dlgs
import (
"fmt"
"image/color"
"os/exec"
"strconv"
"strings"
"syscall"
)
// Color displays a color selection dialog, returning the selected color and a bool for success.
func Color(title, defaultColorHex string) (color.Color, bool, error) {
cmd, err := cmdPath()
if err != nil {
return nil, false, err
}
o, err := exec.Command(cmd, "--color-selection", "--title", title, "--color", defaultColorHex).Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
return nil, ws.ExitStatus() == 0, nil
}
}
out := strings.TrimSpace(string(o))
return parseColor(out), true, err
}
// parseColor returns color from output string.
func parseColor(out string) color.Color {
col := color.RGBA{}
if strings.HasPrefix(out, "#") {
var r, g, b uint8
fmt.Sscanf(out, "#%02x%02x%02x", &r, &g, &b)
col.R = uint8(r)
col.G = uint8(g)
col.B = uint8(b)
} else if strings.HasPrefix(out, "rgba(") {
for _, s := range []string{"rgba", "(", ")"} {
out = strings.Replace(out, s, "", -1)
}
t := strings.Split(out, ",")
if len(t) == 4 {
r, _ := strconv.ParseUint(t[0], 10, 8)
g, _ := strconv.ParseUint(t[1], 10, 8)
b, _ := strconv.ParseUint(t[2], 10, 8)
a, _ := strconv.ParseUint(t[3], 10, 8)
col.R = uint8(r)
col.G = uint8(g)
col.B = uint8(b)
col.A = uint8(a)
}
} else if strings.HasPrefix(out, "rgb(") {
for _, s := range []string{"rgb", "(", ")"} {
out = strings.Replace(out, s, "", -1)
}
t := strings.Split(out, ",")
if len(t) == 3 {
r, _ := strconv.ParseUint(t[0], 10, 8)
g, _ := strconv.ParseUint(t[1], 10, 8)
b, _ := strconv.ParseUint(t[2], 10, 8)
col.R = uint8(r)
col.G = uint8(g)
col.B = uint8(b)
}
}
return col
}