tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/pixel/image_test.go (about)

     1  package pixel_test
     2  
     3  import (
     4  	"image/color"
     5  	"testing"
     6  
     7  	"tinygo.org/x/drivers/pixel"
     8  )
     9  
    10  func TestImageRGB565BE(t *testing.T) {
    11  	image := pixel.NewImage[pixel.RGB565BE](5, 3)
    12  	if width, height := image.Size(); width != 5 && height != 3 {
    13  		t.Errorf("image.Size(): expected 5, 3 but got %d, %d", width, height)
    14  	}
    15  	for _, c := range []color.RGBA{
    16  		{R: 0xff, A: 0xff},
    17  		{G: 0xff, A: 0xff},
    18  		{B: 0xff, A: 0xff},
    19  		{R: 0x10, A: 0xff},
    20  		{G: 0x10, A: 0xff},
    21  		{B: 0x10, A: 0xff},
    22  	} {
    23  		image.Set(4, 2, pixel.NewColor[pixel.RGB565BE](c.R, c.G, c.B))
    24  		c2 := image.Get(4, 2).RGBA()
    25  		if c2 != c {
    26  			t.Errorf("failed to roundtrip color: expected %v but got %v", c, c2)
    27  		}
    28  	}
    29  }
    30  
    31  func TestImageRGB444BE(t *testing.T) {
    32  	image := pixel.NewImage[pixel.RGB444BE](5, 3)
    33  	if width, height := image.Size(); width != 5 && height != 3 {
    34  		t.Errorf("image.Size(): expected 5, 3 but got %d, %d", width, height)
    35  	}
    36  	for _, c := range []color.RGBA{
    37  		{R: 0xff, A: 0xff},
    38  		{G: 0xff, A: 0xff},
    39  		{B: 0xff, A: 0xff},
    40  		{R: 0x11, A: 0xff},
    41  		{G: 0x11, A: 0xff},
    42  		{B: 0x11, A: 0xff},
    43  	} {
    44  		encoded := pixel.NewColor[pixel.RGB444BE](c.R, c.G, c.B)
    45  		image.Set(0, 0, encoded)
    46  		image.Set(0, 1, encoded)
    47  		encoded2 := image.Get(0, 0)
    48  		encoded3 := image.Get(0, 1)
    49  		if encoded != encoded2 {
    50  			t.Errorf("failed to roundtrip color %v: expected %d but got %d", c, encoded, encoded2)
    51  		}
    52  		if encoded != encoded3 {
    53  			t.Errorf("failed to roundtrip color %v: expected %d but got %d", c, encoded, encoded3)
    54  		}
    55  		c2 := encoded2.RGBA()
    56  		if c2 != c {
    57  			t.Errorf("failed to roundtrip color: expected %v but got %v", c, c2)
    58  		}
    59  		c3 := encoded3.RGBA()
    60  		if c3 != c {
    61  			t.Errorf("failed to roundtrip color: expected %v but got %v", c, c3)
    62  		}
    63  	}
    64  }
    65  
    66  func TestImageMonochrome(t *testing.T) {
    67  	image := pixel.NewImage[pixel.Monochrome](5, 3)
    68  	if width, height := image.Size(); width != 5 && height != 3 {
    69  		t.Errorf("image.Size(): expected 5, 3 but got %d, %d", width, height)
    70  	}
    71  	for _, expected := range []color.RGBA{
    72  		{R: 0xff, G: 0xff, B: 0xff},
    73  		{G: 0xff},
    74  		{R: 0xff, G: 0xff},
    75  		{G: 0xff, B: 0xff},
    76  		{R: 0x00},
    77  		{G: 0x00, A: 0xff},
    78  		{B: 0x00, A: 0xff},
    79  	} {
    80  		encoded := pixel.NewColor[pixel.Monochrome](expected.R, expected.G, expected.B)
    81  		image.Set(4, 2, encoded)
    82  		actual := image.Get(4, 2).RGBA()
    83  		switch {
    84  		case expected.R == 0 && expected.G == 0 && expected.B == 0:
    85  			// should be false eg black
    86  			if actual.R != 0 || actual.G != 0 || actual.B != 0 {
    87  				t.Errorf("failed to roundtrip color: expected %v but got %v", expected, actual)
    88  			}
    89  		case int(expected.R)+int(expected.G)+int(expected.B) > 128*3:
    90  			// should be true eg white
    91  			if actual.R == 0 || actual.G == 0 || actual.B == 0 {
    92  				t.Errorf("failed to roundtrip color: expected %v but got %v", expected, actual)
    93  			}
    94  		}
    95  	}
    96  }