diff options
-rw-r--r-- | main.go | 48 |
1 files changed, 33 insertions, 15 deletions
@@ -16,24 +16,21 @@ import ( var usernamePattern = regexp.MustCompile("[a-zA-Z][a-zA-Z0-9]+") func main() { - username := flag.String("username", "", "username to encode as image") + username := flag.String("username", "", "username to encode as image (must contain only letters a-zA-Z0-9 and start with a letter)") flag.Parse() if strings.TrimSpace(*username) == "" { - fmt.Println("no username") + flag.Usage() return } + if !usernamePattern.MatchString(*username) { - fmt.Println("no match") + flag.Usage() return } seed := hash(*username) - err, img := generateImage(seed) - if err != nil { - fmt.Printf("%v\n", err) - return - } - err, str := encodeImage(img) + img := repeat(generateImage(seed)) + str, err := encodeImage(img) if err != nil { fmt.Printf("%v\n", err) return @@ -45,7 +42,7 @@ func hash(username string) [32]byte { return sha256.Sum256([]byte(username)) } -func generateImage(hash [32]byte) (error, image.Image) { +func generateImage(hash [32]byte) image.Image { img := image.NewNRGBA(image.Rect(0, 0, 5, 5)) colorA := color.NRGBA{ R: hash[0], @@ -62,7 +59,7 @@ func generateImage(hash [32]byte) (error, image.Image) { offset := 6 for x := 0; x < 5; x++ { for y := 0; y < 5; y++ { - on := hash[offset]&0xf0 > 0 + on := hash[offset] > 127 color := colorA if on { color = colorB @@ -71,13 +68,34 @@ func generateImage(hash [32]byte) (error, image.Image) { offset++ } } - return nil, img + return img } -func encodeImage(img image.Image) (error, string) { +func encodeImage(img image.Image) (string, error) { var b bytes.Buffer if err := png.Encode(&b, img); err != nil { - return err, "" + return "", err + } + return base64.StdEncoding.EncodeToString(b.Bytes()), nil +} + +func repeat(img image.Image) image.Image { + newImg := image.NewNRGBA(image.Rect(0, 0, 10, 10)) + for x := 0; x < 5; x++ { + for y := 0; y < 5; y++ { + newImg.Set(x, y, img.At(x, y)) + newImg.Set(9-x, y, img.At(x, y)) + newImg.Set(x, 9-y, img.At(x, y)) + newImg.Set(9-x, 9-y, img.At(x, y)) + } } - return nil, base64.StdEncoding.EncodeToString(b.Bytes()) + return newImg } + +// func makeBigger(img image.Image) image.Image { +// newImg := image.NewNRGBA(image.Rect(0, 0, 200, 200)) +// for x := 0; x < 10; x++ { +// for y := 0; y < 10; y++ { +// } +// } +// } |