164 lines
3.1 KiB
Go
164 lines
3.1 KiB
Go
package draw
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
|
|
"github.com/BeatGlow/beatglow/ui"
|
|
"github.com/BeatGlow/beatglow/ui/font"
|
|
"github.com/BeatGlow/display/pixel"
|
|
)
|
|
|
|
type Canvas struct {
|
|
Image Image
|
|
Style ui.Style
|
|
}
|
|
|
|
func NewCanvas(dst Image) *Canvas {
|
|
return &Canvas{
|
|
Image: dst,
|
|
Style: ui.Style{
|
|
Background: image.NewUniform(pixel.Off),
|
|
Foreground: pixel.On,
|
|
Gradient: ui.Rainbow(),
|
|
Font: font.MicroGrotesk,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c Canvas) FilledRectangle(x, y, w, h int) {
|
|
/*
|
|
for py := y; py < y+h; py++ {
|
|
for px := x; px < x+w; px++ {
|
|
c.Image.Set(px, py, c.Style.Background.At(px, py))
|
|
}
|
|
}
|
|
*/
|
|
draw.Draw(c.Image, image.Rect(x, y, x+w, y+h), c.Style.Background, image.Point{}, draw.Src)
|
|
}
|
|
|
|
func (c Canvas) Progess(x, y, w int, progress float64) {
|
|
Rectangle(c.Image, c.Style.Foreground, x, y, w, 8)
|
|
if fill := int(float64(w-2)*progress + .5); fill > 0 {
|
|
c.FilledRectangle(x+1, y+1, fill, 7)
|
|
}
|
|
}
|
|
|
|
func (c Canvas) Text(x, y int, text string) {
|
|
//c.Font.Print(c.Image, c.Stroke, x, y, text)
|
|
c.Style.Font.Draw(c.Image, image.Pt(x, y), image.NewUniform(c.Style.Foreground), text)
|
|
}
|
|
|
|
func (c *Canvas) SetStyle(style ui.Style) *Canvas {
|
|
c.Style = style
|
|
return c
|
|
}
|
|
|
|
func (c *Canvas) WithStyle(style ui.Style, cb func()) {
|
|
oldStyle := c.Style
|
|
c.Style = style
|
|
cb()
|
|
c.Style = oldStyle
|
|
}
|
|
|
|
func (c *Canvas) WithBackground(p image.Image, cb func()) {
|
|
oldBackground := c.Style.Background
|
|
c.Style.Background = p
|
|
cb()
|
|
c.Style.Background = oldBackground
|
|
}
|
|
|
|
func (c *Canvas) WithFont(font *font.Font, cb func()) {
|
|
oldFont := c.Style.Font
|
|
c.Style.Font = font
|
|
cb()
|
|
c.Style.Font = oldFont
|
|
}
|
|
|
|
func (c *Canvas) WithGradient(w, h int, cb func()) {
|
|
gradient := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
for x := 0; x < w; x++ {
|
|
col := c.Style.Gradient.At(float64(x) / float64(w))
|
|
for y := 0; y < h; y++ {
|
|
gradient.Set(x, y, col)
|
|
}
|
|
}
|
|
c.WithBackground(gradient, cb)
|
|
}
|
|
|
|
type Paint struct {
|
|
Color color.Color
|
|
Pattern
|
|
}
|
|
|
|
func (p Paint) At(x, y int) color.Color {
|
|
if p.Pattern == nil {
|
|
return p.Color
|
|
}
|
|
return p.Pattern.At(x, y)
|
|
}
|
|
|
|
func (p Paint) Bounds() image.Rectangle {
|
|
return image.Rectangle{image.Point{-1e9, -1e9}, image.Point{1e9, 1e9}}
|
|
}
|
|
|
|
func (p Paint) ColorModel() color.Model {
|
|
switch p.Color.(type) {
|
|
case pixel.Mono:
|
|
return pixel.MonoModel
|
|
case pixel.Gray2:
|
|
return pixel.Gray2Model
|
|
case pixel.Gray4:
|
|
return pixel.Gray4Model
|
|
case pixel.CRGB15:
|
|
return pixel.CRGB15Model
|
|
case pixel.CRGB16:
|
|
return pixel.CRGB16Model
|
|
case color.Gray:
|
|
return color.GrayModel
|
|
case color.RGBA:
|
|
return color.RGBAModel
|
|
case color.NRGBA:
|
|
return color.NRGBAModel
|
|
default:
|
|
return color.RGBAModel
|
|
}
|
|
}
|
|
|
|
type Pattern interface {
|
|
At(x, y int) color.Color
|
|
}
|
|
|
|
type PatternFunc func(x, y int) color.Color
|
|
|
|
func (f PatternFunc) At(x, y int) color.Color { return f(x, y) }
|
|
|
|
// Solid pattern.
|
|
type Solid struct {
|
|
color.Color
|
|
}
|
|
|
|
func (p Solid) At(x, y int) color.Color {
|
|
return p.Color
|
|
}
|
|
|
|
type Alternate struct {
|
|
A, B color.Color
|
|
X bool
|
|
Y bool
|
|
}
|
|
|
|
func (p Alternate) At(x, y int) color.Color {
|
|
if p.X && p.Y && x%2 == 0 && y%2 == 1 {
|
|
return p.A
|
|
}
|
|
if p.X && x%2 == 0 {
|
|
return p.A
|
|
}
|
|
if p.Y && y%2 == 1 {
|
|
return p.A
|
|
}
|
|
return p.B
|
|
}
|