-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermimage.td
79 lines (64 loc) · 1.63 KB
/
termimage.td
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
import "color"
import "math"
import "canvas"
import "image"
block := "█"
COLORS := [
[000, 000, 000], [255, 000, 000], [000, 255, 000], [255, 255, 000],
[000, 000, 255], [255, 000, 255], [000, 255, 255], [255, 255, 255],
[128, 128, 128], [128, 000, 000], [000, 128, 000], [128, 128, 000],
[000, 000, 128], [128, 000, 128], [000, 128, 128], [192, 192, 192]
]
ANSI_COLORS := [
color.black, color.ired, color.igreen, color.iyellow,
color.iblue, color.ipurple, color.icyan, color.iwhite,
color.iblack, color.red, color.green, color.yellow,
color.blue, color.purple, color.cyan, color.white
]
color_distance := fn(color1, color2) {
dr := color1[0] - color2[0]
dg := color1[1] - color2[1]
db := color1[2] - color2[2]
return math.sqrt(dr * dr + dg * dg + db * db)
}
fn termcolor(rgb) {
// Find the index of the nearest color
minDistance := 100000000000000000
minIndex := 0;
for i := 0; i < len(ANSI_COLORS); i++ {
distance := color_distance(rgb, COLORS[i])
if distance < minDistance {
minDistance = distance;
minIndex = i
}
}
return ANSI_COLORS[minIndex]
}
to_ascii := fn (img, width, height, colored) {
im := image.decode(img)
w := im.bounds().size.width
h := im.bounds().size.height
ctx := canvas.new_context(width, height)
ctx.scale(float(width)/w, float(height)/h)
ctx.drawimage(img, 0, 0)
img := ctx.image()
result := ""
for j := 0; j < height; j++ {
str := ""
for i := 0; i < width; i++ {
rgb := img.at(i, j)
if colored {
str += termcolor(rgb)
}
str += block
}
result += str + "\n"
}
return result + color.reset
}
export {
to_ascii : to_ascii,
set_block : fn(b) {
block = b
}
}