summaryrefslogtreecommitdiff
path: root/main.go
blob: 9e8b22bdfe1c72d6c275041304ad3bb0b008bde2 (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
102
103
104
105
106
107
108
109
110
111
112
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 69):
 * <benjamin@chausse.xyz> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Benjamin Chausse
 * ----------------------------------------------------------------------------
 */

package main

import (
	"fmt"
	"io"
	"os"
)

const (
	topFmt  = "\033[38;2;%d;%d;%dm"
	botFmt  = "\033[48;2;%d;%d;%dm"
	postFmt = "▀\033[0m"
)

type pixelArt struct {
	height uint32
	width  uint32
	pixels []uint32
}

type pixel struct {
	r uint8
	g uint8
	b uint8
}

func int2Pixel(src uint32) pixel {
	return pixel{
		b: uint8((src) & 0xFF),
		g: uint8((src >> 8) & 0xFF),
		r: uint8((src >> 16) & 0xFF),
	}
}

type drawing struct {
	height int
	width  int
	pixels []pixel
}

func mkPixelStack(top, bot pixel) string {
	noTop := (int(top.r+top.g+top.b) == 0)
	noBot := (int(bot.r+bot.g+bot.b) == 0)
	switch {
	case noTop && noBot:
		return " "
	case noTop:
		return fmt.Sprintf(botFmt+postFmt,
			bot.r, bot.g, bot.b)
	case noBot:
		return fmt.Sprintf(topFmt+postFmt,
			top.r, top.g, top.b)
	default:
		return fmt.Sprintf(topFmt+botFmt+postFmt,
			top.r, top.g, top.b,
			bot.r, bot.g, bot.b,
		)
	}
}

func (p pixelArt) draw(stream io.Writer) {
	step := uint32(p.width * 2)

	topIdx, botIdx := uint32(0), p.width
	row := uint32(0)

	top, bot := make([]pixel, p.width), make([]pixel, p.width)

	if p.height%2 != 0 {
		for i := range top {
			top[i] = pixel{0, 0, 0}
			bot[i] = int2Pixel(p.pixels[0])
		}
		drawRowStack(top, bot)
		topIdx += step
		botIdx += step
		row++
	}

	for row < p.height/2 {
		for i := range p.width {
			top[i] = int2Pixel(p.pixels[topIdx+i])
			bot[i] = int2Pixel(p.pixels[botIdx+i])
		}
		io.WriteString(stream, drawRowStack(top, bot))
		topIdx += step
		botIdx += step
		row++
	}
}

func drawRowStack(top, bot []pixel) string {
	var str string
	for i := 0; i < len(top); i++ {
		str += mkPixelStack(top[i], bot[i])
	}
	return str + "\n"
}

func main() {
	vim_logo := pixelArt{height: 72, width: 72, pixels: vim_data}
	vim_logo.draw(os.Stdout)
}