add PictureColor properties to Canvas TargetPicture

This commit is contained in:
faiface 2017-03-08 16:37:57 +01:00
parent 79bffcc7e8
commit d63590a799
1 changed files with 25 additions and 1 deletions

View File

@ -123,6 +123,7 @@ func (c *Canvas) MakePicture(p pixel.Picture) pixel.TargetPicture {
cp := &canvasPicture{ cp := &canvasPicture{
tex: tex, tex: tex,
pixels: pixels,
borders: pixel.R( borders: pixel.R(
float64(bx), float64(by), float64(bx), float64(by),
float64(bw), float64(bh), float64(bw), float64(bh),
@ -353,6 +354,7 @@ func (ct *canvasTriangles) Draw() {
type canvasPicture struct { type canvasPicture struct {
tex *glhf.Texture tex *glhf.Texture
pixels []uint8
borders pixel.Rect borders pixel.Rect
bounds pixel.Rect bounds pixel.Rect
@ -375,6 +377,21 @@ func (cp *canvasPicture) Original() pixel.Picture {
return cp.orig 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) { func (cp *canvasPicture) Draw(t pixel.TargetTriangles) {
ct := t.(*canvasTriangles) ct := t.(*canvasTriangles)
if cp.dst != ct.dst { if cp.dst != ct.dst {
@ -404,6 +421,13 @@ func (ccp *canvasCanvasPicture) Original() pixel.Picture {
return ccp.orig 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) { func (ccp *canvasCanvasPicture) Draw(t pixel.TargetTriangles) {
ct := t.(*canvasTriangles) ct := t.(*canvasTriangles)
if ccp.dst != ct.dst { if ccp.dst != ct.dst {