-
Notifications
You must be signed in to change notification settings - Fork 0
/
jazzavatar.go
168 lines (136 loc) · 3.5 KB
/
jazzavatar.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
package jazzavatar
import (
"crypto/md5"
"encoding/hex"
"math"
"strconv"
"strings"
m "bitbucket.org/neetsdkasu/mersenne_twister_go"
colorful "github.com/lucasb-eyer/go-colorful"
)
// Jazzavatar Jazzavatar Model
type Jazzavatar struct {
Name string
Size string
Radius string
BgColor string
Shapes []shapeData
colors []string
mersenne *m.MersenneTwister
}
type shapeData struct {
Color string
Tx float64
Ty float64
Rotate float64
Center float64
}
// colorList Color List
var colorList = []string{
"#01888C", // teal
"#FC7500", // bright orange
"#034F5D", // dark teal
"#F73F01", // orangered
"#FC1960", // magenta
"#C7144C", // raspberry
"#F3C100", // goldenrod
"#1598F2", // lightning blue
"#2465E1", // sail blue
"#F19E02", // gold
}
const shapeCount = 4
//Init Init Jazzavatar
func (ja *Jazzavatar) Init(name string, size string, radius string) (*Jazzavatar, error) {
ja.Name = name
ja.Size = size
ja.Radius = radius
ja.colors = colorList
seed, err := ja.nameToSeed(name)
if err != nil {
return nil, err
}
ja.mersenne = m.NewMersenneTwister().Init(uint32(seed))
remainingColors, err := ja.hueShift()
if err != nil {
return nil, err
}
bgColor, remainingColors := ja.genColor(remainingColors)
ja.BgColor = bgColor
for i := 0; i < shapeCount-1; i++ {
shape, newRemainingColors, err := ja.genShape(remainingColors, i)
if err != nil {
return nil, err
}
remainingColors = newRemainingColors
ja.Shapes = append(ja.Shapes, shape)
}
return ja, err
}
func (ja *Jazzavatar) nameToSeed(name string) (int64, error) {
seed, err := ja.hexToNumber(name)
if seed == 0 {
seed, err = ja.stringToNumber(name)
}
return seed, err
}
func (ja *Jazzavatar) hexToNumber(hex string) (int64, error) {
hexString := strings.Replace(hex, "0x", "", 1)
if len(hexString) > 8 {
hexString = hexString[:8]
}
return strconv.ParseInt(hexString, 16, 64)
}
func (ja *Jazzavatar) stringToNumber(str string) (int64, error) {
md5Obj := md5.New()
md5Obj.Write([]byte(str))
md5Str := hex.EncodeToString(md5Obj.Sum(nil))
return ja.hexToNumber(md5Str)
}
func (ja *Jazzavatar) hueShift() ([]string, error) {
amount := ja.mersenne.Real2()*30 - 15
var colors []string
for _, c := range ja.colors {
color, err := colorful.Hex(c)
if err != nil {
return nil, err
}
h, s, v := color.Hsv()
h = h + float64(amount)
newColor := colorful.Hsv(h, s, v)
newC := newColor.Hex()
colors = append(colors, newC)
}
return colors, nil
}
func (ja *Jazzavatar) genColor(colors []string) (string, []string) {
_ = ja.mersenne.Real2()
index := int(math.Floor(float64(len(colors)) * ja.mersenne.Real2()))
genColor := colors[index:][0]
colors = append(colors[:index], colors[index+1:]...)
return genColor, colors
}
func (ja *Jazzavatar) genShape(colors []string, index int) (shapeData, []string, error) {
diameter, err := strconv.ParseFloat(ja.Size, 64)
if err != nil {
return shapeData{}, nil, err
}
center := diameter / 2.0
total := shapeCount - 1
firstRot := ja.mersenne.Real2()
angle := math.Pi * 2.0 * firstRot
velocity := diameter/float64(total)*ja.mersenne.Real2() + (float64(index) * diameter / float64(total))
tx := math.Cos(angle) * velocity
ty := math.Sin(angle) * velocity
secondRot := ja.mersenne.Real2()
rot := firstRot*360.0 + secondRot*180.0
rotate := math.Round(rot*10.0) / 10.0
color, remainingColors := ja.genColor(colors)
shap := shapeData{
Color: color,
Tx: tx,
Ty: ty,
Rotate: rotate,
Center: center,
}
return shap, remainingColors, nil
}