aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: e2bd74b5d7a227ec119f235cb1d936dabd5c0e68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main

import (
	"bytes"
	"crypto/sha256"
	"encoding/base64"
	"flag"
	"fmt"
	"image"
	"image/color"
	"image/png"
	"regexp"
	"strings"
)

var usernamePattern = regexp.MustCompile("[a-zA-Z][a-zA-Z0-9]+")

func main() {
	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) == "" {
		flag.Usage()
		return
	}

	if !usernamePattern.MatchString(*username) {
		flag.Usage()
		return
	}

	seed := hash(*username)
	img := repeat(generateImage(seed))
	str, err := encodeImage(img)
	if err != nil {
		fmt.Printf("%v\n", err)
		return
	}
	fmt.Println(str)
}

func hash(username string) [32]byte {
	return sha256.Sum256([]byte(username))
}

func generateImage(hash [32]byte) image.Image {
	img := image.NewNRGBA(image.Rect(0, 0, 5, 5))
	colorA := color.NRGBA{
		R: hash[0],
		G: hash[1],
		B: hash[2],
		A: 255,
	}
	colorB := color.NRGBA{
		R: hash[3],
		G: hash[4],
		B: hash[5],
		A: 255,
	}
	offset := 6
	for x := 0; x < 5; x++ {
		for y := 0; y < 5; y++ {
			on := hash[offset] > 127
			color := colorA
			if on {
				color = colorB
			}
			img.Set(x, y, color)
			offset++
		}
	}
	return img
}

func encodeImage(img image.Image) (string, error) {
	var b bytes.Buffer
	if err := png.Encode(&b, img); err != nil {
		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 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++ {
// 		}
// 	}
// }