forked from JustaPenguin/govatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
govatar.go
188 lines (145 loc) · 3.8 KB
/
govatar.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package govatar
import (
"github.com/disintegration/imaging"
"golang.org/x/crypto/scrypt"
"image"
"image/color"
"image/draw"
"math/rand"
)
var (
blockX int
blockY int
rows int
columns int
numBlocks int
keyLength int
)
type block struct{}
// Create the avatar and return as image.Image
func CreateAvatar(canvasWidth, canvasHeight, blockWidth, blockHeight int, vibrance uint8, userName, salt string) (image.Image, error) {
var shuffleInt int64
shuffleInt = 1
// number of rows/columns
rows = (canvasWidth / 2) / blockWidth
columns = canvasHeight / blockHeight
// number of blocks
numBlocks = rows * columns
keyLength = numBlocks
var (
hash []uint8
colors []uint8
err error
)
// Generate hash using Scrypt
hash, err = Hash(userName, salt)
if err != nil {
return nil, err
}
// Create the avatar image
r := image.Rect(0, 0, canvasWidth, canvasHeight)
img := image.NewRGBA(r)
// Create and draw a grey block as background color
grayBlock := color.RGBA{R: 210, G: 210, B: 210, A: 255}
draw.Draw(img, img.Bounds(), &image.Uniform{C: grayBlock}, image.ZP, draw.Src)
// Create 32 blocks to fill half of avatar canvas
blocks := make([]block, numBlocks)
// For each block decide if to draw anything and what color to draw based on hash
for i := range blocks {
var gC uint8
var rC uint8
var bC uint8
var aC uint8
var skip bool
num0 := hash[i] % 10
num1 := (hash[i] / 10) % 10
num2 := (hash[i] / 100) % 10
if i == 0 {
colors = make([]uint8, 3)
}
if num2 == 0 {
// If byte is < 100 then display nothing
skip = true
// Use this data to seed the RBG shuffle rand value
if num1*num0 != 0 {
shuffleInt *= int64(num1 * num0)
} else {
shuffleInt += int64(num1 + num0)
}
} else {
skip = false
aC = 255
colors = []uint8{
rC,
gC,
bC,
}
// Generate RGB values
colors[0] = ((num0 * vibrance) * num1) + num2
colors[1] = ((num1 * vibrance) * num2) + num0
colors[2] = ((num2 * vibrance) * num0) + num1
// Shuffle the colors based on rand
Shuffle(colors, shuffleInt)
}
// Start position of block
if i == 0 {
blockX = 0
blockY = 0
} else {
// New row
if i%rows == 0 {
blockX = 0
blockY += blockHeight
// New column
} else {
blockX += blockWidth
}
}
if skip {
continue
}
// If colors have somehow overstepped bounds reset them
for i := range colors {
if colors[i] > 255 {
colors[i] = 255
}
if colors[i] < 0 {
colors[i] = 0
}
}
// Create a color based on hash in random order
colorBlock := color.RGBA{R: colors[0], G: colors[1], B: colors[2], A: aC}
// Print area is size of one block starting from position of block
spMin := image.Point{X: blockX, Y: blockY}
spMax := image.Point{X: blockX + blockWidth, Y: blockY + blockHeight}
// Create a rectangle the size of the block
blockRectangle := image.Rectangle{Min: spMin, Max: spMax}
// Draw the block to the image
draw.Draw(img, blockRectangle, &image.Uniform{C: colorBlock}, image.Point{X: 0, Y: 0}, draw.Src)
}
// Create a horizontal mirror of the generated image
imgHFlip := imaging.FlipH(img)
// Draw to the right side of the canvas
r2 := image.Rect(canvasWidth, canvasHeight, canvasWidth/2, 0)
draw.Draw(img, r2, imgHFlip, image.Point{X: canvasWidth / 2, Y: 0}, draw.Src)
return img, nil
}
// Generates a hashed byte array based on username and salt
func Hash(userName, salt string) ([]byte, error) {
var (
hash []byte
err error
)
hash, err = scrypt.Key([]byte(userName), []byte(salt), 16384, 8, 1, keyLength)
if err != nil {
return nil, err
}
return hash, err
}
// Shuffles a uint8 array
func Shuffle(vals []uint8, colorInt int64) {
r := rand.New(rand.NewSource(colorInt))
r.Shuffle(len(vals), func(i, j int) {
vals[i], vals[j] = vals[j], vals[i]
})
}