-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentigo.go
127 lines (107 loc) · 2.56 KB
/
identigo.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
// Package identigo creates a simple, two-color identicon from a provided byteString
package identigo
import (
"crypto/md5"
"encoding/hex"
"image"
"image/color"
"image/draw"
"math"
"strings"
)
type colorValues struct {
red uint8
green uint8
blue uint8
alpha uint8
}
func buildArray(hashString string) []bool {
fill := make([]bool, 32, 64)
for i, digit := range hashString {
if digit > 58 {
fill[i] = true
} else {
fill[i] = false
}
}
return fill
}
// Create function builds the identicon using a standard MD5 hash
// The following foreground and background colors are supported:
//
// black
// navy
// blue
// green
// teal
// lime
// aqua
// maroon
// purple
// olive
// gray
// silver
// red
// fuchsia
// yellow
// white
//
// If an invalid color is specified, the function defaults to a gray icon with green coloring
func Create(bytestring []byte, tilesPerSide int, multiplier int, backcolor string, forecolor string) image.Image {
colors := map[string]colorValues{
"black": {0,0,0,255},
"navy": {0,0,128,255},
"blue": {0,0,128,255},
"green": {0,255,0,255},
"teal": {0,128,128,255},
"lime": {0,255,0,255},
"aqua": {0,255,255,255},
"maroon": {128,0,0,255},
"purple": {128,0,128,255},
"olive": {128,128,0,255},
"gray": {128,128,128,255},
"silver": {192,192,192,255},
"red": {255,0,0,255},
"fuchsia": {255,0,255,255},
"yellow": {255,255,0,255},
"white": {255,255,255,255},
}
// Check to see if colors exists in map
if _, ok := colors[backcolor]; !ok {
backcolor = "gray"
}
if _, ok := colors[forecolor]; !ok {
forecolor = "green"
}
hash := md5.New()
hash.Write(bytestring)
hashString := hex.EncodeToString(hash.Sum([]byte{}))
hashString = strings.ToUpper(hashString)
fill := buildArray(hashString)
m := image.NewRGBA(image.Rect(0, 0, tilesPerSide*multiplier, tilesPerSide*multiplier))
background := color.RGBA{colors[backcolor].red,colors[backcolor].green,colors[backcolor].blue,colors[backcolor].alpha}
draw.Draw(m, m.Bounds(), &image.Uniform{background}, image.ZP, draw.Src)
var col int
var row float64
var x, y int
for i, value := range fill {
if value == true {
col = i / tilesPerSide
row = math.Mod(float64(i), float64(tilesPerSide))
if col != 0 {
col = col * multiplier
}
if row != 0 {
row = row * float64(multiplier)
}
for i := 0; i < multiplier; i++ {
x = col + i
for j := 0; j < multiplier; j++ {
y = int(row) + j
m.Set(int(x), int(y), color.RGBA{colors[forecolor].red, colors[forecolor].green, colors[forecolor].blue, colors[forecolor].alpha})
}
}
}
}
return m
}