Exporte gender aware avatar generation (#9)

Add function to create male or female avatars

Co-authored-by: nfirbas <nejc.firbas99@gmail.com>
Reviewed-on: https://codeberg.org/Codeberg/avatars/pulls/9
Co-authored-by: nfirbas <nfirbas@noreply.codeberg.org>
Co-committed-by: nfirbas <nfirbas@noreply.codeberg.org>
This commit is contained in:
nfirbas 2021-12-28 17:30:22 +01:00 committed by 6543
parent c868879277
commit 8da63012fe

View File

@ -10,17 +10,23 @@ import (
// MakeAvatar create svg from seed // MakeAvatar create svg from seed
func MakeAvatar(seedString string) string { func MakeAvatar(seedString string) string {
var seed uint64 seed := generateSeed(seedString)
for _, c := range []byte(seedString) {
seed = bits.RotateLeft64(seed, 8)
seed ^= uint64(c)
}
if seed&1 == 0 { if seed&1 == 0 {
return femaleAvatar(seed, "") return femaleAvatar(seed, "")
} }
return maleAvatar(seed, "") return maleAvatar(seed, "")
} }
// MakeFemaleAvatar create female svg from seed
func MakeFemaleAvatar(seedString string) string {
return femaleAvatar(generateSeed(seedString), "")
}
// MakeMaleAvatar create male svg from seed
func MakeMaleAvatar(seedString string) string {
return maleAvatar(generateSeed(seedString), "")
}
/** /**
* Based on TypeScript DiceBear Avatars, which in turn were inspired by 8biticon avatars: * Based on TypeScript DiceBear Avatars, which in turn were inspired by 8biticon avatars:
* (MIT License, Copyright (c) 2012 Plastic Jam, Copyright (c) 2019 DiceBear) * (MIT License, Copyright (c) 2012 Plastic Jam, Copyright (c) 2019 DiceBear)
@ -31,6 +37,14 @@ type lcg struct {
seed uint64 seed uint64
} }
func generateSeed(seedString string) (seed uint64) {
for _, c := range []byte(seedString) {
seed = bits.RotateLeft64(seed, 8)
seed ^= uint64(c)
}
return seed
}
func (g *lcg) random() uint32 { func (g *lcg) random() uint32 {
/* Linear Congruent Generator, POSIX/glibc [de]rand48 setting, bits [47..0] are output bits */ /* Linear Congruent Generator, POSIX/glibc [de]rand48 setting, bits [47..0] are output bits */
g.seed = (25214903917*g.seed + 11) % 281474976710656 g.seed = (25214903917*g.seed + 11) % 281474976710656