Add drawPng function to Renderer for PNG icon rendering
- Takes image.Image, position (x,y) in Dp, size (width,height) in Dp - Tint color with alpha < 255 applies color overlay using clip.Rect - Converts Dp to pixels using Renderer.toPx() - Uses paint.NewImageOp for image rendering
This commit is contained in:
parent
fe3e7c3d75
commit
106e91832a
|
|
@ -369,3 +369,35 @@ type WindowConstraints struct {
|
|||
Min image.Point
|
||||
Max image.Point
|
||||
}
|
||||
|
||||
// drawPng draws a PNG image at the given position and size.
|
||||
// x, y are in Dp (top-left corner).
|
||||
// width, height are in Dp (target size).
|
||||
// col is the tint color — if alpha < 255, the image is tinted using blendOp.DstIn.
|
||||
func (r *Renderer) drawPng(gtx layout.Context, img image.Image, x, y Dp, width, height Dp, col color.NRGBA) {
|
||||
if img == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Convert Dp to pixels
|
||||
xPx := int(r.toPx(x))
|
||||
yPx := int(r.toPx(y))
|
||||
wPx := int(r.toPx(width))
|
||||
hPx := int(r.toPx(height))
|
||||
|
||||
// Compute destination rectangle
|
||||
dst := image.Rect(xPx, yPx, xPx+wPx, yPx+hPx)
|
||||
|
||||
// If tint color has alpha < 255, apply tint using blendOp.DstIn
|
||||
if col.A < 255 {
|
||||
// Draw tinted rectangle over the image
|
||||
bgClip := clip.Rect(dst).Push(gtx.Ops)
|
||||
paint.ColorOp{Color: col}.Add(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
bgClip.Pop()
|
||||
}
|
||||
|
||||
// Draw the image
|
||||
paint.NewImageOp(img).Add(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user