58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package ui
|
|
|
|
import (
|
|
"bytes"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
|
|
"github.com/BeatGlow/display"
|
|
"github.com/BeatGlow/display/draw"
|
|
"github.com/BeatGlow/display/pixel"
|
|
)
|
|
|
|
func MustDecodePNG(b []byte) image.Image {
|
|
i, err := png.Decode(bytes.NewBuffer(b))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return i
|
|
}
|
|
|
|
// BufferFor creates a new image buffer for the provided display matching its color model.
|
|
func BufferFor(model color.Model, width, height int) draw.Image {
|
|
switch model {
|
|
case pixel.MonoModel:
|
|
return pixel.NewMonoImage(width, height)
|
|
case pixel.Gray2Model:
|
|
return pixel.NewGray2Image(width, height)
|
|
case pixel.Gray4Model:
|
|
return pixel.NewGray4Image(width, height)
|
|
case pixel.CRGB15Model:
|
|
return pixel.NewCRGB15Image(width, height)
|
|
case pixel.CRGB16Model:
|
|
return pixel.NewCRGB16Image(width, height)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
type Clearer interface {
|
|
Clear()
|
|
}
|
|
|
|
// Clear an image.
|
|
func Clear(i draw.Image) {
|
|
if clear, ok := i.(Clearer); ok {
|
|
clear.Clear()
|
|
return
|
|
}
|
|
draw.Draw(i, i.Bounds(), image.Black, image.Point{}, draw.Src)
|
|
}
|
|
|
|
// Refresh display from image buffer.
|
|
func Refresh(display display.Display, src image.Image) error {
|
|
draw.Draw(display, display.Bounds(), src, image.Point{}, draw.Src)
|
|
return display.Refresh()
|
|
}
|