From 92d8562b1d53bf209562dd2a701319b6f4cac1db Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Fri, 20 Aug 2021 23:15:48 -0700 Subject: [PATCH] ebiten: Add (*Image).RGBA64At (#1773) Closes #1769. --- image.go | 16 ++++++++++++++++ image_test.go | 17 ++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/image.go b/image.go index a7c9db72e..a860cc454 100644 --- a/image.go +++ b/image.go @@ -708,6 +708,22 @@ func (i *Image) At(x, y int) color.Color { return color.RGBA{pix[0], pix[1], pix[2], pix[3]} } +// RGBA64At implements image.RGBA64Image's RGBA64At. +// +// RGBA64At loads pixels from GPU to system memory if necessary, which means +// that RGBA64At can be slow. +// +// RGBA64At always returns a transparent color if the image is disposed. +// +// Note that an important logic should not rely on values returned by RGBA64At, +// since the returned values can include very slight differences between some machines. +// +// RGBA64At can't be called outside the main loop (ebiten.Run's updating function) starts. +func (i *Image) RGBA64At(x, y int) color.RGBA64 { + r, g, b, a := i.At(x, y).(color.RGBA).RGBA() + return color.RGBA64{uint16(r), uint16(g), uint16(b), uint16(a)} +} + // Set sets the color at (x, y). // // Set loads pixels from GPU to system memory if necessary, which means that Set can be slow. diff --git a/image_test.go b/image_test.go index c5ba7f7d2..97ea0929e 100644 --- a/image_test.go +++ b/image_test.go @@ -1582,18 +1582,29 @@ func TestImageAtAfterDisposingSubImage(t *testing.T) { img.Set(0, 0, color.White) img.SubImage(image.Rect(0, 0, 16, 16)) runtime.GC() - got := img.At(0, 0) + want := color.RGBA{0xff, 0xff, 0xff, 0xff} + want64 := color.RGBA64{0xffff, 0xffff, 0xffff, 0xffff} + got := img.At(0, 0) if got != want { - t.Errorf("got: %v, want: %v", got, want) + t.Errorf("At(0,0) got: %v, want: %v", got, want) + } + got = img.RGBA64At(0, 0) + if got != want64 { + t.Errorf("RGBA64At(0,0) got: %v, want: %v", got, want) } img.Set(0, 1, color.White) sub := img.SubImage(image.Rect(0, 0, 16, 16)).(*Image) sub.Dispose() + got = img.At(0, 1) if got != want { - t.Errorf("got: %v, want: %v", got, want) + t.Errorf("At(0,1) got: %v, want: %v", got, want64) + } + got = img.RGBA64At(0, 1) + if got != want64 { + t.Errorf("RGBA64At(0,1) got: %v, want: %v", got, want64) } }