-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
236 lines (199 loc) · 5.38 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"fmt"
"log"
"math"
"net/http"
"os"
"strconv"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Printf("Warning: Error loading .env file: %v", err)
}
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
r := setupRouter()
fmt.Printf("Server starting on port :%s\n", port)
r.Run(":" + port)
}
func setupRouter() *gin.Engine {
r := gin.Default()
r.LoadHTMLGlob("views/*")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
r.GET("/:width/:height", generatePlaceholder)
r.GET("/:width/:height/:color", generatePlaceholder)
return r
}
func generatePlaceholder(c *gin.Context) {
width := c.Param("width")
height := c.Param("height")
color := c.Param("color")
borderRadius := c.DefaultQuery("r", "0")
dot := c.DefaultQuery("d", "false")
grad := c.DefaultQuery("g", "false")
text := c.DefaultQuery("t", "false")
w, err := strconv.Atoi(width)
if err != nil || w <= 0 || w > 3000 {
c.String(http.StatusBadRequest, "Invalid width")
return
}
h, err := strconv.Atoi(height)
if err != nil || h <= 0 || h > 3000 {
c.String(http.StatusBadRequest, "Invalid height")
return
}
radius, err := strconv.Atoi(borderRadius)
if err != nil || radius < 0 || radius > min(w/2, h/2) {
radius = 0
}
if color == "" {
color = "333333"
}
if color[0] == '#' {
color = color[1:]
}
if !(len(color) == 6 || len(color) == 3) {
c.String(http.StatusBadRequest, "Invalid color format")
return
}
if len(color) == 3 {
c2 := color
color = ""
for i := 0; i < len(c2); i++ {
color += string(c2[i]) + string(c2[i]) // Repeat each character
}
}
lighterColor := color
slightlyDarkerForDots := color
dx := 0
if grad == "true" {
lighterColor = getMediumLighterShade(color)
}
if dot == "true" {
slightlyDarkerForDots = getSlightlyDarkerShade(color)
dx = 20
}
textColor := "#ffffff00"
if text == "true" {
textColor = getContrastColor(color)
}
svg := fmt.Sprintf(`<svg width="%d" height="%d" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="mainGrad" x1="0%%" y1="0%%" x2="100%%" y2="100%%">
<stop offset="0%%" style="stop-color:#%s;stop-opacity:1" />
<stop offset="100%%" style="stop-color:#%s;stop-opacity:1" />
</linearGradient>
<pattern id="dots" width="%d" height="%d" patternUnits="userSpaceOnUse">
<circle cx="10" cy="10" r="1.2" fill="#%s" opacity="0.4"/>
</pattern>
<filter id="softShadow">
<feGaussianBlur in="SourceAlpha" stdDeviation="2"/>
<feOffset dx="0" dy="1" result="offsetblur"/>
<feFlood flood-color="#000000" flood-opacity="0.2"/>
<feComposite in2="offsetblur" operator="in"/>
<feMerge>
<feMergeNode/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
<mask id="roundedMask">
<rect width="%d" height="%d" rx="%d" ry="%d" fill="white"/>
</mask>
</defs>
<g mask="url(#roundedMask)">
<rect width="%d" height="%d" fill="url(#mainGrad)"/>
<rect width="%d" height="%d" fill="url(#dots)"/>
</g>
<text x="50%%" y="50%%" text-anchor="middle" dominant-baseline="middle"
font-family="Arial, Helvetica, sans-serif" font-weight="bold" font-size="24"
fill="%s" filter="url(#softShadow)">%dx%d</text>
</svg>`, w, h, w, h, color, lighterColor, dx, dx, slightlyDarkerForDots,
w, h, radius, radius,
w, h, w, h,
textColor, w, h)
c.Header("Content-Type", "image/svg+xml")
if isDev() {
c.Header("Cache-Control", "no-cache")
} else {
c.Header("Cache-Control", "public, max-age=86400")
}
c.String(200, svg)
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
type colorInfo struct {
r, g, b int64
brightness float64
}
func parseColor(hexColor string) colorInfo {
r, _ := strconv.ParseInt(hexColor[0:2], 16, 64)
g, _ := strconv.ParseInt(hexColor[2:4], 16, 64)
b, _ := strconv.ParseInt(hexColor[4:6], 16, 64)
brightness := 0.299*float64(r) + 0.587*float64(g) + 0.114*float64(b)
return colorInfo{
r: r,
g: g,
b: b,
brightness: brightness,
}
}
// for the main gradient
func getMediumLighterShade(hexColor string) string {
info := parseColor(hexColor)
var factor float64
switch {
case info.brightness < 10:
factor = 60.0
case info.brightness < 30:
factor = 3.0
case info.brightness < 60:
factor = 2.0
case info.brightness < 100:
factor = 1.75
case info.brightness < 150:
factor = 1.5
default:
factor = 0.8
}
newR := int(math.Min(float64(info.r)*factor, 255))
newG := int(math.Min(float64(info.g)*factor, 255))
newB := int(math.Min(float64(info.b)*factor, 255))
return fmt.Sprintf("%02x%02x%02x", newR, newG, newB)
}
// for dots - slightly darker than base color
func getSlightlyDarkerShade(hexColor string) string {
info := parseColor(hexColor)
factor := 0.55
if info.brightness < 80 {
factor = 1.8
}
newR := int(float64(info.r) * factor)
newG := int(float64(info.g) * factor)
newB := int(float64(info.b) * factor)
return fmt.Sprintf("%02x%02x%02x", newR, newG, newB)
}
func getContrastColor(hexColor string) string {
r, _ := strconv.ParseInt(hexColor[0:2], 16, 64)
g, _ := strconv.ParseInt(hexColor[2:4], 16, 64)
b, _ := strconv.ParseInt(hexColor[4:6], 16, 64)
luminance := (0.299*float64(r) + 0.587*float64(g) + 0.114*float64(b)) / 255
if luminance > 0.5 {
return "#111111"
}
return "#eeeeee"
}
func isDev() bool {
return os.Getenv("ENVIRONMENT") == "DEV"
}