-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (98 loc) · 2.16 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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
type ColorStop struct {
Position float64
Color string
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: program <pattern>")
os.Exit(1)
}
pattern := os.Args[1]
files, err := filepath.Glob(pattern)
if err != nil {
fmt.Printf("Error finding files: %v\n", err)
os.Exit(1)
}
gradients := make(map[string][]ColorStop)
for _, file := range files {
name := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
stops, err := processFile(file)
if err != nil {
fmt.Printf("Error processing %s: %v\n", file, err)
continue
}
gradients[name] = stops
}
outputGradients(gradients)
}
func processFile(filename string) ([]ColorStop, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var stops []ColorStop
rgbRegex := regexp.MustCompile(`rgb\(\s*(\d+),\s*(\d+),\s*(\d+)\)\s*([\d.]+)%`)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
matches := rgbRegex.FindStringSubmatch(line)
if matches != nil {
r := matches[1]
g := matches[2]
b := matches[3]
position := matches[4]
var pos float64
fmt.Sscanf(position, "%f", &pos)
pos = pos / 100.0 // Convert percentage to decimal
color := fmt.Sprintf("#%02x%02x%02x",
parseIntOrZero(r),
parseIntOrZero(g),
parseIntOrZero(b))
stops = append(stops, ColorStop{
Position: pos,
Color: color,
})
}
}
return stops, scanner.Err()
}
func parseIntOrZero(s string) int {
val := 0
fmt.Sscanf(s, "%d", &val)
return val
}
func outputGradients(gradients map[string][]ColorStop) {
fmt.Println("const gradientTypes: Record<string, ColorStop[]> = {")
first := true
for name, stops := range gradients {
if !first {
fmt.Println(",")
}
first = false
fmt.Printf(" [\"%s\"]: [", name)
for i, stop := range stops {
if i > 0 {
fmt.Print(",")
}
if i%2 == 0 {
fmt.Print("\n ")
} else {
fmt.Print(" ")
}
fmt.Printf("{ position: %.6f, color: \"%s\" }",
stop.Position, stop.Color)
}
fmt.Print("\n ]")
}
fmt.Println("\n};")
}