diff --git a/README.md b/README.md index 62b3d38..5d4551d 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,11 @@ This package provides a simple way to generate unique, symmetric identicons base Avatar 5
EmberNexus23 +      + + Avatar 5
+ nice__user__name +

## Installation @@ -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 { diff --git a/example/main.go b/example/main.go index 6b6ebe4..2ef368d 100644 --- a/example/main.go +++ b/example/main.go @@ -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 { diff --git a/goavatar.go b/goavatar.go index 2a9561a..3a0ca73 100644 --- a/goavatar.go +++ b/goavatar.go @@ -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 { @@ -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} } @@ -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 } @@ -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) diff --git a/goavatar_test.go b/goavatar_test.go index 188aa33..2c73207 100644 --- a/goavatar_test.go +++ b/goavatar_test.go @@ -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} @@ -30,7 +30,7 @@ func TestMake(t *testing.T) { tests := []struct { name string input string - opts []optFunc + opts []OptFunc width int height int }{ @@ -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, }, }