-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
qrcon.go
63 lines (49 loc) · 1.25 KB
/
qrcon.go
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
// Package qrcon implements a QR Code ANSI printer for displaying QR codes on
// the console.
package qrcon
import (
"fmt"
"image/color"
"strings"
"github.com/skip2/go-qrcode"
)
const (
black = "\033[40m \033[0m"
white = "\033[47m \033[0m"
)
// ErrUnknowColor is returned when the color is unknown.
var ErrUnknowColor = fmt.Errorf("unknown color")
// QRCode returns a string containing an ANSI encoded
// QR Code.
func QRCode(content string) (string, error) {
q, err := qrcode.New(content, qrcode.Medium)
if err != nil {
return "", fmt.Errorf("failed to create qr code: %w", err)
}
var sb strings.Builder
i := q.Image(0)
b := i.Bounds()
for x := b.Min.X; x < b.Max.X; x++ {
for y := b.Min.Y; y < b.Max.Y; y++ {
col := i.At(x, y)
switch {
case sameColor(col, q.ForegroundColor):
_, _ = sb.WriteString(black)
case sameColor(col, q.BackgroundColor):
_, _ = sb.WriteString(white)
default:
return "", fmt.Errorf("error at (%d,%d): %+v: %w", x, y, col, ErrUnknowColor)
}
}
_, _ = sb.WriteString("\n")
}
return sb.String(), nil
}
func sameColor(a color.Color, b color.Color) bool {
r1, g1, b1, a1 := a.RGBA()
r2, g2, b2, a2 := b.RGBA()
if r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2 {
return true
}
return false
}