From 6ccb614b0815efb745de72f7dd12f6c95e24dcae Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 18 Jul 2020 21:51:52 +0900 Subject: [PATCH] graphicsdriver/metal: Implement CompositeModeMultiply Updates #410 --- image_test.go | 28 +++++++++++++++++++++++ internal/graphicsdriver/metal/graphics.go | 2 ++ 2 files changed, 30 insertions(+) diff --git a/image_test.go b/image_test.go index 7481bf249..08d0b3f9a 100644 --- a/image_test.go +++ b/image_test.go @@ -2199,3 +2199,31 @@ func TestImageReplacePixelsAndModifyPixels(t *testing.T) { } } } + +func TestImageCompositeModeMultiply(t *testing.T) { + const w, h = 16, 16 + dst, _ := NewImage(w, h, FilterDefault) + src, _ := NewImage(w, h, FilterDefault) + + dst.Fill(color.RGBA{0x01, 0x02, 0x03, 0x04}) + src.Fill(color.RGBA{0x05, 0x06, 0x07, 0x08}) + + op := &DrawImageOptions{} + op.CompositeMode = CompositeModeMultiply + dst.DrawImage(src, op) + + for j := 0; j < h; j++ { + for i := 0; i < w; i++ { + got := dst.At(i, j).(color.RGBA) + want := color.RGBA{ + R: byte(math.Floor((0x01 / 255.0) * (0x05 / 255.0) * 255)), + G: byte(math.Floor((0x02 / 255.0) * (0x06 / 255.0) * 255)), + B: byte(math.Floor((0x03 / 255.0) * (0x07 / 255.0) * 255)), + A: byte(math.Floor((0x04 / 255.0) * (0x08 / 255.0) * 255)), + } + if got != want { + t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want) + } + } + } +} diff --git a/internal/graphicsdriver/metal/graphics.go b/internal/graphicsdriver/metal/graphics.go index 71dde5bc5..ed102a28c 100644 --- a/internal/graphicsdriver/metal/graphics.go +++ b/internal/graphicsdriver/metal/graphics.go @@ -571,6 +571,8 @@ func (g *Graphics) Reset() error { return mtl.BlendFactorOneMinusSourceAlpha case driver.OneMinusDstAlpha: return mtl.BlendFactorOneMinusDestinationAlpha + case driver.DstColor: + return mtl.BlendFactorDestinationColor default: panic(fmt.Sprintf("metal: invalid operation: %d", c)) }