From d63590a799c484b10b080637c869793fa23bc867 Mon Sep 17 00:00:00 2001 From: faiface Date: Wed, 8 Mar 2017 16:37:57 +0100 Subject: [PATCH] add PictureColor properties to Canvas TargetPicture --- pixelgl/canvas.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/pixelgl/canvas.go b/pixelgl/canvas.go index 567d824..1bf67af 100644 --- a/pixelgl/canvas.go +++ b/pixelgl/canvas.go @@ -122,7 +122,8 @@ func (c *Canvas) MakePicture(p pixel.Picture) pixel.TargetPicture { }) cp := &canvasPicture{ - tex: tex, + tex: tex, + pixels: pixels, borders: pixel.R( float64(bx), float64(by), float64(bw), float64(bh), @@ -353,6 +354,7 @@ func (ct *canvasTriangles) Draw() { type canvasPicture struct { tex *glhf.Texture + pixels []uint8 borders pixel.Rect bounds pixel.Rect @@ -375,6 +377,21 @@ func (cp *canvasPicture) Original() pixel.Picture { return cp.orig } +func (cp *canvasPicture) Color(at pixel.Vec) pixel.NRGBA { + if !cp.bounds.Contains(at) { + return pixel.NRGBA{} + } + bx, by, bw, _ := intBounds(cp.borders) + x, y := int(at.X())-bx, int(at.Y())-by + off := y*bw + x + return pixel.NRGBA{ + R: float64(cp.pixels[off*4+0]) / 255, + G: float64(cp.pixels[off*4+1]) / 255, + B: float64(cp.pixels[off*4+2]) / 255, + A: float64(cp.pixels[off*4+3]) / 255, + } +} + func (cp *canvasPicture) Draw(t pixel.TargetTriangles) { ct := t.(*canvasTriangles) if cp.dst != ct.dst { @@ -404,6 +421,13 @@ func (ccp *canvasCanvasPicture) Original() pixel.Picture { return ccp.orig } +func (ccp *canvasCanvasPicture) Color(at pixel.Vec) pixel.NRGBA { + if !ccp.bounds.Contains(at) { + return pixel.NRGBA{} + } + return ccp.src.Color(at) +} + func (ccp *canvasCanvasPicture) Draw(t pixel.TargetTriangles) { ct := t.(*canvasTriangles) if ccp.dst != ct.dst {