Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 45 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@ This package provides a simple way to generate unique, symmetric identicons base
</kbd>
&nbsp;&nbsp;&nbsp;&nbsp;
<kbd>
<img src="./arts/avatar_6.png" width="100" alt="Avatar 5"/><br/>
<img src="./arts/avatar_6.png" width="100" alt="Avatar 6"/><br/>
<strong>nice__user__name</strong>
</kbd>
&nbsp;&nbsp;&nbsp;&nbsp;
<kbd>
<img src="./arts/avatar_7.png" width="100" alt="Avatar 7"/><br/>
<strong>MultiLayer2</strong>
</kbd>
&nbsp;&nbsp;&nbsp;&nbsp;
<kbd>
<img src="./arts/avatar_8.png" width="100" alt="Avatar 8"/><br/>
<strong>MultiLayer3Custom</strong>
</kbd>
</p>

## Installation
Expand Down Expand Up @@ -107,16 +117,40 @@ func main() {
// 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)
// 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...)

// Generate multi-layered avatar (2 layers)
image7 := goavatar.Make("MultiLayer2",
goavatar.WithSize(512),
goavatar.WithLayers(2),
)

// Generate multi-layered avatar (3 layers) with custom colors for each layer
image8 := goavatar.Make("MultiLayer3Custom",
goavatar.WithSize(512),
goavatar.WithLayers(3),
goavatar.WithLayerColor(0, 255, 0, 0, 255), // Red
goavatar.WithLayerColor(1, 0, 255, 0, 255), // Green
goavatar.WithLayerColor(2, 0, 0, 255, 255), // Blue
)

// Generate multi-layered avatar with transparency
image9 := goavatar.Make("MultiLayer3Transparent",
goavatar.WithSize(512),
goavatar.WithLayers(3),
goavatar.WithLayerColor(0, 255, 0, 0, 80), // Red (Transparent)
goavatar.WithLayerColor(1, 0, 255, 0, 160), // Green (Transparent)
goavatar.WithLayerColor(2, 0, 0, 255, 255), // Blue (Opaque)
)

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

// loop through the image slice and save the images
for i, img := range imgSlice {
Expand Down
2 changes: 2 additions & 0 deletions arts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
!avatar_4.png
!avatar_5.png
!avatar_6.png
!avatar_7.png
!avatar_8.png
!goavatar-banner.png
Binary file added arts/avatar_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added arts/avatar_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 16 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func main() {
goavatar.WithSize(50), // Set custom image widthxheight if size is less then 64 this will go to default (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
Expand All @@ -54,8 +53,23 @@ func main() {
opts = append(opts, goavatar.WithGridSize(13))
image6 := goavatar.Make("nice__user__name", opts...)

// Generate multi-layered avatar (2 layers)
image7 := goavatar.Make("MultiLayer2",
goavatar.WithSize(512),
goavatar.WithLayers(2),
)

// Generate multi-layered avatar (3 layers) with custom colors for each layer
image8 := goavatar.Make("MultiLayer3Custom",
goavatar.WithSize(512),
goavatar.WithLayers(3),
goavatar.WithLayerColor(0, 255, 0, 0, 255), // Red
goavatar.WithLayerColor(1, 0, 255, 0, 255), // Green
goavatar.WithLayerColor(2, 0, 0, 255, 255), // Blue
)

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

// loop through the image slice and save the images
for i, img := range imgSlice {
Expand Down
124 changes: 83 additions & 41 deletions goavatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import (
"encoding/hex"
"image"
"image/color"
"image/draw"
"math"
)

// option contains the configuration for the avatar generator.
// options contains the configuration for the avatar generator.
type options struct {
size int
gridSize int
bgColor color.RGBA
fgColor color.RGBA
bgColor color.NRGBA
fgColors []color.NRGBA
layers int
}

// optFunc is a function that applies an option to the options struct.
// OptFunc is a function that applies an option to the options struct.
type OptFunc func(*options)

// WithSize sets the width and height of the avatar minimum 64x64.
Expand All @@ -42,24 +44,46 @@ func WithGridSize(g int) OptFunc {
// WithBgColor sets the background color of the avatar.
func WithBgColor(r, g, b, a uint8) OptFunc {
return func(o *options) {
o.bgColor = color.RGBA{r, g, b, a}
o.bgColor = color.NRGBA{r, g, b, a}
}
}

// WithFgColor sets the foreground color of the avatar.
// It sets the first layer's color.
func WithFgColor(r, g, b, a uint8) OptFunc {
return func(o *options) {
o.fgColor = color.RGBA{r, g, b, a}
o.fgColors = []color.NRGBA{{r, g, b, a}}
}
}

// WithLayers sets the number of foreground layers (1-3).
func WithLayers(n int) OptFunc {
return func(o *options) {
if n >= 1 && n <= 3 {
o.layers = n
}
}
}

// WithLayerColor sets the color for a specific layer index (0-based).
func WithLayerColor(layerIndex int, r, g, b, a uint8) OptFunc {
return func(o *options) {
// Expand slice if needed
for len(o.fgColors) <= layerIndex {
o.fgColors = append(o.fgColors, color.NRGBA{})
}
o.fgColors[layerIndex] = color.NRGBA{r, g, b, a}
}
}

// defaultOptions provides the default value to generate the avatar.
func defaultOptions(hash string) options {
return options{
size: 64, // default size should be 64 to make sure images are perfect square
gridSize: 8, // minimum size for the grid for make shape complexity
bgColor: color.RGBA{240, 240, 240, 255}, // light gray color
fgColor: color.RGBA{hash[0], hash[1], hash[2], 255}, // use the first three hash bytes as the foreground color
size: 64, // default size should be 64 to make sure images are perfect square
gridSize: 8, // minimum size for the grid for make shape complexity
bgColor: color.NRGBA{240, 240, 240, 255}, // light gray color
fgColors: []color.NRGBA{{hash[0], hash[1], hash[2], 255}}, // use the first three hash bytes as the foreground color
layers: 1,
}
}

Expand All @@ -86,11 +110,8 @@ func drawPixel(img *image.RGBA, gridX, gridY int, c color.Color, gridSize, image
}

// Fill the block
for y := startY; y < endY; y++ {
for x := startX; x < endX; x++ {
img.Set(x, y, c)
}
}
rect := image.Rect(startX, startY, endX, endY)
draw.Draw(img, rect, &image.Uniform{c}, image.Point{}, draw.Over)
}

// Make generates an avatar image based on the input string and options.
Expand All @@ -106,37 +127,58 @@ func Make(input string, opts ...OptFunc) image.Image {
// create a blank image
img := image.NewRGBA(image.Rect(0, 0, o.size, o.size))

// generate colors
avatarColor := o.fgColor
bgColor := o.bgColor
// Fill background
draw.Draw(img, img.Bounds(), &image.Uniform{o.bgColor}, image.Point{}, draw.Src)

currentHash := hash
isOdd := o.gridSize%2 != 0

// generate the pixel pattern
// loop over each pixel in the grid
for y := 0; y < o.gridSize; y++ {
for x := 0; x < o.gridSize/2; x++ {
// use bitwise operation to determine if a pixel should be colored
pixelOn := (hash[y]>>(x%8))&1 == 1

// image should
if pixelOn {
drawPixel(img, x, y, avatarColor, o.gridSize, o.size)
drawPixel(img, o.gridSize-1-x, y, avatarColor, o.gridSize, o.size) // mirror the pixel
} else {
drawPixel(img, x, y, bgColor, o.gridSize, o.size)
drawPixel(img, o.gridSize-1-x, y, bgColor, o.gridSize, o.size) // mirror the bg pixel
}
for l := 0; l < o.layers; l++ {
// derive hash for this layer
if l > 0 {
currentHash = generateHash(currentHash)
}

// determine color
var avatarColor color.NRGBA
if l < len(o.fgColors) {
avatarColor = o.fgColors[l]
// Check if color is empty/zero? defaultOptions sets index 0.
// WithLayerColor might extend with zeros.
// If alpha is 0, should we generate?
// User might purposefully set transparent? Unlikely for avatar foreground.
// Assuming if user sets it, they set it.
// But if we expanded with empty RGBA (0,0,0,0), it's invisible.
// If it is strictly 0,0,0,0, maybe fallback to hash?
// Let's assume user provides valid colors if they use WithLayerColor.
// But for "unspecified" layers where user requested 3 layers but provided 1 color:
if avatarColor == (color.NRGBA{}) {
avatarColor = color.NRGBA{currentHash[0], currentHash[1], currentHash[2], 255}
}
} else {
avatarColor = color.NRGBA{currentHash[0], currentHash[1], currentHash[2], 255}
}
// Draw the center column if gridSize is odd
if isOdd {
mid := o.gridSize / 2
pixelOn := (hash[y]>>(mid%8))&1 == 1
color := bgColor
if pixelOn {
color = avatarColor

// generate the pixel pattern
// loop over each pixel in the grid
for y := 0; y < o.gridSize; y++ {
for x := 0; x < o.gridSize/2; x++ {
// use bitwise operation to determine if a pixel should be colored
pixelOn := (currentHash[y]>>(x%8))&1 == 1

if pixelOn {
drawPixel(img, x, y, avatarColor, o.gridSize, o.size)
drawPixel(img, o.gridSize-1-x, y, avatarColor, o.gridSize, o.size) // mirror the pixel
}
}
// Draw the center column if gridSize is odd
if isOdd {
mid := o.gridSize / 2
pixelOn := (currentHash[y]>>(mid%8))&1 == 1
if pixelOn {
drawPixel(img, mid, y, avatarColor, o.gridSize, o.size)
}
}
drawPixel(img, mid, y, color, o.gridSize, o.size)
}
}

Expand Down
41 changes: 36 additions & 5 deletions goavatar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,43 @@ func expectedTopLeftPixel(input string, opts []OptFunc) (col color.Color) {
for _, opt := range opts {
opt(&conf)
}
// For the top‐left cell (x=0,y=0), the decision is based on the least‐significant bit of the raw hash character.
// Using the raw ASCII value of hash[0] as in the current implementation.
if (hash[0] & 1) == 1 {
return conf.fgColor

// Determine the final color at (0,0)
// It's cumulative. Background first.
// Then layer 0. If pixelOn, draw layer 0 color.
// Then layer 1. If pixelOn, draw layer 1 color.
// ...
// Since we overwrite, the LAST active layer wins.

finalColor := conf.bgColor

currentHash := hash

for l := 0; l < conf.layers; l++ {
if l > 0 {
currentHash = generateHash(currentHash)
}

// For the top‐left cell (x=0,y=0), the decision is based on the least‐significant bit of the raw hash character.
// Using the raw ASCII value of hash[0] as in the current implementation.
pixelOn := (currentHash[0] & 1) == 1

if pixelOn {
// determine color for this layer
var layerColor color.NRGBA
if l < len(conf.fgColors) {
layerColor = conf.fgColors[l]
if layerColor == (color.NRGBA{}) {
layerColor = color.NRGBA{currentHash[0], currentHash[1], currentHash[2], 255}
}
} else {
layerColor = color.NRGBA{currentHash[0], currentHash[1], currentHash[2], 255}
}
finalColor = layerColor
}
}
return conf.bgColor

return finalColor
}

func TestMake(t *testing.T) {
Expand Down