-
Notifications
You must be signed in to change notification settings - Fork 1
/
avatars.go
92 lines (77 loc) · 1.8 KB
/
avatars.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
package avatars
import (
"bytes"
"html"
"image"
"image/png"
"github.com/lafriks/go-avatars/bottts"
"github.com/lafriks/go-avatars/rnd"
"github.com/fogleman/gg"
"github.com/lafriks/go-svg"
"github.com/lafriks/go-svg/renderer"
rendr_gg "github.com/lafriks/go-svg/renderer/gg"
)
// Avatar that was generated using specified seed.
type Avatar struct {
r *rnd.Random
name string
svg []byte
}
// Generate an avatar using the given name as seed.
func Generate(name string, opts ...Option) (*Avatar, error) {
r := rnd.NewRandom(name)
attrs, body := bottts.Generate(r)
res := bytes.Buffer{}
res.WriteString("<svg")
for name, val := range attrs {
res.WriteByte(' ')
res.WriteString(name)
res.WriteString(`="`)
res.WriteString(html.EscapeString(val))
res.WriteByte('"')
}
res.WriteString(">\n")
res.WriteString(body)
res.WriteString("\n</svg>")
return &Avatar{
name: name,
r: r,
svg: res.Bytes(),
}, nil
}
// Name of the avatar.
func (a *Avatar) Name() string {
return a.name
}
// SVG content of the avatar.
func (a *Avatar) SVG() []byte {
return a.svg
}
// Image rendered from the SVG.
func (a *Avatar) Image(opts ...RenderOption) (image.Image, error) {
s, err := svg.Parse(bytes.NewReader(a.svg), svg.IgnoreErrorMode)
if err != nil {
return nil, err
}
opt := renderOptions(opts...)
gc := gg.NewContext(opt.Size, opt.Size)
rendr_gg.Draw(gc, s, renderer.Target(
float64(opt.Padding),
float64(opt.Padding),
float64(opt.Size-opt.Padding*2),
float64(opt.Size-opt.Padding*2),
))
return gc.Image(), nil
}
// PNG encoded image.
func (a *Avatar) PNG(opts ...RenderOption) ([]byte, error) {
img, err := a.Image(opts...)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
if err := png.Encode(buf, img); err != nil {
return nil, err
}
return buf.Bytes(), nil
}