-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvgrad.go
79 lines (68 loc) · 1.26 KB
/
vgrad.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
// See LICENSE.txt for licensing information.
// Vgrad generates simple vertical gradients.
package ricons
import (
"math/rand"
"time"
)
type vgradIconGen struct {
r *rand.Rand
}
func (g *vgradIconGen) String() string {
return "vgrad: simple vertical gradient"
}
func (g *vgradIconGen) Generate(i *Icon) error {
width := i.Dim.Max.X
height := i.Dim.Max.Y
re := g.r.Intn(255)
sr := re / height
if sr == 0 {
sr = 1
}
if re > 128 {
sr = -sr
}
if sr == 0 {
sr = 1
}
gr := g.r.Intn(255)
sg := gr / height
if sg == 0 {
sg = 1
}
if gr > 128 {
sg = -sg
}
bl := g.r.Intn(255)
sb := bl / height
if bl > 128 {
sb = -sb
}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
idx := (y-i.Image.Rect.Min.Y)*i.Image.Stride + (x-i.Image.Rect.Min.X)*4
i.Image.Pix[idx] = uint8(re)
i.Image.Pix[idx+1] = uint8(gr)
i.Image.Pix[idx+2] = uint8(bl)
i.Image.Pix[idx+3] = 255
}
if re > 0 && re < 255 {
re += sr
}
if gr > 0 && gr < 255 {
gr += sg
}
if bl > 0 && bl < 255 {
bl += sb
}
}
return nil
}
func (g *vgradIconGen) NewIcon(width, height int) (*Icon, error) {
i := NewIcon(width, height)
return i, g.Generate(i)
}
func init() {
g := &vgradIconGen{rand.New(rand.NewSource(time.Now().Unix()))}
Register("vgrad", g)
}