avatar.go (view raw)
1//
2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
3//
4// Permission to use, copy, modify, and distribute this software for any
5// purpose with or without fee is hereby granted, provided that the above
6// copyright notice and this permission notice appear in all copies.
7//
8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19 "bytes"
20 "crypto/sha512"
21 "image"
22 "image/png"
23)
24
25func avatar(name string) []byte {
26 h := sha512.New()
27 h.Write([]byte(name))
28 s := h.Sum(nil)
29 img := image.NewNRGBA(image.Rect(0, 0, 64, 64))
30 for i := 0; i < 64; i++ {
31 for j := 0; j < 64; j++ {
32 p := i*img.Stride + j*4
33 xx := i/16*16 + j/16
34 x := s[xx]
35 if x < 64 {
36 img.Pix[p+0] = 16
37 img.Pix[p+1] = 0
38 img.Pix[p+2] = 48
39 img.Pix[p+3] = 255
40 } else if x < 128 {
41 img.Pix[p+0] = 48
42 img.Pix[p+1] = 0
43 img.Pix[p+2] = 96
44 img.Pix[p+3] = 255
45 } else if x < 192 {
46 img.Pix[p+0] = 72
47 img.Pix[p+1] = 0
48 img.Pix[p+2] = 144
49 img.Pix[p+3] = 255
50 } else {
51 img.Pix[p+0] = 96
52 img.Pix[p+1] = 0
53 img.Pix[p+2] = 192
54 img.Pix[p+3] = 255
55 }
56 }
57 }
58 var buf bytes.Buffer
59 png.Encode(&buf, img)
60 return buf.Bytes()
61}