Files
beatglow/app/helper.go
T
2026-10-29 17:29:41 +01:00

77 lines
1.6 KiB
Go

package app
import (
"image"
"image/color"
"image/draw"
"github.com/BeatGlow/display/pixel"
)
// BufferFor creates a new image buffer for the provided display matching its color model.
func BufferFor(ctx Context, size image.Point) draw.Image {
switch ctx.Display().ColorModel() {
case pixel.MonoModel:
return pixel.NewMonoImage(size.X, size.Y)
case pixel.Gray2Model:
return pixel.NewGray2Image(size.X, size.Y)
case pixel.Gray4Model:
return pixel.NewGray4Image(size.X, size.Y)
case pixel.CRGB15Model:
return pixel.NewCRGB15Image(size.X, size.Y)
case pixel.CRGB16Model:
return pixel.NewCRGB16Image(size.X, size.Y)
default:
return nil
}
}
type clearer interface {
Clear()
}
// ImageCopy copies an image over dst.
func ImageCopy(dst draw.Image, src image.Image) {
draw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Src)
}
// ImageCopyBounds copies an image over dst bounded by r.
func ImageCopyBounds(dst draw.Image, src image.Image, r image.Rectangle) {
draw.Draw(dst, r, src, image.Point{}, draw.Src)
}
// Clear an image.
func ImageClear(dst draw.Image) {
if c, ok := dst.(clearer); ok {
c.Clear()
return
}
ImageFill(dst, color.Black)
}
type filler interface {
Fill(color.Color)
}
// Fill an imagre with a single color.
func ImageFill(dst draw.Image, c color.Color) {
if f, ok := dst.(filler); ok {
f.Fill(c)
return
}
var src image.Image
switch c {
case color.Black:
src = image.Black
case color.White:
src = image.White
case color.Transparent:
src = image.Transparent
default:
src = image.NewUniform(c)
}
draw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Src)
}