-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (143 loc) · 3.37 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"github.com/beevik/etree"
)
func main() {
for _, dir := range []string{
"apps/scalable",
"devices/scalable",
"mimetypes/scalable",
"places/16",
"places/48",
"preferences/scalable",
} {
for _, file := range readFiles(fmt.Sprintf("candy-icons/%v", dir)) {
outDir := fmt.Sprintf("out/nord-icons/%v", dir)
doc := createXmlDoc(file.Second)
var writer *bufio.Writer
if err := os.MkdirAll(outDir, os.FileMode(0755)); err != nil {
log.Fatal(err)
}
if outFile, err := os.Create(fmt.Sprintf("%v/%v", outDir, file.First)); err != nil {
log.Fatal(err)
} else {
writer = bufio.NewWriter(outFile)
}
if _, err := doc.WriteTo(writer); err != nil {
log.Fatal(err)
}
if err := writer.Flush(); err != nil {
log.Fatal(err)
}
}
}
}
type Pair[F, S any] struct {
First F
Second S
}
func createXmlDoc(path string) *etree.Document {
log.Println("Process file:", path)
doc := etree.NewDocument()
if err := doc.ReadFromFile(path); err != nil {
log.Fatal(err)
}
root := doc.SelectElement("svg")
var gradients []*etree.Element
for _, defs := range root.FindElements("defs") {
if hasStopElements(*defs) {
gradients = append(gradients, defs.FindElements("linearGradient")...)
}
}
for _, g := range root.FindElements("g") {
if hasStopElements(*g) {
gradients = append(gradients, g.FindElements("linearGradient")...)
}
}
gradients = append(gradients, root.FindElements("linearGradient")...)
for _, gradient := range gradients {
p := randomPalette()
var colors []string
for _, s := range gradient.FindElements("stop") {
var color string
var style *etree.Attr
switch {
case s.SelectAttr("style") != nil:
style = s.SelectAttr("style")
case s.SelectAttr("stop-color") != nil:
style = s.SelectAttr("stop-color")
}
if style != nil {
for {
if len(p.colors()) == len(colors) {
color = p.randomColor()
colors = []string{}
colors = append(colors, color)
break
} else if contains(colors, color) {
continue
} else {
color = p.randomColor()
colors = append(colors, color)
break
}
}
style.Value = fmt.Sprintf("stop-color:%v", color)
}
}
}
return doc
}
func hasStopElements(e etree.Element) bool {
for _, lg := range e.FindElements("linearGradient") {
if len(lg.FindElements("stop")) > 0 {
return true
}
}
return false
}
func contains(s []string, ele string) bool {
sort.Strings(s)
i := sort.SearchStrings(s, ele)
return i < len(s) && s[i] == ele
}
func readFiles(in string) []Pair[string, string] {
f, err := os.Open(in)
if err != nil {
fmt.Println(err)
panic("Error while opening input dir")
}
files, err := f.ReadDir(0)
if err != nil {
fmt.Println(err)
panic("Error while reading input dir")
}
path, err := filepath.Abs(in)
if err != nil {
fmt.Println(err)
panic("Error getting absolute path")
}
// out := make(map[os.DirEntry]string)
var out []Pair[string, string]
for _, file := range files {
if extension(file.Name()) == "svg" && file.Type().IsRegular() {
p := Pair[string, string]{
First: file.Name(),
Second: filepath.Join(path, file.Name()),
}
out = append(out, p)
}
}
return out
}
func extension(fileName string) string {
split := strings.Split(fileName, ".")
return split[len(split)-1]
}