mirror of
https://github.com/pcvolkmer/idicon.git
synced 2025-07-05 21:12:54 +00:00
Move icon generation into dedicated files
This commit is contained in:
64
icons/icons.go
Normal file
64
icons/icons.go
Normal file
@ -0,0 +1,64 @@
|
||||
package icons
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type IconGenerator interface {
|
||||
GenIcon(id string, size int, f func([16]byte) color.RGBA) *image.NRGBA
|
||||
}
|
||||
|
||||
func HashBytes(id string) [16]byte {
|
||||
hash := [16]byte{}
|
||||
md5RegExp := regexp.MustCompile("[a-f0-9]{32}")
|
||||
if !md5RegExp.Match([]byte(id)) {
|
||||
hash = md5.Sum([]byte(id))
|
||||
} else {
|
||||
dec, _ := hex.DecodeString(id)
|
||||
for idx, b := range dec {
|
||||
hash[idx] = b
|
||||
}
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
||||
func mirrorData(data []bool, blocks int) []bool {
|
||||
for x := 0; x < blocks; x++ {
|
||||
min := x*blocks + 1
|
||||
for y := 0; y < blocks; y++ {
|
||||
a := ((blocks - x - 1) * blocks) + y
|
||||
b := min + y - 1
|
||||
if data[a] {
|
||||
data[b] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func drawImage(data []bool, blocks int, size int, c color.Color) *image.NRGBA {
|
||||
img := image.NewNRGBA(image.Rect(0, 0, size, size))
|
||||
|
||||
draw.Draw(img, img.Bounds(), &image.Uniform{color.Gray{240}}, image.Point{0, 0}, draw.Src)
|
||||
|
||||
blockSize := size / (blocks + 1)
|
||||
border := (size - (blocks * blockSize)) / 2
|
||||
|
||||
for x := border; x < blockSize*blocks+border; x++ {
|
||||
bx := (x - border) / blockSize
|
||||
for y := border; y < blockSize*blocks+border; y++ {
|
||||
by := (y - border) / blockSize
|
||||
idx := bx*blocks + by
|
||||
if data[idx] && (bx < blocks || by < blocks) {
|
||||
img.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return img
|
||||
}
|
Reference in New Issue
Block a user