-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
52 lines (46 loc) · 832 Bytes
/
utils.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
package main
import (
"bytes"
"image"
"image/color"
"image/png"
"math/rand"
)
func randomImage() *bytes.Buffer {
// prepare image
img := image.NewRGBA(image.Rectangle{
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 512, Y: 512},
})
// generate colors
c1 := color.RGBA{
R: uint8(rand.Intn(256)),
G: uint8(rand.Intn(256)),
B: uint8(rand.Intn(256)),
A: 255,
}
c2 := color.RGBA{
R: uint8(rand.Intn(256)),
G: uint8(rand.Intn(256)),
B: uint8(rand.Intn(256)),
A: 255,
}
// draw image
for x := 0; x < 512; x++ {
for y := 0; y < 512; y++ {
switch {
case x < 256 && y < 256 || x > 256 && y > 256:
img.Set(x, y, c1)
default:
img.Set(x, y, c2)
}
}
}
// encode image
var buf bytes.Buffer
err := png.Encode(&buf, img)
if err != nil {
panic(err.Error())
}
return &buf
}