-
Notifications
You must be signed in to change notification settings - Fork 1
/
avatar.go
520 lines (471 loc) · 13.6 KB
/
avatar.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package avatar
import (
"encoding/base64"
"fmt"
"math/rand"
"os"
"strings"
"time"
"unicode/utf8"
"github.com/google/uuid"
)
// model
type Avatar struct {
_id string
_type AvatarType
shape string
palette string
// text
name string
// sticker
sticker string
// character
skinColor string
hairColor string
facialHair string
hair string
dress string
eye string
eyebrow string
mouth string
glass string
accessory string
}
func (avatar *Avatar) init() {
avatar._id = uuid.NewString()
avatar.RandomizeShape("square")
avatar.RandomizePalette(Default)
if avatar._type == MALE || avatar._type == FEMALE {
avatar.RandomizeSkinColor()
avatar.RandomizeHair()
avatar.RandomizeFacialHair()
avatar.RandomizeDress()
avatar.RandomizeEye()
avatar.RandomizeEyebrow()
avatar.RandomizeMouth()
avatar.RandomizeGlass()
avatar.RandomizeAccessory()
}
}
func (Avatar) randTo(length int) int {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
return rnd.Intn(length)
}
func (avatar Avatar) id(key string) string {
return key + "-" + avatar._id
}
// InlineSVG generate svg content for embeded (html svg tag) element
func (avatar Avatar) InlineSVG() string {
data := make([]string, 0)
data = append(data, `<svg viewBox="0 0 128 128" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">`)
data = append(data, `<desc>Created with https://github.com/gomig/avatar</desc>`)
// generate color definitions
skinColors := resolveSkinColor(avatar.skinColor)
hairColors := resolveHairColor(avatar.hairColor)
palette := resolvePalette(avatar.palette)
data = append(data, "<defs>")
skinColor := skinColors.skin.Color
if skinColors.skin.Mode == Definition {
_id := avatar.id("skin-color")
data = append(data, strings.ReplaceAll(skinColors.skin.Color, "{id}", _id))
skinColor = fmt.Sprintf("url(#%s)", _id)
}
skinShadow := skinColors.shadow.Color
if skinColors.shadow.Mode == Definition {
_id := avatar.id("skin-shadow")
data = append(data, strings.ReplaceAll(skinColors.shadow.Color, "{id}", _id))
skinShadow = fmt.Sprintf("url(#%s)", _id)
}
hairColor := hairColors.base.Color
if hairColors.base.Mode == Definition {
_id := avatar.id("hair-color")
data = append(data, strings.ReplaceAll(hairColors.base.Color, "{id}", _id))
hairColor = fmt.Sprintf("url(#%s)", _id)
}
hairShadow := hairColors.shadow.Color
if hairColors.shadow.Mode == Definition {
_id := avatar.id("hair-shadow")
data = append(data, strings.ReplaceAll(hairColors.shadow.Color, "{id}", _id))
hairShadow = fmt.Sprintf("url(#%s)", _id)
}
hairHighlight := hairColors.highlight.Color
if hairColors.highlight.Mode == Definition {
_id := avatar.id("hair-highlight")
data = append(data, strings.ReplaceAll(hairColors.highlight.Color, "{id}", _id))
hairHighlight = fmt.Sprintf("url(#%s)", _id)
}
shapeColor := palette.shape.Color
if palette.shape.Mode == Definition {
_id := avatar.id("shape-color")
data = append(data, strings.ReplaceAll(palette.shape.Color, "{id}", _id))
shapeColor = fmt.Sprintf("url(#%s)", _id)
}
textColor := palette.text.Color
if palette.text.Mode == Definition {
_id := avatar.id("text-color")
data = append(data, strings.ReplaceAll(palette.text.Color, "{id}", _id))
textColor = fmt.Sprintf("url(#%s)", _id)
}
dressColor := palette.dress.Color
if palette.dress.Mode == Definition {
_id := avatar.id("dress-color")
data = append(data, strings.ReplaceAll(palette.dress.Color, "{id}", _id))
dressColor = fmt.Sprintf("url(#%s)", _id)
}
dressShadow := palette.dressShadow.Color
if palette.dressShadow.Mode == Definition {
_id := avatar.id("dress-shadow")
data = append(data, strings.ReplaceAll(palette.dressShadow.Color, "{id}", _id))
dressShadow = fmt.Sprintf("url(#%s)", _id)
}
decoratorColor := palette.decorator.Color
if palette.decorator.Mode == Definition {
_id := avatar.id("decorator-color")
data = append(data, strings.ReplaceAll(palette.decorator.Color, "{id}", _id))
decoratorColor = fmt.Sprintf("url(#%s)", _id)
}
data = append(data, "</defs>")
// utils
render := func(svg string) string {
svg = strings.ReplaceAll(svg, "{skin}", skinColor)
svg = strings.ReplaceAll(svg, "{skin_shadow}", skinShadow)
svg = strings.ReplaceAll(svg, "{hair}", hairColor)
svg = strings.ReplaceAll(svg, "{hair_shadow}", hairShadow)
svg = strings.ReplaceAll(svg, "{hair_highlight}", hairHighlight)
svg = strings.ReplaceAll(svg, "{shape}", shapeColor)
svg = strings.ReplaceAll(svg, "{text}", textColor)
svg = strings.ReplaceAll(svg, "{dress}", dressColor)
svg = strings.ReplaceAll(svg, "{dress_shadow}", dressShadow)
svg = strings.ReplaceAll(svg, "{decorator}", decoratorColor)
return svg
}
// add shape
shape, mask := resolveShape(avatar.shape)
if shape != "" {
data = append(data, render(shape))
}
if mask != "" {
data = append(data, fmt.Sprintf(`<mask id="%s">`, avatar.id("mask")))
data = append(data, mask)
data = append(data, `</mask>`)
data = append(data, fmt.Sprintf(`<g mask="url(#%s)">`, avatar.id("mask")))
} else {
data = append(data, "<g>")
}
// text avatar
if avatar._type == LETTER {
shape := resolveLetterShape(avatar.name)
data = append(data, render(shape))
}
// sticker avatar
if avatar._type == STICKER {
shape := resolveSticker(avatar.sticker)
data = append(data, render(shape))
}
// character avatar
if avatar._type == MALE {
data = append(data, render(bodyShape))
data = append(data, render(resolveMaleDress(avatar.dress)))
data = append(data, render(resolveMaleEye(avatar.eye)))
data = append(data, render(resolveMaleEyebrow(avatar.eyebrow)))
data = append(data, render(resolveMaleMouth(avatar.mouth)))
data = append(data, render(resolveMaleGlass(avatar.glass)))
data = append(data, render(resolveMaleAccessory(avatar.accessory)))
data = append(data, render(resolveMaleHair(avatar.hair)))
data = append(data, render(resolveFacialHair(avatar.facialHair)))
}
if avatar._type == FEMALE {
data = append(data, render(bodyShape))
data = append(data, render(resolveFemaleDress(avatar.dress)))
data = append(data, render(resolveFemaleEye(avatar.eye)))
data = append(data, render(resolveFemaleEyebrow(avatar.eyebrow)))
data = append(data, render(resolveFemaleMouth(avatar.mouth)))
data = append(data, render(resolveFemaleGlass(avatar.glass)))
data = append(data, render(resolveFemaleAccessory(avatar.accessory)))
data = append(data, render(resolveFemaleHair(avatar.hair)))
}
data = append(data, `</g>`)
data = append(data, `</svg>`)
return strings.Join(data, "")
}
// SVG get svg file content
func (avatar Avatar) SVG() string {
return `<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">` + avatar.InlineSVG()
}
// Base64 get svg as base64 data image
func (avatar Avatar) Base64() string {
return "data:image/svg+xml;base64," + string(base64.StdEncoding.EncodeToString([]byte(avatar.SVG())))
}
// SaveAs save svg file
func (avatar Avatar) SaveAs(path string) error {
return os.WriteFile(path, []byte(avatar.SVG()), 0644)
}
// Type get avatar type
func (avatar Avatar) Type() AvatarType {
return avatar._type
}
// Shape get avatar shape
func (avatar Avatar) Shape() string {
return avatar.shape
}
// Palette get avatar palette
func (avatar Avatar) Palette() string {
return avatar.palette
}
// Letter get avatar letter
func (avatar Avatar) Letter() string {
return transformLetter(avatar.name)
}
// Type get avatar letter unicode
func (avatar Avatar) LetterCode() rune {
r, _ := utf8.DecodeRuneInString(avatar.Letter())
return r
}
// Type get avatar letter unicode
func (avatar Avatar) Sticker() string {
return avatar.sticker
}
// RandomizeShape randomize shape from registered ones
func (avatar *Avatar) RandomizeShape(only ...string) *Avatar {
if len(only) == 0 {
only = shapeKeys()
}
only = filterKeys(shapeKeys(), only)
if len(only) == 1 {
avatar.shape = only[0]
} else if len(only) > 1 {
avatar.shape = only[avatar.randTo(len(only))]
} else {
avatar.shape = ""
}
return avatar
}
// RandomizePalette randomize palette from registered ones
func (avatar *Avatar) RandomizePalette(only ...string) *Avatar {
if len(only) == 0 {
only = paletteKeys()
}
only = filterKeys(paletteKeys(), only)
if len(only) == 1 {
avatar.palette = only[0]
} else if len(only) > 1 {
avatar.palette = only[avatar.randTo(len(only))]
} else {
avatar.palette = ""
}
return avatar
}
// RandomizeSticker randomize sticker from registered ones
func (avatar *Avatar) RandomizeSticker(only ...string) *Avatar {
if len(only) == 0 {
only = stickerKeys()
}
only = filterKeys(stickerKeys(), only)
if len(only) == 1 {
avatar.sticker = only[0]
} else if len(only) > 1 {
avatar.sticker = only[avatar.randTo(len(only))]
} else {
avatar.sticker = ""
}
return avatar
}
// RandomizeSkinColor randomize skin from registered ones
func (avatar *Avatar) RandomizeSkinColor(only ...string) *Avatar {
if len(only) == 0 {
only = skinColorKeys()
}
only = filterKeys(skinColorKeys(), only)
if len(only) == 1 {
avatar.skinColor = only[0]
} else if len(only) > 1 {
avatar.skinColor = only[avatar.randTo(len(only))]
} else {
avatar.skinColor = ""
}
return avatar
}
// RandomizeHairColor randomize hair color from registered ones
func (avatar *Avatar) RandomizeHairColor(only ...string) *Avatar {
if len(only) == 0 {
only = hairColorKeys()
}
only = filterKeys(hairColorKeys(), only)
if len(only) == 1 {
avatar.hairColor = only[0]
} else if len(only) > 1 {
avatar.hairColor = only[avatar.randTo(len(only))]
} else {
avatar.hairColor = ""
}
return avatar
}
// RandomizeFacialHair randomize facial hair from registered ones
func (avatar *Avatar) RandomizeFacialHair(only ...string) *Avatar {
if len(only) == 0 {
only = facialHairKeys()
}
only = filterKeys(facialHairKeys(), only)
if len(only) == 1 {
avatar.facialHair = only[0]
} else if len(only) > 1 {
avatar.facialHair = only[avatar.randTo(len(only))]
} else {
avatar.facialHair = ""
}
return avatar
}
// RandomizeHair randomize hair from registered ones
func (avatar *Avatar) RandomizeHair(only ...string) *Avatar {
if avatar._type == FEMALE {
if len(only) == 0 {
only = femaleHairKeys()
}
only = filterKeys(femaleHairKeys(), only)
} else {
if len(only) == 0 {
only = maleHairKeys()
}
only = filterKeys(maleHairKeys(), only)
}
if len(only) == 1 {
avatar.hair = only[0]
} else if len(only) > 1 {
avatar.hair = only[avatar.randTo(len(only))]
} else {
avatar.hair = ""
}
return avatar
}
// RandomizeDress randomize dress from registered ones
func (avatar *Avatar) RandomizeDress(only ...string) *Avatar {
if avatar._type == FEMALE {
if len(only) == 0 {
only = femaleDressKeys()
}
only = filterKeys(femaleDressKeys(), only)
} else {
if len(only) == 0 {
only = maleDressKeys()
}
only = filterKeys(maleDressKeys(), only)
}
if len(only) == 1 {
avatar.dress = only[0]
} else if len(only) > 1 {
avatar.dress = only[avatar.randTo(len(only))]
} else {
avatar.dress = ""
}
return avatar
}
// RandomizeEye randomize eye from registered ones
func (avatar *Avatar) RandomizeEye(only ...string) *Avatar {
if avatar._type == FEMALE {
if len(only) == 0 {
only = femaleEyeKeys()
}
only = filterKeys(femaleEyeKeys(), only)
} else {
if len(only) == 0 {
only = maleEyeKeys()
}
only = filterKeys(maleEyeKeys(), only)
}
if len(only) == 1 {
avatar.eye = only[0]
} else if len(only) > 1 {
avatar.eye = only[avatar.randTo(len(only))]
} else {
avatar.eye = ""
}
return avatar
}
// RandomizeEyebrow randomize eyebrow from registered ones
func (avatar *Avatar) RandomizeEyebrow(only ...string) *Avatar {
if avatar._type == FEMALE {
if len(only) == 0 {
only = femaleEyebrowKeys()
}
only = filterKeys(femaleEyebrowKeys(), only)
} else {
if len(only) == 0 {
only = maleEyebrowKeys()
}
only = filterKeys(maleEyebrowKeys(), only)
}
if len(only) == 1 {
avatar.eyebrow = only[0]
} else if len(only) > 1 {
avatar.eyebrow = only[avatar.randTo(len(only))]
} else {
avatar.eyebrow = ""
}
return avatar
}
// RandomizeMouth randomize mouth from registered ones
func (avatar *Avatar) RandomizeMouth(only ...string) *Avatar {
if avatar._type == FEMALE {
if len(only) == 0 {
only = femaleMouthKeys()
}
only = filterKeys(femaleMouthKeys(), only)
} else {
if len(only) == 0 {
only = maleMouthKeys()
}
only = filterKeys(maleMouthKeys(), only)
}
if len(only) == 1 {
avatar.mouth = only[0]
} else if len(only) > 1 {
avatar.mouth = only[avatar.randTo(len(only))]
} else {
avatar.mouth = ""
}
return avatar
}
// RandomizeGlass randomize glass from registered ones
func (avatar *Avatar) RandomizeGlass(only ...string) *Avatar {
if avatar._type == FEMALE {
if len(only) == 0 {
only = femaleGlassKeys()
}
only = filterKeys(femaleGlassKeys(), only)
} else {
if len(only) == 0 {
only = maleGlassKeys()
}
only = filterKeys(maleGlassKeys(), only)
}
if len(only) == 1 {
avatar.glass = only[0]
} else if len(only) > 1 {
avatar.glass = only[avatar.randTo(len(only))]
} else {
avatar.glass = ""
}
return avatar
}
// RandomizeAccessory randomize accessory from registered ones
func (avatar *Avatar) RandomizeAccessory(only ...string) *Avatar {
if avatar._type == FEMALE {
if len(only) == 0 {
only = femaleAccessoryKeys()
}
only = filterKeys(femaleAccessoryKeys(), only)
} else {
if len(only) == 0 {
only = maleAccessoryKeys()
}
only = filterKeys(maleAccessoryKeys(), only)
}
if len(only) == 1 {
avatar.accessory = only[0]
} else if len(only) > 1 {
avatar.accessory = only[avatar.randTo(len(only))]
} else {
avatar.accessory = ""
}
return avatar
}