-
Notifications
You must be signed in to change notification settings - Fork 1
/
identicon.go
93 lines (80 loc) · 2.07 KB
/
identicon.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
93
// Package stellar_identicon provides a way to generate identicons for Stellar accounts.
package identicon
import (
"encoding/base32"
"errors"
"github.com/stellar/go/strkey"
"image"
"image/color"
"math"
)
const (
// Default width of resulting identicon image in pixels
Width = 210
// Default height of resulting identicon image in pixels
Height = 210
)
const (
columns = 7
rows = 7
)
// This exception will be thrown when an invalid address is used
var InvalidEd25519PublicKeyError = errors.New("invalid ed25519 public key")
// Generates an identicon for the given stellar address
func Generate(publicKey string, width, height int) (*image.RGBA, error) {
if !strkey.IsValidEd25519PublicKey(publicKey) {
return nil, InvalidEd25519PublicKeyError
}
keyData, err := base32.StdEncoding.DecodeString(publicKey)
if err != nil {
return nil, err
}
keyData = keyData[2:]
var matrix [columns][rows]bool
columnsForCalculation := int(math.Ceil(columns / 2.0))
for column := 0; column < columnsForCalculation; column++ {
for row := 0; row < rows; row++ {
if getBit(column+row*columnsForCalculation, keyData[1:]) {
matrix[row][column] = true
matrix[row][columns-column-1] = true
}
}
}
img := image.NewRGBA(image.Rect(0, 0, width, height))
foreground := getColor(keyData)
blockWidth := width / columns
blockHeight := height / rows
for row, rowColumns := range matrix {
for column, cell := range rowColumns {
if cell {
x0 := column * blockWidth
y0 := row * blockHeight
x1 := (column+1)*blockWidth - 1
y1 := (row+1)*blockHeight - 1
for x := x0; x <= x1; x++ {
for y := y0; y <= y1; y++ {
img.Set(x, y, foreground)
}
}
}
}
}
return img, nil
}
func getColor(data []byte) color.RGBA {
hsv := HSV{
H: float64(data[0]) / 255,
S: 0.7,
V: 0.8,
}
rgb := hsv.RGB()
return color.RGBA{
R: uint8(math.Round(rgb.R * 255)), G: uint8(math.Round(rgb.G * 255)), B: uint8(math.Round(rgb.B * 255)), A: 255,
}
}
func getBit(n int, keyData []byte) bool {
if keyData[n/8]>>(8-((n%8)+1))&1 == 1 {
return true
}
return false
}