-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphics.go
174 lines (138 loc) · 4.82 KB
/
graphics.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
163
164
165
166
167
168
169
170
171
172
173
174
// logo generation for PWA needs
package main
import (
"bufio"
"fmt"
"github.com/PerformLine/go-stockutil/colorutil"
ico "github.com/biessek/golang-ico"
"github.com/disintegration/imaging"
log "github.com/sirupsen/logrus"
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
"image"
"image/draw"
"image/png"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// getContrastColor takes a color in hexadecimal format (e.g. "#aabbcc") and
// returns a contrasting color in RGB hexadecimal format.
func getContrastColor(inputColor string) string {
// Parse the input color
baseColor, err := colorutil.Parse(inputColor)
if err != nil {
// Handle parsing error
log.Fatal("Error parsing input color:", err)
return ""
}
// Convert to HSL
h, s, l := baseColor.HSL()
// Adjust lightness based on the condition
if l > 0.5 {
l = 0.06
} else {
l = 0.94
}
// Convert back to RGB
r, g, b := colorutil.HslToRgb(h, s, l)
adjustedColor := fmt.Sprintf("#%02x%02x%02x", r, g, b)
return adjustedColor
}
func openSvgToString(svgPath string) (string, error) {
// Open SVG file
svgFile, err := os.Open(svgPath)
if err != nil {
return "", err
}
defer svgFile.Close()
// Read the SVG content
scanner := bufio.NewScanner(svgFile)
var svgContent string
for scanner.Scan() {
svgContent += scanner.Text() + "\n"
}
return svgContent, nil
}
func convertSvgToFavicons(svgPath string) error {
svgContent, _ := openSvgToString(svgPath)
// Replace 'fill="#e0f3ff"' with the value from config.Customization.Colors.Contrast
themeColor := config.Customization.Colors.Theme
contrastColor := getContrastColor(themeColor)
svgContent = strings.ReplaceAll(string(svgContent), `fill="#e0f3ff"`, fmt.Sprintf(`fill="%s"`, contrastColor))
// Save the modified SVG content to a new file at 'dist/img/icons/safari-pinned-tab.svg'
err := ioutil.WriteFile(pwaIconsFolderPath+"/safari-pinned-tab.svg", []byte(svgContent), 0644)
if err != nil {
return fmt.Errorf("error saving modified SVG: %v", err)
}
// Replace '<path' with '<rect width="100%" height="100%" fill="red"/>\n<path'
svgContent = strings.ReplaceAll(svgContent, `<path`, `<rect width="100%" height="100%" rx="80" ry="80" fill="`+themeColor+`"/>\n<path`)
// Save the modified SVG content to a new file at 'dist/img/favicon.svg'
err = ioutil.WriteFile(pwaIconsFolderPath+"/favicon.svg", []byte(svgContent), 0644)
if err != nil {
return fmt.Errorf("error saving modified SVG: %v", err)
}
img := svgToRaster(svgContent, 32)
file, err := os.Create(faviconPath)
if err != nil {
fmt.Println("Error creating file:", err)
return err
}
defer file.Close() // Close the file when done
ico.Encode(file, img)
return nil
}
func convertSvgToPng(svgPath string, pngPath string, sideLength int) error {
svgContent, _ := openSvgToString(svgPath)
// Replace color occurrences
replacedContent := strings.Replace(svgContent, "#e0f3ff", getContrastColor(config.Customization.Colors.Theme), -1)
img := svgToRaster(replacedContent, sideLength)
// Resize the image to add padding (15% of side length)
resizedImg := imaging.Resize(img, sideLength*70/100, sideLength*70/100, imaging.Lanczos)
// Set the background color
canvas := image.NewRGBA(image.Rect(0, 0, sideLength, sideLength))
bgColor := colorutil.MustParse(config.Customization.Colors.Theme)
draw.Draw(canvas, canvas.Bounds(), &image.Uniform{bgColor}, image.Point{}, draw.Src)
// Center the image on a new square canvas (with the original side length)
square := imaging.PasteCenter(canvas, resizedImg)
// Create the output file
outputFile, err := os.Create(pngPath)
if err != nil {
return err
}
defer outputFile.Close()
// Encode the image to PNG
return png.Encode(outputFile, square)
}
func svgToRaster(svgContent string, sideLength int) image.Image {
// Decode SVG
icon, _ := oksvg.ReadIconStream(strings.NewReader(svgContent))
icon.SetTarget(0, 0, float64(sideLength), float64(sideLength))
// Create a new RGBA image
img := image.NewRGBA(image.Rect(0, 0, sideLength, sideLength))
// Set the background color
bgColor := colorutil.MustParse(config.Customization.Colors.Theme)
draw.Draw(img, img.Bounds(), &image.Uniform{bgColor}, image.Point{}, draw.Src)
// draw icon
icon.Draw(rasterx.NewDasher(sideLength, sideLength, rasterx.NewScannerGV(sideLength, sideLength, img, img.Bounds())), 1)
return img
}
func listLogoFiles() ([]string, error) {
var logoFiles []string
// Walk through the directory
err := filepath.Walk(pwaIconsFolderPath, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Check if the file is a regular file and has a .png or .svg extension
if !info.IsDir() && (strings.HasSuffix(info.Name(), ".png") || strings.HasSuffix(info.Name(), ".svg")) {
logoFiles = append(logoFiles, filePath)
}
return nil
})
if err != nil {
return nil, err
}
return logoFiles, nil
}