Advanced customizable svg avatar generator for golang.
Unicode | Letter | Sticker | Female | Female | Female | Male | Male | Male |
---|---|---|---|---|---|---|---|---|
go get github.com/gomig/avatar
If letter shape or letter map not founded for first character of string fallback (Default
) letter shape will be used.
import "github.com/gomig/avatar"
func main(){
john := avatar.NewTextAvatar("John") // make avatar with J letter
htmlTag := john.InlineSVG()
svg := john.SVG()
dataImg := john.Base64()
john.SaveAs("john.svg")
}
import "github.com/gomig/avatar"
func main(){
// Register sticker shape globally
avatar.AddSticker("apple", `<path fill="{text}" d="..."/>`)
// Create avatar using apple sticker
apple := avatar.NewStickerAvatar("apple")
apple.SaveAs("apple.svg")
}
import "github.com/gomig/avatar"
func main(){
mia := avatar.NewPersonAvatar(false) // pass true for male, false for female
mia.RandomizeShape(avatar.Circle, "polygon") // get random background shape
mia.SaveAs("mia.svg")
}
Generate svg content for embeded (html svg tag) element.
Get svg file content.
Get svg as base64 data image.
Save svg file.
Get avatar type.
Get avatar shape.
Get avatar palette.
Get avatar letter.
Get avatar letter unicode.
Get avatar sticker name.
You can pass list off registered parts to generate random avatar. If no parameters passed random character created from all registered values.
Note: in PersonAvatar random values get from registered value based on gender.
If you want use predefined elements you can access them by constant.
- Default (All except FacialHair, Glass And Accessory) default element
- None (FacialHair, Glass And Accessory) allow select no item.
- BrownHair (HairColor)
#3a292f
hair color. - LightBrownHair (HairColor)
#744819
hair color. - DarkBrownHair (HairColor)
#432818
hair color. - WhiteSkin (Skin)
#fbdad0
skin color. - BrownSkin (Skin)
#ceaa82
skin color. - BlackSkin (Skin)
#b06f51
skin color. - Fill (Shape) fill background of avatar
- Circle (Shape) circular background
- Polygon (Shape) polygon background
- Purple (Palette) purple palette (only available if
RegisterExtraPalettes
called) - Green (Palette) green palette (only available if
RegisterExtraPalettes
called) - Blue (Palette) blue palette (only available if
RegisterExtraPalettes
called) - Yellow (Palette) yellow palette (only available if
RegisterExtraPalettes
called) - Orange (Palette) orange palette (only available if
RegisterExtraPalettes
called) - Red (Palette) red palette (only available if
RegisterExtraPalettes
called) - Teal (Palette) teal palette (only available if
RegisterExtraPalettes
called) - Pink (Palette) pink palette (only available if
RegisterExtraPalettes
called) - Short (Hair) short hair style (only available if
RegisterExtraMaleHair|RegisterExtraFemaleHair
called) - Wavy (Hair) wavy hair style (only available if
RegisterExtraMaleHair|RegisterExtraFemaleHair
called) - Medium (MaleHair) medium hair style (only available if
RegisterExtraMaleHair
called) - Curly (FemaleHair) curly hair style (only available if
RegisterExtraFemaleHair
called) - Suit (Dress) short hair style (only available if
RegisterExtraMaleDress|RegisterExtraFemaleDress
called) - Shirt (Dress) short hair style (only available if
RegisterExtraMaleDress|RegisterExtraFemaleDress
called) - TShirt (Dress) short hair style (only available if
RegisterExtraMaleDress|RegisterExtraFemaleDress
called) - Mustach (FacialHair) mustach facial (only available if
RegisterExtraFacialHair
called) - MustachFancy (FacialHair) fancy mustach (only available if
RegisterExtraFacialHair
called) - Beard (FacialHair) short beard (only available if
RegisterExtraFacialHair
called) - BeardMedium (FacialHair) medium beard (only available if
RegisterExtraFacialHair
called) - BeardLong (FacialHair) long beard (only available if
RegisterExtraFacialHair
called) - Prescription (Glass) colored prescription glass (only available if
RegisterExtraMaleGlass|RegisterExtraFemaleGlass
called) - PrescriptionRound (Glass) rounded prescription glass (only available if
RegisterExtraMaleGlass|RegisterExtraFemaleGlass
called) - Sunglass (Glass) black sunglass (only available if
RegisterExtraMaleGlass|RegisterExtraFemaleGlass
called) - SunglassRound (Glass) rounded black sunglass (only available if
RegisterExtraMaleGlass|RegisterExtraFemaleGlass
called) - Necklace (FemaleAccessory) normal necklace (only available if
RegisterExtraFemaleAccessory
called) - Choker (FemaleAccessory) choker necklace (only available if
RegisterExtraFemaleAccessory
called)
Randomize shape from registered ones.
Randomize avatar color palette from registered ones.
Randomize sticker from registered ones (this method works on sticker avatar only).
Randomize skin from registered ones (this method works on person avatar only).
Randomize hair color from registered ones (this method works on person avatar only).
Randomize facial hair from registered ones (this method works on male person avatar only).
Randomize hair from registered ones (this method works on person avatar only).
Randomize dress from registered ones (this method works on person avatar only).
Randomize eye from registered ones (this method works on person avatar only).
Randomize eyebrow from registered ones (this method works on person avatar only).
Randomize mouth from registered ones (this method works on person avatar only).
Randomize glass from registered ones (this method works on person avatar only).
Randomize accessory from registered ones (this method works on person avatar only).
By default no extra element are exists and all generated avatar are equal! for using predefined package element you must call register functions.
Register extra color palettes (purple
, green
, blue
, yellow
, orange
, red
, teal
and pink
).
Register transform map for transforming persian letter to english.
Register letter shape for persian letters (if RegisterPersianTransform
called this method not work and persian letter transformed and shown as english letter)
Register extra male hairs (short
, medium
and wavy
).
Register extra male dresses (t-shirt
, suit
and shirt
).
Register extra male facial hairs (mustach
, mustach-fancy
, beard
, beard-medium
and beard-long
).
Register extra male glasses (prescription
, prescription-round
, sunglass
and sunglass-round
).
Currently no male accessory are designed built-in!
Register extra female hairs (short
, wavy
and curly
).
Register extra female dresses (t-shirt
, suit
and shirt
).
Register extra female glasses (prescription
, prescription-round
, sunglass
and sunglass-round
).
Register extra female accessory (necklace
and choker
).
Avatar generation is based on separated svg elements. You can customize current avatar style or add new styles to avatar package using helpers method.
You can work on template.ai file and save your work to svg then add your generated elements parts to avatar packages. Each shape must have some placeholder for replace colors or id in final result.
Elements color are based on palette. Color value can be simple hex (Hex
) or gradient (Definition
).
There are three type of colors:
- HairColor: contains base, shadow and highlight color.
- SkinColor: contains skin and shadow color.
- Palette: contains shape, text, dress, dressShadow and decorator color.
Caution: for gradient colors, gradient element must contains id="{id}"
placeholder attribute. otherwise color not work!
Clear all registered hair color and set hair colors to pre-defined (Default
, brown
, light_brown
and dark_brown
) value.
Add new hair color.
import "github.com/gomig/avatar"
func main(){
avatar.AddHairColor(
"brown",
avatar.Color{avatar.Hex, "#432818"},
avatar.Color{avatar.Hex, "#140b06"},
avatar.Color{avatar.Hex, "#884139"},
)
}
Change default hair color.
Clear all registered skin color and set skin colors to pre-defined (Default
, white
, brown
and black
) value.
Add new skin color.
import "github.com/gomig/avatar"
func main(){
avatar.AddHairColor(
"blond",
avatar.Color{
avatar.Definition,
`<linearGradient id="{id}" gradientTransform="rotate(90)">
<stop offset="5%" stop-color="#fffa00" />
<stop offset="95%" stop-color="#cc8800" />
</linearGradient>`,
},
avatar.Color{avatar.Hex, "#84706d"})
}
Change default skin color.
Clear all registered palette and keep Default
palette only.
Add new palette.
import "github.com/gomig/avatar"
func main(){
avatar.AddPalette(
"blue",
avatar.Color{avatar.Hex, "#a2cced"},
avatar.Color{avatar.Hex, "#162835"},
avatar.Color{avatar.Hex, "#006fc8"},
avatar.Color{avatar.Hex, "#045389"},
avatar.Color{avatar.Hex, "#121b21"},
)
}
Change default palette.
You can use following placeholder in your element to replace with dynamic generated data in avatar.
{id}
must included in gradient colors to pass dynamic id.{skin}
person skin color (used in body shape).{skin_shadow}
dark skin color (used in shadow parts of body shape).{hair}
hair color (used in hair, eyebrow and facialHair).{hair_shadow}
dark parts of hair (used in hair, eyebrow and facialHair).{hair_highlight}
highlight parts of hair (used in hair, eyebrow and facialHair).{shape}
background shape color (used in background shape fill).{text}
text and sticker color.{dress}
main dress color.{dress_shadow}
darken parts color of dress.{decorator}
decorator color (tie, glass frame, necklace, etc.).
Note: You can set mask for each background shape. mask shape must has white
fill.
Clear all registered shapes.
Register new background shape.
import "github.com/gomig/avatar"
func main(){
avatar.AddShape(
"circle",
"<circle fill="{shape}" cx="64" cy="64" r="64"/>",
"<path fill="white" d="M0,0v64c0,35.3,28.7,64,64,64s64-28.7,64-64V0H0z"/>")
}
You can register shape for each unicode letters or you can transform letters to english.
Define new unicode letter for transform to english letter or letter with shape.
import "github.com/gomig/avatar"
func main(){
avatar.AddLetterMap("ج", `j`)
}
Define shape for letter. Letter must have one character length! By default english letter shapes are registered but you can override them by re-set letter shape.
import "github.com/gomig/avatar"
func main(){
avatar.AddLetterShape("a", `<path fill="{text}" d="..."/>`)
}
Set fallback default letter shape. If no shape or transform for character found this shape used!
Clear all registered stickers.
import "github.com/gomig/avatar"
func main(){
avatar.ResetSticker()
}
Register new sticker.
import "github.com/gomig/avatar"
func main(){
avatar.AddSticker("star", `<path fill="{text}" d="..."/>`)
}
Person avatar can be male or female. all type of data can registered for Male
, Female
or Both
.
Set character body shape to default.
Set character body shape.
import "github.com/gomig/avatar"
func main(){
avatar.SetBody(`<path fill="{skin}" ...>`)
}
Clear all registered facial hairs.
Add new facial hair.
import "github.com/gomig/avatar"
func main(){
avatar.SetBody("wavy", `<path fill="{hair}" ...>`)
}
Clear all registered male hairs (except Default
).
Clear all registered female hairs (except Default
).
Register new hair style.
import "github.com/gomig/avatar"
func main(){
avatar.SetBody("wavy", avatar.Male, `<path fill="{hair}" ...>`)
}
Set default hair.
Clear all registered male dresses (except Default
).
Clear all registered female dresses (except Default
).
Register new dress.
import "github.com/gomig/avatar"
func main(){
avatar.AddCloth(
"polo",
avatar.Both,
`<g>
<path fill="{dress}" ...>
<path fill="{dress_shadow}" ...>
</g>`,
)
}
Set default dress shape.
Clear all registered male eye (except Default
).
Clear all registered female eye (except Default
).
Register new eye.
import "github.com/gomig/avatar"
func main(){
avatar.SetBody("asian", avatar.Male, `<path ...>`)
}
Set default eye shpae.
Clear all registered male eyebrow (except Default
).
Clear all registered female eyebrow (except Default
).
Register new eyebrow.
import "github.com/gomig/avatar"
func main(){
avatar.SetBody("wavy", avatar.Male, `<path fill="{hair_shadow}" ...>`)
}
Set default eyebrow shape.
Clear all registered male mouths (except Default
).
Clear all registered female mouths (except Default
).
Register new mouth.
import "github.com/gomig/avatar"
func main(){
avatar.AddMouth("red_lip", avatar.Female, `<path ...>`)
}
Set default mouth.
Delete all registered male glasses.
Delete all registered female glasses.
Register new glass.
Clear all registered male accessory.
Clear all registered female accessory.
Add new accessory.
import "github.com/gomig/avatar"
func main(){
avatar.SetBody("necklace", avatar.Female, `<path fill="{decorator}" ...>`)
}