From 8da63012fe6968daf311c4e1ceb470d8ca7e04fc Mon Sep 17 00:00:00 2001 From: nfirbas Date: Tue, 28 Dec 2021 17:30:22 +0100 Subject: [PATCH] Exporte gender aware avatar generation (#9) Add function to create male or female avatars Co-authored-by: nfirbas Reviewed-on: https://codeberg.org/Codeberg/avatars/pulls/9 Co-authored-by: nfirbas Co-committed-by: nfirbas --- avatars.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/avatars.go b/avatars.go index 71279e0..d953ca3 100644 --- a/avatars.go +++ b/avatars.go @@ -10,17 +10,23 @@ import ( // MakeAvatar create svg from seed func MakeAvatar(seedString string) string { - var seed uint64 - for _, c := range []byte(seedString) { - seed = bits.RotateLeft64(seed, 8) - seed ^= uint64(c) - } + seed := generateSeed(seedString) if seed&1 == 0 { return femaleAvatar(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: * (MIT License, Copyright (c) 2012 Plastic Jam, Copyright (c) 2019 DiceBear) @@ -31,6 +37,14 @@ type lcg struct { 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 { /* Linear Congruent Generator, POSIX/glibc [de]rand48 setting, bits [47..0] are output bits */ g.seed = (25214903917*g.seed + 11) % 281474976710656