-
Notifications
You must be signed in to change notification settings - Fork 2
/
grid.go
83 lines (71 loc) · 1.49 KB
/
grid.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
// See LICENSE.txt for licensing information.
// Grid generates grid-based icons.
package ricons
import (
"image"
"image/color"
"image/draw"
"math/rand"
"time"
)
type gridIconGen struct {
r *rand.Rand
s int
p []color.Color
b image.Image
}
func (g *gridIconGen) String() string {
return "grid: grid-based icons"
}
func (g *gridIconGen) Generate(i *Icon) error {
r := i.Dim
draw.Draw(i.Image, r, g.b, image.ZP, draw.Src)
gr := make([][]bool, g.s)
el := make([]bool, g.s*g.s)
for i := range gr {
gr[i] = el[i*g.s : (i+1)*g.s]
}
for i := 0; i < 8; i++ {
x := g.r.Intn(g.s)
y := g.r.Intn(g.s)
gr[x][y] = true
if g.r.Float32() > 0.5 {
gr[g.s-x-1][y] = true
}
if g.r.Float32() > 0.5 {
gr[x][g.s-y-1] = true
}
}
c := image.NewUniform(g.p[g.r.Intn(len(g.p))])
w := i.Dim.Max.X / g.s
h := i.Dim.Max.Y / g.s
for y := 0; y < g.s; y++ {
for x := 0; x < g.s; x++ {
if gr[x][y] {
r.Min.X = x * w
r.Min.Y = y * h
r.Max.X = (x + 1) * w
r.Max.Y = (y + 1) * h
draw.Draw(i.Image, r, c, image.ZP, draw.Src)
}
}
}
return nil
}
func (g *gridIconGen) NewIcon(width, height int) (*Icon, error) {
i := NewIcon(width, height)
return i, g.Generate(i)
}
func init() {
g := &gridIconGen{
rand.New(rand.NewSource(time.Now().Unix())),
5,
[]color.Color{
color.RGBA{0xaa, 0x66, 0x66, 0xff},
color.RGBA{0x66, 0xaa, 0x66, 0xff},
color.RGBA{0x66, 0x66, 0xaa, 0xff},
},
image.NewUniform(color.RGBA{0xdd, 0xdd, 0xdd, 0xff}),
}
Register("grid", g)
}