Skip to content

tsdtsdtsd/identicon

Repository files navigation

Identicon

A golang library for Identicon generation

Latest Release Version Godoc Build Status Go Report Card codecov CodeQL

This Go library helps to generate deterministic Identicons from strings, like these:

Example Banner


⚠️ v1.0 brings BREAKING CHANGES ⚠️

The API changed and also the internal hashing algorithm, so identicons generated after v0.3.2 will look different.


Usage:

  1. Get it:
go get github.com/tsdtsdtsd/identicon
  1. Create a new identicon:
black := color.RGBA{0, 0, 0, 255}
red := color.RGBA{255, 0, 0, 255}

icon, err := identicon.New(
    "gary@example.com", 
    // optional:
    identicon.WithResolution(7),      // default: 5
    identicon.WithImageSize(210),     // default: 100
    identicon.WithBGColor(black),     // default: light gray R:240 G:240 B:240 A:255 (#f0f0f0)
    identicon.WithFGColor(red),       // default: based on identifier
    identicon.WithHasher(sha1.New()), // default: fnv128
)
if err != nil {
    log.Fatal(err)
}
  1. Create an image:

    Image() returns an *Image which implements Go's image.Image interface, so you can use the result directly to encode it as an image file:

file, err := os.Create("identicon-gary.png")
if err != nil {
    log.Fatal(err)
}

err = png.Encode(file, icon.Image())
if err != nil {
    log.Fatal(err)
}

file.Close()

Image also implements Go's draw.Image interface, you can use it to change the output further:

    // TODO: example 
    // draw.Draw(icon, ...

Banner example

You can find another example in the /_example folder. It contains an application, which generates the above image. It also helps me to test the algorythm for changes.