-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (101 loc) · 1.82 KB
/
main.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
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
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"os"
)
type plist struct {
Dict []dict `xml:"dict"`
}
type dict struct {
Key []string `xml:"key"`
Real []float64 `xml:"real"`
Dict []dict `xml:"dict"`
}
type Color struct {
Name string
RFloat float64
GFloat float64
BFloat float64
RInt uint8
GInt uint8
BInt uint8
Hex string
}
func die(y string) {
fmt.Println("error: " + y)
os.Exit(1)
}
func usage() {
fmt.Println(fmt.Sprintf(
"Usage:\n %s myfile.itermcolors\n %s print myfile.itermcolors",
os.Args[0], os.Args[0],
))
os.Exit(1)
}
func convert(filename string) []Color {
contents, err := os.ReadFile(filename)
if err != nil {
die(err.Error())
}
var p plist
err = xml.Unmarshal(contents, &p)
if err != nil {
die(err.Error())
}
var colors []Color
mainDict := p.Dict[0]
for i, colorName := range mainDict.Key {
c := Color{
Name: colorName,
}
c.BFloat = mainDict.Dict[i].Real[0]
c.GFloat = mainDict.Dict[i].Real[1]
c.RFloat = mainDict.Dict[i].Real[2]
c.RInt = uint8(c.RFloat * 255)
c.GInt = uint8(c.GFloat * 255)
c.BInt = uint8(c.BFloat * 255)
c.Hex = fmt.Sprintf("#%02x%02x%02x", c.RInt, c.GInt, c.BInt)
colors = append(colors, c)
}
return colors
}
func main() {
var filename string
var print bool
switch len(os.Args) {
case 2:
filename = os.Args[1]
case 3:
if os.Args[1] != "print" {
usage()
}
filename = os.Args[2]
print = true
default:
usage()
}
colors := convert(filename)
if print {
printColor := func(c Color) {
fmt.Printf(
"%s (%s): \033[48;2;%d;%d;%dm \033[0m\n",
c.Name,
c.Hex,
c.RInt,
c.GInt,
c.BInt,
)
}
for _, c := range colors {
printColor(c)
}
return
}
j, err := json.MarshalIndent(colors, "", " ")
if err != nil {
die(err.Error())
}
fmt.Println(string(j))
}