mirror of
https://github.com/pcvolkmer/idicon.git
synced 2025-04-19 08:36:50 +00:00
Add methods to initialize icon generators
This commit is contained in:
parent
b84f291d2b
commit
18a1c2276b
@ -6,10 +6,20 @@ import (
|
||||
)
|
||||
|
||||
type GhIconGenerator struct {
|
||||
colorGenerator func([16]byte) color.RGBA
|
||||
}
|
||||
|
||||
func NewGhIconGenerator() *GhIconGenerator {
|
||||
return &GhIconGenerator{}
|
||||
}
|
||||
|
||||
func (generator *GhIconGenerator) WithColorGenerator(colorGenerator func([16]byte) color.RGBA) *GhIconGenerator {
|
||||
generator.colorGenerator = colorGenerator
|
||||
return generator
|
||||
}
|
||||
|
||||
// Based on https://github.com/dgraham/identicon
|
||||
func (generator *GhIconGenerator) GenIcon(id string, size int, f func([16]byte) color.RGBA) *image.NRGBA {
|
||||
func (generator *GhIconGenerator) GenIcon(id string, size int) *image.NRGBA {
|
||||
if size > 512 {
|
||||
size = 512
|
||||
}
|
||||
@ -29,7 +39,7 @@ func (generator *GhIconGenerator) GenIcon(id string, size int, f func([16]byte)
|
||||
}
|
||||
}
|
||||
|
||||
return drawImage(mirrorData(data, blocks), blocks, size, f(hash))
|
||||
return drawImage(mirrorData(data, blocks), blocks, size, generator.colorGenerator(hash))
|
||||
}
|
||||
|
||||
// https://processing.org/reference/map_.html
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type IconGenerator interface {
|
||||
GenIcon(id string, size int, f func([16]byte) color.RGBA) *image.NRGBA
|
||||
GenIcon(id string, size int) *image.NRGBA
|
||||
}
|
||||
|
||||
func HashBytes(id string) [16]byte {
|
||||
|
@ -7,9 +7,19 @@ import (
|
||||
)
|
||||
|
||||
type IdIconGenerator struct {
|
||||
colorGenerator func([16]byte) color.RGBA
|
||||
}
|
||||
|
||||
func (generator *IdIconGenerator) GenIcon(id string, size int, f func([16]byte) color.RGBA) *image.NRGBA {
|
||||
func NewIdIconGenerator() *IdIconGenerator {
|
||||
return &IdIconGenerator{}
|
||||
}
|
||||
|
||||
func (generator *IdIconGenerator) WithColorGenerator(colorGenerator func([16]byte) color.RGBA) *IdIconGenerator {
|
||||
generator.colorGenerator = colorGenerator
|
||||
return generator
|
||||
}
|
||||
|
||||
func (generator *IdIconGenerator) GenIcon(id string, size int) *image.NRGBA {
|
||||
id = strings.ToLower(id)
|
||||
blocks := 5
|
||||
if size > 512 {
|
||||
@ -21,7 +31,7 @@ func (generator *IdIconGenerator) GenIcon(id string, size int, f func([16]byte)
|
||||
for i := 0; i < len(hash)-1; i++ {
|
||||
data[i] = hash[i]%2 != hash[i+1]%2
|
||||
}
|
||||
return drawImage(mirrorData(data, blocks), blocks, size, f(hash))
|
||||
return drawImage(mirrorData(data, blocks), blocks, size, generator.colorGenerator(hash))
|
||||
}
|
||||
|
||||
func ColorV1(hash [16]byte) color.RGBA {
|
||||
|
@ -7,13 +7,13 @@ import (
|
||||
)
|
||||
|
||||
func TestIgnoreCase(t *testing.T) {
|
||||
iconGenerator := IdIconGenerator{}
|
||||
|
||||
w1 := bytes.NewBuffer([]byte{})
|
||||
png.Encode(w1, iconGenerator.GenIcon("example", 80, ColorV1))
|
||||
ig1 := NewIdIconGenerator().WithColorGenerator(ColorV1)
|
||||
png.Encode(w1, ig1.GenIcon("example", 80))
|
||||
|
||||
w2 := bytes.NewBuffer([]byte{})
|
||||
png.Encode(w2, iconGenerator.GenIcon("Example", 80, ColorV1))
|
||||
ig2 := NewIdIconGenerator().WithColorGenerator(ColorV1)
|
||||
png.Encode(w2, ig2.GenIcon("Example", 80))
|
||||
|
||||
if bytes.Compare(w1.Bytes(), w2.Bytes()) != 0 {
|
||||
t.Errorf("resulting images do not match")
|
||||
@ -21,14 +21,14 @@ func TestIgnoreCase(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStringMatchesHash(t *testing.T) {
|
||||
iconGenerator := IdIconGenerator{}
|
||||
|
||||
w1 := bytes.NewBuffer([]byte{})
|
||||
ig1 := NewIdIconGenerator().WithColorGenerator(ColorV2)
|
||||
// MD5 of lowercase 'example'
|
||||
png.Encode(w1, iconGenerator.GenIcon("1a79a4d60de6718e8e5b326e338ae533", 80, ColorV2))
|
||||
png.Encode(w1, ig1.GenIcon("1a79a4d60de6718e8e5b326e338ae533", 80))
|
||||
|
||||
w2 := bytes.NewBuffer([]byte{})
|
||||
png.Encode(w2, iconGenerator.GenIcon("Example", 80, ColorV2))
|
||||
ig2 := NewIdIconGenerator().WithColorGenerator(ColorV2)
|
||||
png.Encode(w2, ig2.GenIcon("Example", 80))
|
||||
|
||||
if bytes.Compare(w1.Bytes(), w2.Bytes()) != 0 {
|
||||
t.Errorf("resulting images do not match")
|
||||
|
@ -53,13 +53,12 @@ func requestHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var iconGenerator icons.IconGenerator
|
||||
if pattern == "github" {
|
||||
iconGenerator = &icons.GhIconGenerator{}
|
||||
|
||||
iconGenerator = icons.NewGhIconGenerator().WithColorGenerator(cFunc)
|
||||
} else {
|
||||
iconGenerator = &icons.IdIconGenerator{}
|
||||
iconGenerator = icons.NewIdIconGenerator().WithColorGenerator(cFunc)
|
||||
}
|
||||
|
||||
err = png.Encode(w, iconGenerator.GenIcon(id, size, cFunc))
|
||||
err = png.Encode(w, iconGenerator.GenIcon(id, size))
|
||||
}
|
||||
|
||||
var (
|
||||
|
Loading…
x
Reference in New Issue
Block a user