-
Notifications
You must be signed in to change notification settings - Fork 0
/
toimage_test.go
53 lines (43 loc) · 1.61 KB
/
toimage_test.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
package vt100
import (
"image/color"
"testing"
)
func TestToImage(t *testing.T) {
// Character dimensions in pixels
charWidth, charHeight := 8, 8
// Initialize vt100 terminal settings
Init()
defer Close()
// Create a new canvas
canvas := NewCanvas()
textWidth, textHeight := canvas.Size()
// Calculate expected image dimensions
expectedImgWidth, expectedImgHeight := int(textWidth)*charWidth, int(textHeight)*charHeight
// Draw something on the canvas
x, y := 10, 5 // Example coordinates
canvas.chars[uint(y)*canvas.w+uint(x)].r = 'X'
canvas.chars[uint(y)*canvas.w+uint(x)].fg = []byte{31} // Red foreground
// Generate the image
img, err := canvas.ToImage()
if err != nil {
t.Fatalf("Failed to generate image: %v", err)
}
// Check the dimensions of the generated image
if img.Bounds().Dx() != expectedImgWidth || img.Bounds().Dy() != expectedImgHeight {
t.Errorf("Image dimensions are incorrect. Got %dx%d, want %dx%d", img.Bounds().Dx(), img.Bounds().Dy(), expectedImgWidth, expectedImgHeight)
}
// Check if the pixel at the specified character position has the expected color
// Adjusting pixel position to account for character dimensions
pixelX, pixelY := x*charWidth, y*charHeight
expectedColor := ansiCodeToColor([]byte{31}, true) // Red
actualColor := img.At(pixelX, pixelY)
if !colorsAreEqual(expectedColor, actualColor) {
t.Errorf("Pixel color at (%d, %d) is incorrect. Got %v, want %v", pixelX, pixelY, actualColor, expectedColor)
}
}
func colorsAreEqual(c1, c2 color.Color) bool {
r1, g1, b1, a1 := c1.RGBA()
r2, g2, b2, a2 := c2.RGBA()
return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2
}