avatar.go (view raw)
1package main
2
3import (
4 "bytes"
5 "crypto/sha512"
6 "image"
7 "image/png"
8)
9
10func avatar(name string) []byte {
11 h := sha512.New()
12 h.Write([]byte(name))
13 s := h.Sum(nil)
14 img := image.NewNRGBA(image.Rect(0, 0, 64, 64))
15 for i := 0; i < 64; i++ {
16 for j := 0; j < 64; j++ {
17 p := i*img.Stride + j*4
18 xx := i/16*16 + j/16
19 x := s[xx]
20 if x < 64 {
21 img.Pix[p+0] = 32
22 img.Pix[p+1] = 0
23 img.Pix[p+2] = 64
24 img.Pix[p+3] = 255
25 } else if x < 128 {
26 img.Pix[p+0] = 32
27 img.Pix[p+1] = 0
28 img.Pix[p+2] = 92
29 img.Pix[p+3] = 255
30 } else if x < 192 {
31 img.Pix[p+0] = 64
32 img.Pix[p+1] = 0
33 img.Pix[p+2] = 128
34 img.Pix[p+3] = 255
35 } else {
36 img.Pix[p+0] = 96
37 img.Pix[p+1] = 0
38 img.Pix[p+2] = 160
39 img.Pix[p+3] = 255
40 }
41 }
42 }
43 var buf bytes.Buffer
44 png.Encode(&buf, img)
45 return buf.Bytes()
46}