identicon

package module
v0.0.0-...-317ebf9 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2021 License: MIT Imports: 9 Imported by: 0

README

identicon

go package to generate github identicons

based on dgraham's rust identicon

https://github.com/dgraham/identicon

algorithm

github identicons are generated from the md5 hash of a user's id.

there are two components to an identicon: the pattern and the color

for example: my user id is 32273214

$ echo -n "32273214" | md5sum
3a44233aabcac24ed7c6e160b30f4289  -
pattern

the first 15 nibbles of the md5 hash make the identicon pattern (nibble == half byte == 4 bits)

even nibbles result in colored (foreground, o) pixels, odd nibbles result in gray (background, -) pixels

in my case:

3 a 4 4 2
3 3 a a b
c a c 2 4

produces:

- o o o o
- - o o -
o o o o o

the final identicon pattern is made by rotation and mirroring:

o - - - o
o - o - o
o o o o o
o o o o o
o - o - o
color

color is calculated from the last 7 nibbles (28 bits) of the hash HHHSSLL:

the HHH (0..4095) value is remapped to a value between 0..360 degrees and is used as the hue

the SS (0..255) value is remapped to a value between 0..20 and is subtracted from a max saturation of 65 percent

the LL (0..255) is remapped to a value between 0..20 and is subtracted from a max lightness of 75 percent

in my case 30f4289:

0x30f is remapped to 68.83 degrees, which is my hue,

0x42 is remapped to 5.17, so my saturation is 65 - 5.17 == 59.83%,

0x89 is remapped to 10.74, so my luminance is 75 - 10.74 == 64.26%.

further these values are converted into the rgb color space resulting in #cada6d rgb color code

the combination of the pattern and color create the final identicon image

usage

all exported functions take data []byte and size int

data is used as input to md5.

note: size is identicon pixel size (not final image size).

final image will be 6 x 6 pixels of given size

(that is a 5 x 5 matrix plus margins)

example

package main

import (
	"fmt"
	"github.com/asarandi/identicon"
)

func main() {
	for i := 0; i <= 10; i++ {
		s := fmt.Sprintf("%d", i)
		identicon.File([]byte(s), 8, s+".png")
	}
}

0 1 2 3 4 5 6 7 8 9 10

license

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base64

func Base64(data []byte, size int) (string, error)

Base64 returns a base64 encoded string

func Bytes

func Bytes(data []byte, size int) ([]byte, error)

Bytes returns a PNG encoded identicon as bytes

func File

func File(data []byte, size int, name string) error

File saves an identicon to a file

func Image

func Image(data []byte, size int) image.Image

Image returns an image.Image given input data and pixel size

Types

This section is empty.

Jump to

Keyboard shortcuts

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