Files
go-jdenticon/example_usage.go
Kevin McIntyre f84b511895 init
2025-06-18 01:00:00 -04:00

123 lines
3.0 KiB
Go

package main
import (
"fmt"
"os"
"github.com/kevin/go-jdenticon/jdenticon"
)
func main() {
// Test the new public API with different input types
// 1. Generate SVG from email address
fmt.Println("=== Generating SVG avatar for email ===")
svg, err := jdenticon.ToSVG("user@example.com", 128)
if err != nil {
panic(err)
}
fmt.Printf("SVG length: %d characters\n", len(svg))
fmt.Printf("SVG preview: %s...\n", svg[:100])
// Save SVG to file
err = os.WriteFile("avatar_email.svg", []byte(svg), 0644)
if err != nil {
panic(err)
}
fmt.Println("✅ Saved to avatar_email.svg")
// 2. Generate PNG from username
fmt.Println("\n=== Generating PNG avatar for username ===")
png, err := jdenticon.ToPNG("johndoe", 64)
if err != nil {
panic(err)
}
fmt.Printf("PNG size: %d bytes\n", len(png))
// Save PNG to file
err = os.WriteFile("avatar_username.png", png, 0644)
if err != nil {
panic(err)
}
fmt.Println("✅ Saved to avatar_username.png")
// 3. Generate with custom configuration
fmt.Println("\n=== Generating with custom config ===")
config, err := jdenticon.Configure(
jdenticon.WithHueRestrictions([]float64{120, 240}), // Blue/green hues only
jdenticon.WithColorSaturation(0.8),
jdenticon.WithBackgroundColor("#ffffff"), // White background
jdenticon.WithPadding(0.1),
)
if err != nil {
panic(err)
}
customSvg, err := jdenticon.ToSVG("custom-avatar", 96, config)
if err != nil {
panic(err)
}
err = os.WriteFile("avatar_custom.svg", []byte(customSvg), 0644)
if err != nil {
panic(err)
}
fmt.Println("✅ Saved custom styled avatar to avatar_custom.svg")
// 4. Test different input types
fmt.Println("\n=== Testing different input types ===")
inputs := []interface{}{
"hello world",
42,
3.14159,
true,
[]byte("binary data"),
}
for i, input := range inputs {
svg, err := jdenticon.ToSVG(input, 48)
if err != nil {
panic(err)
}
filename := fmt.Sprintf("avatar_type_%d.svg", i)
err = os.WriteFile(filename, []byte(svg), 0644)
if err != nil {
panic(err)
}
fmt.Printf("✅ Generated avatar for %T: %v -> %s\n", input, input, filename)
}
// 5. Show hash generation
fmt.Println("\n=== Hash generation ===")
testValues := []interface{}{"test", 123, []byte("data")}
for _, val := range testValues {
hash := jdenticon.ToHash(val)
fmt.Printf("Hash of %v (%T): %s\n", val, val, hash)
}
// 6. Generate avatars for a group of users
fmt.Println("\n=== Group avatars ===")
users := []string{
"alice@company.com",
"bob@company.com",
"charlie@company.com",
"diana@company.com",
}
for _, user := range users {
png, err := jdenticon.ToPNG(user, 80)
if err != nil {
panic(err)
}
filename := fmt.Sprintf("user_%s.png", user[:5]) // Use first 5 chars as filename
err = os.WriteFile(filename, png, 0644)
if err != nil {
panic(err)
}
fmt.Printf("✅ Generated avatar for %s -> %s\n", user, filename)
}
fmt.Println("\n🎉 All avatars generated successfully!")
fmt.Println("Check the generated SVG and PNG files in the current directory.")
}