A golang library for Identicon generation
This Go library helps to generate deterministic Identicons from strings, like these:
The API changed and also the internal hashing algorithm, so identicons generated after v0.3.2 will look different.
- Get it:
go get github.com/tsdtsdtsd/identicon
- Create a new identicon:
black := color.RGBA{0, 0, 0, 255}
red := color.RGBA{255, 0, 0, 255}
icon, err := identicon.New(
"[email protected]",
// 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)
}
-
Create an image:
Image()
returns an*Image
which implements Go'simage.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, ...
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.