-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags.go
More file actions
285 lines (264 loc) · 7.88 KB
/
Copy pathflags.go
File metadata and controls
285 lines (264 loc) · 7.88 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"image"
"image/color"
"math"
"math/rand"
"github.com/hajimehoshi/ebiten/v2"
)
// Pre-rendered flag resolution (3:2 aspect). Flags are drawn scaled to any
// on-screen size, so a moderately high resolution keeps edges clean.
const (
flagW = 60
flagH = 40
)
// flagPalette holds strong, saturated colours used for flag accents.
var flagPalette = []color.RGBA{
{198, 30, 40, 255}, // red
{24, 62, 132, 255}, // blue
{24, 116, 62, 255}, // green
{255, 205, 0, 255}, // gold
{255, 255, 255, 255}, // white
{32, 32, 36, 255}, // black
{255, 138, 0, 255}, // orange
{122, 42, 140, 255}, // purple
{0, 138, 158, 255}, // teal
{178, 58, 12, 255}, // rust
}
// flagDesign describes one procedurally generated national flag: a base
// colour plus a random arrangement of bands, crosses, stars or a shield.
// colors[0] is always the base (background) colour.
type flagDesign struct {
style string
base color.RGBA
colors []color.RGBA
vertical bool // tricolor orientation
diagonal bool // saltire (X) instead of an upright cross
stars int // star count (star style)
points int // star point count (4..8)
rot float64 // star rotation in radians
shield bool // shield style
}
func luminance(c color.RGBA) float64 {
return 0.299*float64(c.R) + 0.587*float64(c.G) + 0.114*float64(c.B)
}
func shade(c color.RGBA, f float64) color.RGBA {
return color.RGBA{uint8(float64(c.R) * f), uint8(float64(c.G) * f), uint8(float64(c.B) * f), 255}
}
// pickContrast returns up to n palette colours whose luminance differs from
// base by at least 70, shuffled; black/white are used when nothing contrasts.
func pickContrast(rng *rand.Rand, base color.RGBA, n int) []color.RGBA {
baseL := luminance(base)
var candidates []color.RGBA
for _, c := range flagPalette {
if math.Abs(luminance(c)-baseL) >= 70 {
candidates = append(candidates, c)
}
}
if len(candidates) == 0 {
if baseL > 128 {
return []color.RGBA{{32, 32, 36, 255}}
}
return []color.RGBA{{255, 255, 255, 255}}
}
rng.Shuffle(len(candidates), func(i, j int) { candidates[i], candidates[j] = candidates[j], candidates[i] })
if n > len(candidates) {
n = len(candidates)
}
return candidates[:n]
}
// genFlagDesign procedurally builds a national flag from a random style:
// tricolour (2-3 horizontal or vertical bands), a centred or Scandinavian
// cross, a diagonal cross (saltire), one to three stars with 4-8 points, a
// shield (coat of arms) with a charge, or a plain field with a hoist stripe.
// The country's colour is the base so every flag is visibly "theirs".
func genFlagDesign(rng *rand.Rand, primary color.RGBA) flagDesign {
base := shade(primary, 0.8)
styles := []string{"tricolor", "tricolor", "tricolor", "cross", "scandi",
"saltire", "star", "shield", "plain"}
d := flagDesign{style: styles[rng.Intn(len(styles))], base: base, colors: []color.RGBA{base}}
acc := pickContrast(rng, base, 6)
next := func() color.RGBA {
// skip accents too similar to colours already used, so e.g. a
// tricolour's two bands never look alike
for len(acc) > 0 {
c := acc[0]
acc = acc[1:]
ok := true
for _, used := range d.colors {
if math.Abs(luminance(c)-luminance(used)) < 45 {
ok = false
break
}
}
if ok {
return c
}
}
if luminance(base) > 128 {
return color.RGBA{32, 32, 36, 255}
}
return color.RGBA{255, 255, 255, 255}
}
switch d.style {
case "tricolor":
n := 2 + rng.Intn(2)
d.vertical = rng.Intn(2) == 0
for i := 1; i < n; i++ {
d.colors = append(d.colors, next())
}
case "cross", "scandi", "saltire":
d.diagonal = d.style == "saltire"
d.colors = append(d.colors, next())
case "star":
d.stars = 1 + rng.Intn(3)
d.points = 4 + rng.Intn(5)
d.rot = rng.Float64() * math.Pi
d.colors = append(d.colors, next())
case "shield":
d.shield = true
body := next()
d.colors = append(d.colors, body)
// the charge is a star with a random point count and rotation
d.points = 4 + rng.Intn(5)
d.rot = rng.Float64() * math.Pi
charge := pickContrast(rng, body, 1)
d.colors = append(d.colors, charge[0])
case "plain":
d.colors = append(d.colors, next())
}
return d
}
// --- rasterizer (pure stdlib, testable without ebiten) ------------------------
func fillRect(img *image.RGBA, x0, y0, x1, y1 int, c color.RGBA) {
for y := y0; y < y1; y++ {
for x := x0; x < x1; x++ {
img.SetRGBA(x, y, c)
}
}
}
// fillPoly paints a polygon (even-odd rule) given normalized 0..1 coords.
func fillPoly(img *image.RGBA, pts [][2]float64, c color.RGBA) {
b := img.Bounds()
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
if pointInPoly(float64(x)+0.5, float64(y)+0.5, pts) {
img.SetRGBA(x, y, c)
}
}
}
}
func pointInPoly(x, y float64, pts [][2]float64) bool {
inside := false
for i, j := 0, len(pts)-1; i < len(pts); j, i = i, i+1 {
xi, yi := pts[i][0], pts[i][1]
xj, yj := pts[j][0], pts[j][1]
if (yi > y) != (yj > y) && x < (xj-xi)*(y-yi)/(yj-yi)+xi {
inside = !inside
}
}
return inside
}
// fillStar paints a star with the given point count: outer radius r, inner
// radius 0.42*r, rotated by rot.
func fillStar(img *image.RGBA, cx, cy, r float64, points int, rot float64, c color.RGBA) {
pts := make([][2]float64, 0, points*2)
for i := 0; i < points*2; i++ {
rad := r
if i%2 == 1 {
rad = r * 0.42
}
ang := rot + float64(i)*math.Pi/float64(points)
pts = append(pts, [2]float64{cx + rad*math.Cos(ang), cy + rad*math.Sin(ang)})
}
fillPoly(img, pts, c)
}
// drawFlagDesign paints a design into a plain image.RGBA.
func drawFlagDesign(d flagDesign) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, flagW, flagH))
fillRect(img, 0, 0, flagW, flagH, d.base)
switch d.style {
case "tricolor":
n := len(d.colors)
if d.vertical {
for i, c := range d.colors {
x0 := i * flagW / n
x1 := (i + 1) * flagW / n
if i == n-1 {
x1 = flagW
}
fillRect(img, x0, 0, x1, flagH, c)
}
} else {
for i, c := range d.colors {
y0 := i * flagH / n
y1 := (i + 1) * flagH / n
if i == n-1 {
y1 = flagH
}
fillRect(img, 0, y0, flagW, y1, c)
}
}
case "cross":
c := d.colors[1]
fillRect(img, flagW*2/5, 0, flagW*2/5+flagW/5, flagH, c)
fillRect(img, 0, flagH*2/5, flagW, flagH*2/5+flagH/5, c)
case "scandi":
c := d.colors[1]
fillRect(img, flagW/4, 0, flagW/4+flagW/6, flagH, c)
fillRect(img, 0, flagH*2/5, flagW, flagH*2/5+flagH/5, c)
case "saltire":
c := d.colors[1]
th := float64(flagW) / 5
for y := 0; y < flagH; y++ {
for x := 0; x < flagW; x++ {
d1 := math.Abs(float64(y)*flagW-float64(x)*flagH) / math.Hypot(flagW, flagH)
d2 := math.Abs(float64(y)*flagW-float64(flagW-x)*flagH) / math.Hypot(flagW, flagH)
if d1 <= th/2 || d2 <= th/2 {
img.SetRGBA(x, y, c)
}
}
}
case "star":
c := d.colors[1]
var cx []float64
switch d.stars {
case 1:
cx = []float64{0.5}
case 2:
cx = []float64{0.33, 0.67}
default:
cx = []float64{0.25, 0.5, 0.75}
}
r := float64(flagH) * 0.3
if d.stars > 1 {
r = float64(flagH) * 0.24
}
for i := 0; i < d.stars; i++ {
fillStar(img, cx[i]*flagW, flagH/2, r, d.points, d.rot, c)
}
case "shield":
body := d.colors[1]
fillShield(img, body)
fillStar(img, flagW/2, flagH*0.58, float64(flagH)*0.22, d.points, d.rot, d.colors[2])
case "plain":
fillRect(img, 0, 0, flagW/8, flagH, d.colors[1])
}
return img
}
// fillShield paints a classic heraldic shield shape.
func fillShield(img *image.RGBA, c color.RGBA) {
pts := [][2]float64{
{0.28, 0.14}, {0.72, 0.14}, {0.86, 0.34},
{0.62, 0.85}, {0.5, 0.93}, {0.38, 0.85}, {0.14, 0.34},
}
for i := range pts {
pts[i] = [2]float64{pts[i][0] * flagW, pts[i][1] * flagH}
}
fillPoly(img, pts, c)
}
// makeFlagImage renders the country's flag as an ebiten.Image.
func makeFlagImage(rng *rand.Rand, primary color.RGBA) *ebiten.Image {
d := genFlagDesign(rng, primary)
return ebiten.NewImageFromImage(drawFlagDesign(d))
}