Skip to content
Merged
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ This package provides a simple way to generate unique, symmetric identicons base
<img src="./arts/avatar_5.png" width="100" alt="Avatar 5"/><br/>
<strong>EmberNexus23</strong>
</kbd>
&nbsp;&nbsp;&nbsp;&nbsp;
<kbd>
<img src="./arts/avatar_6.png" width="100" alt="Avatar 5"/><br/>
<strong>nice__user__name</strong>
</kbd>
</p>

## Installation
Expand Down Expand Up @@ -98,8 +103,16 @@ 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)
imgSlice = append(imgSlice, image1, image2, image3, image4, image5, image6)

// loop through the image slice and save the images
for i, img := range imgSlice {
Expand Down
10 changes: 9 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ 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)
imgSlice = append(imgSlice, image1, image2, image3, image4, image5, image6)

// loop through the image slice and save the images
for i, img := range imgSlice {
Expand Down
21 changes: 12 additions & 9 deletions goavatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type options struct {
}

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

// WithSize sets the width and height of the avatar minimum 64x64.
func WithSize(s int) optFunc {
func WithSize(s int) OptFunc {
return func(o *options) {
// insure that image should be at least 64x64
if s >= 64 {
Expand All @@ -29,21 +29,24 @@ func WithSize(s int) optFunc {
}

// WithGridSize sets the grid size of the avatar.
func WithGridSize(g int) optFunc {
func WithGridSize(g int) OptFunc {
return func(o *options) {
o.gridSize = g
// make sure grid is minimum 8 to make nice pattrens
if g > 8 {
o.gridSize = g
}
}
}

// WithBgColor sets the background color of the avatar.
func WithBgColor(r, g, b, a uint8) optFunc {
func WithBgColor(r, g, b, a uint8) OptFunc {
return func(o *options) {
o.bgColor = color.RGBA{r, g, b, a}
}
}

// WithFgColor sets the foreground color of the avatar.
func WithFgColor(r, g, b, a uint8) optFunc {
func WithFgColor(r, g, b, a uint8) OptFunc {
return func(o *options) {
o.fgColor = color.RGBA{r, g, b, a}
}
Expand All @@ -52,8 +55,8 @@ func WithFgColor(r, g, b, a uint8) optFunc {
// 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,
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
}
Expand All @@ -75,7 +78,7 @@ func drawPixel(img *image.RGBA, x, y int, c color.Color, pixelW, pixelH int) {
}

// Make generates an avatar image based on the input string and options.
func Make(input string, opts ...optFunc) image.Image {
func Make(input string, opts ...OptFunc) image.Image {
// generate the hash of an input
hash := generateHash(input)
o := defaultOptions(hash)
Expand Down
14 changes: 7 additions & 7 deletions goavatar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// and then using the same raw hash logic as in Make: for x=0, y=0, it tests if (hash[0] & 1) == 1.
//
// NOTE: generateHash returns a hex‑encoded string, so here we use its first character’s ASCII code.
func expectedTopLeftPixel(input string, opts []optFunc) (col color.Color) {
func expectedTopLeftPixel(input string, opts []OptFunc) (col color.Color) {
// generate the hash of the input
hash := generateHash(input)
// get the default configuration; which sets fgColor to {hash[0], hash[1], hash[2], 255}
Expand All @@ -30,7 +30,7 @@ func TestMake(t *testing.T) {
tests := []struct {
name string
input string
opts []optFunc
opts []OptFunc
width int
height int
}{
Expand All @@ -43,33 +43,33 @@ func TestMake(t *testing.T) {
{
name: "Custom width and height",
input: "custom-size",
opts: []optFunc{WithSize(512)},
opts: []OptFunc{WithSize(512)},
width: 512, height: 512,
},
{
name: "Custom background color",
input: "custom-bg",
// override background color only
opts: []optFunc{WithBgColor(255, 0, 0, 255)},
opts: []OptFunc{WithBgColor(255, 0, 0, 255)},
width: 64, height: 64,
},
{
name: "Custom foreground color",
input: "custom-fg",
// override foreground color only
opts: []optFunc{WithFgColor(10, 20, 30, 255)},
opts: []OptFunc{WithFgColor(10, 20, 30, 255)},
width: 64, height: 64,
},
{
name: "QuantumNomad42",
input: "QuantumNomad42",
opts: []optFunc{WithSize(512)},
opts: []OptFunc{WithSize(512)},
width: 512, height: 512,
},
{
name: "EchoFrost7",
input: "EchoFrost7",
opts: []optFunc{WithSize(512)},
opts: []OptFunc{WithSize(512)},
width: 512, height: 512,
},
}
Expand Down
Loading