goavatar

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2025 License: MIT Imports: 5 Imported by: 0

README

Goavatar Identicon Generator in Go

This package provides a simple way to generate unique, symmetric identicons based on an input string (e.g., an email address or username). It uses an MD5 hash to create a deterministic pattern and color scheme, then mirrors the design for a visually appealing avatar.

User Avatars

Avatar 1
QuantumNomad42      Avatar 2
EchoFrost7      Avatar 3
NebulaTide19      Avatar 4
ZephyrPulse88      Avatar 5
EmberNexus23      Avatar 5
nice__user__name

Installation

To use this package in your Go project, install it via:

go get github.com/MuhammadSaim/goavatar

Then, import it in your Go code:

import "github.com/MuhammadSaim/goavatar"

Usage

Basic Example
package main

import (
 "fmt"
 "image"
 "image/png"
 "os"

 "github.com/MuhammadSaim/goavatar"
)

func main() {
 // empty slice.
 imgSlice := make([]image.Image, 0)

 // Generates a unique avatar based on "QuantumNomad42" with a custom width and height.
 // Saves the generated avatar as avatar_1.png
 image1 := goavatar.Make("QuantumNomad42",
  goavatar.WithSize(512),  // Set custom image widthxheight (default is 64)
 )

 // Generate the second avatar with a custom grid size with a 10x10 grid for more detail.
 // Saves the generated avatar as avatar_2.png
 image2 := goavatar.Make("EchoFrost7",
  goavatar.WithSize(512),   // Set custom image widthxheight (default is 64)
  goavatar.WithGridSize(10), // Set custom grid size (default is 8), affects pattern complexity
 )

 // Generate the third avatar with a custom brownish background color.
 // Saves the generated avatar as avatar_3.png
 image3 := goavatar.Make("NebulaTide19",
  goavatar.WithSize(512),                 // Set custom image widthxheight (default is 256)
  goavatar.WithBgColor(170, 120, 10, 255), // Change background color (default is light gray)
 )

 // Generate the fourth avatar with a custom brownish background and white foreground.
 // Saves the generated avatar as avatar_4.png
 image4 := goavatar.Make("ZephyrPulse88",
  goavatar.WithSize(512),                  // Set custom image widthxheight (default is 64)
  goavatar.WithBgColor(170, 120, 10, 255),  // Change background color (default is light gray)
  goavatar.WithFgColor(255, 255, 255, 255), // Change foreground color (default is extracted from hash)

 )

 // Generate an avatar using default settings
 // Saves the generated avatar as avatar_5.png
 image5 := goavatar.Make("EmberNexus23")

 // Collect options dynamically
 var opts []goavatar.OptFunc

 // add size
 opts = append(opts, goavatar.WithSize(100))
 opts = append(opts, goavatar.WithGridSize(10))
 image6 := goavatar.Make("nice__user__name", opts...)

 // append all the images into the list
 imgSlice = append(imgSlice, image1, image2, image3, image4, image5, image6)

 // loop through the image slice and save the images
 for i, img := range imgSlice {

  filename := fmt.Sprintf("../arts/avatar_%d.png", i+1)

  // Create the file
  file, err := os.Create(filename)
  if err != nil {
   fmt.Println("Error creating file:", err)
   continue
  }
  defer file.Close()

  // Encode image as PNG and save
  err = png.Encode(file, img)
  if err != nil {
   fmt.Println("Error saving image:", err)
  } else {
   fmt.Println("Saved: ", filename)
  }

 }
}

This will generate a unique identicons for the input string and save in the arts directory.

Package Documentation

Generate Identicon
func Make(input, ...optFunc) image.Image
  • input: A string used to generate a unique identicon (e.g., email, username).
  • ...optFunc: Functional options to override the default values.
  • image.Image: Function returns an image.Image, allowing the caller to handle image processing, encoding, and storage as needed.

License

This project is open-source under the MIT License.

Contributing

Contributions are welcome! Feel free to open a pull request or create an issue.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make(input string, opts ...OptFunc) image.Image

Make generates an avatar image based on the input string and options.

Types

type OptFunc

type OptFunc func(*options)

optFunc is a function that applies an option to the options struct.

func WithBgColor

func WithBgColor(r, g, b, a uint8) OptFunc

WithBgColor sets the background color of the avatar.

func WithFgColor

func WithFgColor(r, g, b, a uint8) OptFunc

WithFgColor sets the foreground color of the avatar.

func WithGridSize

func WithGridSize(g int) OptFunc

WithGridSize sets the grid size of the avatar.

func WithSize

func WithSize(s int) OptFunc

WithSize sets the width and height of the avatar minimum 64x64.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL