-
Notifications
You must be signed in to change notification settings - Fork 4
/
sixel.go
55 lines (49 loc) · 1 KB
/
sixel.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
package main
import (
"fmt"
"github.com/qpliu/qrencode-go/qrencode"
"io"
)
func printSixelQr(w io.Writer, grid *qrencode.BitGrid, inverse bool) {
black := "0"
white := "1"
fmt.Fprint(w,
"\x1BPq\"1;1",
"#", black, ";2;0;0;0",
"#", white, ";2;100;100;100",
)
if inverse {
black, white = white, black
}
height := grid.Height()
width := grid.Width()
line := "#" + white + "!" + fmt.Sprintf("%d", (width+2)*6) + "~"
fmt.Fprint(w, line, "-")
for y := 0; y < height; y++ {
fmt.Fprint(w, "#", white)
color := white
repeat := 6
var current string
for x := 0; x < width; x++ {
if grid.Get(x, y) {
current = black
} else {
current = white
}
if current != color {
fmt.Fprint(w, "#", color, "!", repeat, "~")
color = current
repeat = 0
}
repeat += 6
}
if color == white {
fmt.Fprintf(w, "#%s!%d~", white, repeat+6)
} else {
fmt.Fprintf(w, "#%s!%d~#%s!6~", color, repeat, white)
}
fmt.Fprint(w, "-")
}
fmt.Fprint(w, line)
fmt.Fprint(w, "\x1B\\")
}