Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:41 +01:00
1 parent 85a9ec684c
commit c5d6137fab
160 files changed
+76293 -387

No files matched your search

+163
View File
@@ -0,0 +1,163 @@
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
}
+163
View File
@@ -0,0 +1,163 @@
// Package draw contains screen drawing routines.
package draw
import (
"image"
"image/color"
"image/draw"
)
// Image is a type alias for the Go package.
type Image = draw.Image
// Line draws a line from (x0, y0) to (x1, y1).
func Line(dst Image, c color.Color, x0, y0, x1, y1 int) {
PolyLine(dst, c, image.Point{X: x0, Y: y0}, image.Point{X: x1, Y: y1})
}
// PolyLine draws a multi point line between the specified points.
func PolyLine(dst Image, c color.Color, points ...image.Point) {
if l := len(points); l < 1 {
return
} else if l == 1 {
points = append(points, points[0])
}
for i, l := 0, len(points)-1; i < l; i++ {
a, b := points[i], points[i+1]
bresenham(dst, c, a.X, a.Y, b.X, b.Y)
}
}
// Rectangle draws an empty rectangle.
func Rectangle(dst Image, c color.Color, x, y, w, h int) {
Line(dst, c, x+0, y+0, x+w, y+0)
Line(dst, c, x+w, y+0, x+w, y+h)
Line(dst, c, x+0, y+h, x+w, y+h)
Line(dst, c, x+0, y+0, x+0, y+h)
}
// FilledRectangle draws a filled rectangle from (x, y) to (x + w, y + h).
func FilledRectangle(dst Image, c color.Color, x, y, w, h int) {
for dy := 0; dy < h; dy++ {
Line(dst, c, x, y+dy, x+w, y+dy)
}
}
// Generalized with integer
func bresenham(dst Image, c color.Color, x1, y1, x2, y2 int) {
var dx, dy, e, slope int
// Because drawing p1 -> p2 is equivalent to draw p2 -> p1,
// I sort points in x-axis order to handle only half of possible cases.
if x1 > x2 {
x1, y1, x2, y2 = x2, y2, x1, y1
}
dx, dy = x2-x1, y2-y1
// Because point is x-axis ordered, dx cannot be negative
if dy < 0 {
dy = -dy
}
switch {
// Is line a point ?
case x1 == x2 && y1 == y2:
dst.Set(x1, y1, c)
// Is line an horizontal ?
case y1 == y2:
for ; dx != 0; dx-- {
dst.Set(x1, y1, c)
x1++
}
dst.Set(x1, y1, c)
// Is line a vertical ?
case x1 == x2:
if y1 > y2 {
y1, y2 = y2, y1
}
for ; dy != 0; dy-- {
dst.Set(x1, y1, c)
y1++
}
dst.Set(x1, y1, c)
// Is line a diagonal ?
case dx == dy:
if y1 < y2 {
for ; dx != 0; dx-- {
dst.Set(x1, y1, c)
x1++
y1++
}
} else {
for ; dx != 0; dx-- {
dst.Set(x1, y1, c)
x1++
y1--
}
}
dst.Set(x1, y1, c)
// wider than high ?
case dx > dy:
if y1 < y2 {
// BresenhamDxXRYD(img, x1, y1, x2, y2, col)
dy, e, slope = 2*dy, dx, 2*dx
for ; dx != 0; dx-- {
dst.Set(x1, y1, c)
x1++
e -= dy
if e < 0 {
y1++
e += slope
}
}
} else {
// BresenhamDxXRYU(img, x1, y1, x2, y2, col)
dy, e, slope = 2*dy, dx, 2*dx
for ; dx != 0; dx-- {
dst.Set(x1, y1, c)
x1++
e -= dy
if e < 0 {
y1--
e += slope
}
}
}
dst.Set(x2, y2, c)
// higher than wide.
default:
if y1 < y2 {
// BresenhamDyXRYD(img, x1, y1, x2, y2, col)
dx, e, slope = 2*dx, dy, 2*dy
for ; dy != 0; dy-- {
dst.Set(x1, y1, c)
y1++
e -= dx
if e < 0 {
x1++
e += slope
}
}
} else {
// BresenhamDyXRYU(img, x1, y1, x2, y2, col)
dx, e, slope = 2*dx, dy, 2*dy
for ; dy != 0; dy-- {
dst.Set(x1, y1, c)
y1--
e -= dx
if e < 0 {
x1++
e += slope
}
}
}
dst.Set(x2, y2, c)
}
}
+27
View File
@@ -0,0 +1,27 @@
package draw
import (
"image/color"
"image/draw"
"math"
)
func Progress(dst Image, c color.Color, x, y, w int, progress float64) {
Rectangle(dst, c, x, y, w, 8)
if fill := int(math.Round(float64(w) * progress)); fill > 0 {
FilledRectangle(dst, c, x+1, y+1, fill, 6)
}
}
func Sprite(dst draw.Image, c color.Color, x, y int, sprite []uint8) {
w, h, data := sprite[0], sprite[1], sprite[2:]
for dy := 0; dy < int(h); dy++ {
for dx := 0; dx < int(w); dx++ {
byteOffset := int(dy/8)*int(w) + dx
bitOffset := dy % 8
if data[byteOffset]&(1<<bitOffset) != 0 {
dst.Set(x+dx, y+dy, c)
}
}
}
}
+87
View File
@@ -0,0 +1,87 @@
package font
import (
"image"
"image/color"
"image/draw"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
// Font is a bitmap font.
type Font struct {
GlyphWith int
GlyphHeight int
Spacing int
glyphs map[rune][]uint16
glyphsV bool
}
func (f *Font) render(out draw.Image, c color.Color, x, y int, pixels []uint16) int {
if f.glyphsV {
for i, bits := range pixels {
for j := 0; j < f.GlyphWith; j++ {
if bits&(1<<j) != 0 {
out.Set(x+(f.GlyphWith-j-1), y+i, c)
}
}
}
} else {
for dx := 0; dx < f.GlyphWith; dx++ {
for dy := 0; dy < f.GlyphHeight; dy++ {
if pixels[dx]&(1<<dy) != 0 {
out.Set(x+dx, y+dy, c)
}
}
}
}
return f.GlyphWith + f.Spacing
}
func (r *Font) Measure(text string) image.Point {
switch size := len(text); size {
case 0:
return image.Point{}
case 1:
return image.Point{X: r.GlyphWith, Y: r.GlyphHeight}
default:
return image.Point{X: r.GlyphWith*size + r.Spacing*size - r.Spacing, Y: r.GlyphHeight}
}
}
func (f *Font) PrintChar(dst draw.Image, c color.Color, x, y int, r rune) int {
if b := dst.Bounds(); !image.Pt(x, y).In(b) {
// Fast path for out of bounds.
return f.GlyphWith + f.Spacing
}
if r == ' ' {
// Fast path for spaces.
return f.GlyphWith + f.Spacing
}
if pixels, ok := f.glyphs[r]; ok {
return f.render(dst, c, x, y, pixels)
} else if r == '?' {
// Glyph was a question mark, but we don't have it, treat it as a space.
return f.PrintChar(dst, c, x, y, ' ')
}
return f.PrintChar(dst, c, x, y, normalizeRune(r))
}
func (f *Font) Print(dst draw.Image, c color.Color, x, y int, text string) {
for _, r := range []rune(text) {
x += f.PrintChar(dst, c, x, y, r)
}
}
func normalizeRune(r rune) rune {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, err := transform.String(t, string(r))
if err != nil {
return '?'
}
return []rune(result)[0]
}
+385
View File
@@ -0,0 +1,385 @@
package font
// Fixed4x6 font
var Fixed4x6 = &Font{
GlyphWith: 4,
GlyphHeight: 6,
Spacing: 1,
glyphsV: true,
glyphs: fixed4x4Map,
}
// Fixed5x7 font
var Fixed5x7 = &Font{
GlyphWith: 5,
GlyphHeight: 7,
Spacing: 3,
glyphs: fixed5x7Map,
}
var (
fixed4x4Map = map[rune][]uint16{
0x00: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x00
0x01: {0x20, 0x50, 0x70, 0x50, 0x20, 0x00}, // 0x01
0x02: {0x20, 0x70, 0x50, 0x70, 0x20, 0x00}, // 0x02
0x03: {0x00, 0x50, 0x70, 0x70, 0x20, 0x00}, // 0x03
0x04: {0x00, 0x20, 0x70, 0x70, 0x20, 0x00}, // 0x04
0x05: {0x20, 0x70, 0x70, 0x20, 0x70, 0x00}, // 0x05
0x06: {0x20, 0x20, 0x70, 0x20, 0x70, 0x00}, // 0x06
0x07: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x07
0x08: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x08
0x09: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x09
0x0A: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0A
0x0B: {0x00, 0x30, 0x10, 0x60, 0x60, 0x00}, // 0x0B
0x0C: {0x20, 0x50, 0x20, 0x70, 0x20, 0x00}, // 0x0C
0x0D: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x0D
0x0E: {0x20, 0x30, 0x50, 0x10, 0x20, 0x00}, // 0x0E
0x0F: {0x20, 0x70, 0x50, 0x70, 0x20, 0x00}, // 0x0F
0x10: {0x40, 0x60, 0x70, 0x60, 0x40, 0x00}, // 0x10
0x11: {0x10, 0x30, 0x70, 0x30, 0x10, 0x00}, // 0x11
0x12: {0x20, 0x70, 0x20, 0x70, 0x20, 0x00}, // 0x12
0x13: {0x50, 0x50, 0x50, 0x00, 0x50, 0x00}, // 0x13
0x14: {0x00, 0x10, 0x20, 0x20, 0x20, 0x20}, // 0x14
0x15: {0x20, 0x20, 0x20, 0x20, 0x40, 0x00}, // 0x15
0x16: {0x00, 0x00, 0x00, 0x00, 0x70, 0x00}, // 0x16
0x17: {0x20, 0x70, 0x20, 0x70, 0x20, 0x70}, // 0x17
0x18: {0x20, 0x70, 0x20, 0x20, 0x20, 0x00}, // 0x18
0x19: {0x20, 0x20, 0x20, 0x70, 0x20, 0x00}, // 0x19
0x1A: {0x00, 0x20, 0xF0, 0x20, 0x00, 0x00}, // 0x1A
0x1B: {0x00, 0x40, 0xF0, 0x40, 0x00, 0x00}, // 0x1B
0x1C: {0x00, 0x00, 0x40, 0x70, 0x00, 0x00}, // 0x1C
0x1D: {0x00, 0x50, 0x70, 0x50, 0x00, 0x00}, // 0x1D
0x1E: {0x00, 0x20, 0x70, 0x70, 0x00, 0x00}, // 0x1E
0x1F: {0x00, 0x70, 0x70, 0x20, 0x00, 0x00}, // 0x1F
0x20: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x20
0x21: {0x20, 0x20, 0x20, 0x00, 0x20, 0x00}, // 0x21
0x22: {0x50, 0x50, 0x00, 0x00, 0x00, 0x00}, // 0x22
0x23: {0x50, 0x70, 0x50, 0x70, 0x50, 0x00}, // 0x23
0x24: {0x20, 0x30, 0x60, 0x30, 0x60, 0x20}, // 0x24
0x25: {0x40, 0x10, 0x20, 0x40, 0x10, 0x00}, // 0x25
0x26: {0x20, 0x50, 0x30, 0x50, 0x70, 0x00}, // 0x26
0x27: {0x60, 0x40, 0x00, 0x00, 0x00, 0x00}, // 0x27
0x28: {0x20, 0x40, 0x40, 0x40, 0x20, 0x00}, // 0x28
0x29: {0x40, 0x20, 0x20, 0x20, 0x40, 0x00}, // 0x29
0x2A: {0x50, 0x20, 0x70, 0x20, 0x50, 0x00}, // 0x2A
0x2B: {0x00, 0x20, 0x70, 0x20, 0x00, 0x00}, // 0x2B
0x2C: {0x00, 0x00, 0x00, 0x00, 0x60, 0x40}, // 0x2C
0x2D: {0x00, 0x00, 0x70, 0x00, 0x00, 0x00}, // 0x2D
0x2E: {0x00, 0x00, 0x00, 0x00, 0x20, 0x00}, // 0x2E
0x2F: {0x10, 0x10, 0x20, 0x40, 0x40, 0x00}, // 0x2F
0x30: {0x30, 0x50, 0x50, 0x50, 0x60, 0x00}, // 0x30
0x31: {0x20, 0x60, 0x20, 0x20, 0x70, 0x00}, // 0x31
0x32: {0x60, 0x10, 0x20, 0x40, 0x70, 0x00}, // 0x32
0x33: {0x60, 0x10, 0x20, 0x10, 0x60, 0x00}, // 0x33
0x34: {0x10, 0x50, 0x70, 0x10, 0x10, 0x00}, // 0x34
0x35: {0x70, 0x40, 0x60, 0x10, 0x60, 0x00}, // 0x35
0x36: {0x20, 0x40, 0x60, 0x50, 0x20, 0x00}, // 0x36
0x37: {0x70, 0x10, 0x30, 0x20, 0x20, 0x00}, // 0x37
0x38: {0x20, 0x50, 0x20, 0x50, 0x20, 0x00}, // 0x38
0x39: {0x20, 0x50, 0x30, 0x10, 0x20, 0x00}, // 0x39
0x3A: {0x00, 0x00, 0x20, 0x00, 0x20, 0x00}, // 0x3A
0x3B: {0x00, 0x00, 0x20, 0x00, 0x60, 0x40}, // 0x3B
0x3C: {0x10, 0x20, 0x40, 0x20, 0x10, 0x00}, // 0x3C
0x3D: {0x00, 0x00, 0x70, 0x00, 0x70, 0x00}, // 0x3D
0x3E: {0x40, 0x20, 0x10, 0x20, 0x40, 0x00}, // 0x3E
0x3F: {0x60, 0x10, 0x20, 0x00, 0x20, 0x00}, // 0x3F
0x40: {0x70, 0x50, 0x50, 0x40, 0x70, 0x00}, // 0x40
0x41: {0x20, 0x50, 0x70, 0x50, 0x50, 0x00}, // 0x41
0x42: {0x60, 0x50, 0x60, 0x50, 0x60, 0x00}, // 0x42
0x43: {0x30, 0x40, 0x40, 0x40, 0x30, 0x00}, // 0x43
0x44: {0x60, 0x50, 0x50, 0x50, 0x60, 0x00}, // 0x44
0x45: {0x70, 0x40, 0x60, 0x40, 0x70, 0x00}, // 0x45
0x46: {0x70, 0x40, 0x60, 0x40, 0x40, 0x00}, // 0x46
0x47: {0x30, 0x40, 0x50, 0x50, 0x30, 0x00}, // 0x47
0x48: {0x50, 0x50, 0x70, 0x50, 0x50, 0x00}, // 0x48
0x49: {0x70, 0x20, 0x20, 0x20, 0x70, 0x00}, // 0x49
0x4A: {0x10, 0x10, 0x10, 0x50, 0x20, 0x00}, // 0x4A
0x4B: {0x50, 0x50, 0x60, 0x50, 0x50, 0x00}, // 0x4B
0x4C: {0x40, 0x40, 0x40, 0x40, 0x70, 0x00}, // 0x4C
0x4D: {0x50, 0x70, 0x70, 0x50, 0x50, 0x00}, // 0x4D
0x4E: {0x50, 0x70, 0x50, 0x50, 0x50, 0x00}, // 0x4E
0x4F: {0x20, 0x50, 0x50, 0x50, 0x20, 0x00}, // 0x4F
0x50: {0x60, 0x50, 0x60, 0x40, 0x40, 0x00}, // 0x50
0x51: {0x20, 0x50, 0x50, 0x70, 0x30, 0x00}, // 0x51
0x52: {0x60, 0x50, 0x60, 0x50, 0x50, 0x00}, // 0x52
0x53: {0x30, 0x40, 0x70, 0x10, 0x60, 0x00}, // 0x53
0x54: {0x70, 0x20, 0x20, 0x20, 0x20, 0x00}, // 0x54
0x55: {0x50, 0x50, 0x50, 0x50, 0x70, 0x00}, // 0x55
0x56: {0x50, 0x50, 0x50, 0x50, 0x20, 0x00}, // 0x56
0x57: {0x50, 0x50, 0x70, 0x70, 0x50, 0x00}, // 0x57
0x58: {0x50, 0x50, 0x20, 0x50, 0x50, 0x00}, // 0x58
0x59: {0x50, 0x50, 0x20, 0x20, 0x20, 0x00}, // 0x59
0x5A: {0x70, 0x10, 0x20, 0x40, 0x70, 0x00}, // 0x5A
0x5B: {0x60, 0x40, 0x40, 0x40, 0x60, 0x00}, // 0x5B
0x5C: {0x40, 0x40, 0x20, 0x10, 0x10, 0x00}, // 0x5C
0x5D: {0x60, 0x20, 0x20, 0x20, 0x60, 0x00}, // 0x5D
0x5E: {0x20, 0x50, 0x00, 0x00, 0x00, 0x00}, // 0x5E
0x5F: {0x00, 0x00, 0x00, 0x00, 0x00, 0xF0}, // 0x5F
0x60: {0x60, 0x20, 0x00, 0x00, 0x00, 0x00}, // 0x60
0x61: {0x00, 0x00, 0x30, 0x50, 0x70, 0x00}, // 0x61
0x62: {0x40, 0x40, 0x60, 0x50, 0x60, 0x00}, // 0x62
0x63: {0x00, 0x00, 0x30, 0x40, 0x30, 0x00}, // 0x63
0x64: {0x10, 0x10, 0x30, 0x50, 0x30, 0x00}, // 0x64
0x65: {0x00, 0x00, 0x70, 0x60, 0x30, 0x00}, // 0x65
0x66: {0x10, 0x20, 0x70, 0x20, 0x20, 0x00}, // 0x66
0x67: {0x00, 0x00, 0x70, 0x50, 0x10, 0x70}, // 0x67
0x68: {0x40, 0x40, 0x60, 0x50, 0x50, 0x00}, // 0x68
0x69: {0x20, 0x00, 0x20, 0x20, 0x20, 0x00}, // 0x69
0x6A: {0x20, 0x00, 0x20, 0x20, 0x20, 0x60}, // 0x6A
0x6B: {0x40, 0x40, 0x50, 0x60, 0x50, 0x00}, // 0x6B
0x6C: {0x20, 0x20, 0x20, 0x20, 0x20, 0x00}, // 0x6C
0x6D: {0x00, 0x00, 0x70, 0x70, 0x50, 0x00}, // 0x6D
0x6E: {0x00, 0x00, 0x60, 0x50, 0x50, 0x00}, // 0x6E
0x6F: {0x00, 0x00, 0x20, 0x50, 0x20, 0x00}, // 0x6F
0x70: {0x00, 0x00, 0x60, 0x50, 0x60, 0x40}, // 0x70
0x71: {0x00, 0x00, 0x30, 0x50, 0x30, 0x10}, // 0x71
0x72: {0x00, 0x00, 0x60, 0x40, 0x40, 0x00}, // 0x72
0x73: {0x00, 0x00, 0x30, 0x20, 0x60, 0x00}, // 0x73
0x74: {0x00, 0x20, 0x70, 0x20, 0x30, 0x00}, // 0x74
0x75: {0x00, 0x00, 0x50, 0x50, 0x70, 0x00}, // 0x75
0x76: {0x00, 0x00, 0x50, 0x50, 0x20, 0x00}, // 0x76
0x77: {0x00, 0x00, 0x50, 0x70, 0x70, 0x00}, // 0x77
0x78: {0x00, 0x00, 0x50, 0x20, 0x50, 0x00}, // 0x78
0x79: {0x00, 0x00, 0x50, 0x50, 0x20, 0x40}, // 0x79
0x7A: {0x00, 0x00, 0x60, 0x20, 0x30, 0x00}, // 0x7A
0x7B: {0x30, 0x20, 0x60, 0x20, 0x30, 0x00}, // 0x7B
0x7C: {0x20, 0x20, 0x20, 0x20, 0x20, 0x00}, // 0x7C
0x7D: {0x60, 0x20, 0x30, 0x20, 0x60, 0x00}, // 0x7D
0x7E: {0x50, 0xA0, 0x00, 0x00, 0x00, 0x00}, // 0x7E
0x7F: {0x00, 0x20, 0x50, 0x70, 0x00, 0x00}, // 0x7F
0x80: {0x30, 0x40, 0x40, 0x70, 0x20, 0x40}, // 0x80
0x81: {0x50, 0x00, 0x50, 0x50, 0x30, 0x00}, // 0x81
0x82: {0x10, 0x20, 0x70, 0x60, 0x30, 0x00}, // 0x82
0x83: {0x20, 0x50, 0x30, 0x50, 0x70, 0x00}, // 0x83
0x84: {0x50, 0x00, 0x30, 0x50, 0x70, 0x00}, // 0x84
0x85: {0x40, 0x20, 0x30, 0x50, 0x70, 0x00}, // 0x85
0x86: {0x20, 0x00, 0x30, 0x50, 0x70, 0x00}, // 0x86
0x87: {0x00, 0x70, 0x40, 0x70, 0x20, 0x60}, // 0x87
0x88: {0x20, 0x50, 0x70, 0x60, 0x30, 0x00}, // 0x88
0x89: {0x50, 0x00, 0x70, 0x60, 0x30, 0x00}, // 0x89
0x8A: {0x40, 0x20, 0x70, 0x60, 0x30, 0x00}, // 0x8A
0x8B: {0x50, 0x00, 0x20, 0x20, 0x20, 0x00}, // 0x8B
0x8C: {0x20, 0x50, 0x00, 0x20, 0x20, 0x00}, // 0x8C
0x8D: {0x40, 0x20, 0x00, 0x20, 0x20, 0x00}, // 0x8D
0x8E: {0x50, 0x20, 0x50, 0x70, 0x50, 0x00}, // 0x8E
0x8F: {0x20, 0x20, 0x50, 0x70, 0x50, 0x00}, // 0x8F
0x90: {0x10, 0x20, 0x70, 0x60, 0x70, 0x00}, // 0x90
0x91: {0x00, 0x00, 0x30, 0x70, 0x60, 0x00}, // 0x91
0x92: {0x30, 0x60, 0x70, 0x60, 0x70, 0x00}, // 0x92
0x93: {0x20, 0x50, 0x20, 0x50, 0x20, 0x00}, // 0x93
0x94: {0x50, 0x00, 0x20, 0x50, 0x20, 0x00}, // 0x94
0x95: {0x40, 0x20, 0x20, 0x50, 0x20, 0x00}, // 0x95
0x96: {0x20, 0x50, 0x00, 0x50, 0x70, 0x00}, // 0x96
0x97: {0x40, 0x20, 0x50, 0x50, 0x70, 0x00}, // 0x97
0x98: {0x50, 0x00, 0x50, 0x50, 0x20, 0x40}, // 0x98
0x99: {0x50, 0x20, 0x50, 0x50, 0x20, 0x00}, // 0x99
0x9A: {0x50, 0x00, 0x50, 0x50, 0x70, 0x00}, // 0x9A
0x9B: {0x20, 0x70, 0x40, 0x70, 0x20, 0x00}, // 0x9B
0x9C: {0x10, 0x20, 0x70, 0x20, 0x70, 0x00}, // 0x9C
0x9D: {0x50, 0x70, 0x20, 0x70, 0x20, 0x00}, // 0x9D
0x9E: {0x00, 0x60, 0x60, 0x50, 0x50, 0x00}, // 0x9E
0x9F: {0x30, 0x20, 0x30, 0x20, 0x60, 0x00}, // 0x9F
0xA0: {0x10, 0x20, 0x30, 0x50, 0x70, 0x00}, // 0xA0
0xA1: {0x10, 0x20, 0x00, 0x20, 0x20, 0x00}, // 0xA1
0xA2: {0x10, 0x20, 0x70, 0x50, 0x70, 0x00}, // 0xA2
0xA3: {0x10, 0x20, 0x00, 0x50, 0x70, 0x00}, // 0xA3
0xA4: {0x70, 0x00, 0x70, 0x50, 0x50, 0x00}, // 0xA4
0xA5: {0x70, 0x00, 0x50, 0x70, 0x50, 0x00}, // 0xA5
0xA6: {0x30, 0x50, 0x70, 0x00, 0x70, 0x00}, // 0xA6
0xA7: {0x20, 0x50, 0x20, 0x00, 0x70, 0x00}, // 0xA7
0xA8: {0x20, 0x00, 0x20, 0x40, 0x30, 0x00}, // 0xA8
0xA9: {0x00, 0x70, 0x40, 0x40, 0x00, 0x00}, // 0xA9
0xAA: {0x00, 0xE0, 0x20, 0x20, 0x00, 0x00}, // 0xAA
0xAB: {0x40, 0x50, 0x20, 0x50, 0x30, 0x00}, // 0xAB
0xAC: {0x40, 0x50, 0x20, 0x70, 0x10, 0x00}, // 0xAC
0xAD: {0x20, 0x00, 0x20, 0x20, 0x20, 0x00}, // 0xAD
0xAE: {0x00, 0x50, 0xA0, 0x50, 0x00, 0x00}, // 0xAE
0xAF: {0x00, 0xA0, 0x50, 0xA0, 0x00, 0x00}, // 0xAF
0xB0: {0x40, 0x10, 0x40, 0x10, 0x40, 0x10}, // 0xB0
0xB1: {0x50, 0xA0, 0x50, 0xA0, 0x50, 0xA0}, // 0xB1
0xB2: {0xB0, 0xE0, 0xB0, 0xE0, 0xB0, 0xE0}, // 0xB2
0xB3: {0x20, 0x20, 0x20, 0x20, 0x20, 0x20}, // 0xB3
0xB4: {0x20, 0x20, 0xE0, 0x20, 0x20, 0x20}, // 0xB4
0xB5: {0x20, 0xE0, 0x20, 0xE0, 0x20, 0x20}, // 0xB5
0xB6: {0x50, 0x50, 0xD0, 0x50, 0x50, 0x50}, // 0xB6
0xB7: {0x00, 0x00, 0xF0, 0x50, 0x50, 0x50}, // 0xB7
0xB8: {0x00, 0xE0, 0x20, 0xE0, 0x20, 0x20}, // 0xB8
0xB9: {0x50, 0xD0, 0x10, 0xD0, 0x50, 0x50}, // 0xB9
0xBA: {0x50, 0x50, 0x50, 0x50, 0x50, 0x50}, // 0xBA
0xBB: {0x00, 0xF0, 0x10, 0xD0, 0x50, 0x50}, // 0xBB
0xBC: {0x50, 0xD0, 0x10, 0xF0, 0x00, 0x00}, // 0xBC
0xBD: {0x50, 0x50, 0xF0, 0x00, 0x00, 0x00}, // 0xBD
0xBE: {0x20, 0xE0, 0x20, 0xE0, 0x00, 0x00}, // 0xBE
0xBF: {0x00, 0x00, 0xE0, 0x20, 0x20, 0x20}, // 0xBF
0xC0: {0x20, 0x20, 0x30, 0x00, 0x00, 0x00}, // 0xC0
0xC1: {0x20, 0x20, 0xF0, 0x00, 0x00, 0x00}, // 0xC1
0xC2: {0x00, 0x00, 0xF0, 0x20, 0x20, 0x20}, // 0xC2
0xC3: {0x20, 0x20, 0x30, 0x20, 0x20, 0x20}, // 0xC3
0xC4: {0x00, 0x00, 0xF0, 0x00, 0x00, 0x00}, // 0xC4
0xC5: {0x20, 0x20, 0xF0, 0x20, 0x20, 0x20}, // 0xC5
0xC6: {0x20, 0x30, 0x20, 0x30, 0x20, 0x20}, // 0xC6
0xC7: {0x50, 0x50, 0x50, 0x50, 0x50, 0x50}, // 0xC7
0xC8: {0x50, 0x50, 0x40, 0x70, 0x00, 0x00}, // 0xC8
0xC9: {0x00, 0x70, 0x40, 0x50, 0x50, 0x50}, // 0xC9
0xCA: {0x50, 0xD0, 0x00, 0xF0, 0x00, 0x00}, // 0xCA
0xCB: {0x00, 0xF0, 0x00, 0xD0, 0x50, 0x50}, // 0xCB
0xCC: {0x50, 0x50, 0x40, 0x50, 0x50, 0x50}, // 0xCC
0xCD: {0x00, 0xF0, 0x00, 0xF0, 0x00, 0x00}, // 0xCD
0xCE: {0x50, 0xD0, 0x00, 0xD0, 0x50, 0x50}, // 0xCE
0xCF: {0x20, 0xF0, 0x00, 0xF0, 0x00, 0x00}, // 0xCF
0xD0: {0x50, 0x50, 0xF0, 0x00, 0x00, 0x00}, // 0xD0
0xD1: {0x00, 0xF0, 0x00, 0xF0, 0x20, 0x20}, // 0xD1
0xD2: {0x00, 0x00, 0xF0, 0x50, 0x50, 0x50}, // 0xD2
0xD3: {0x50, 0x50, 0x70, 0x00, 0x00, 0x00}, // 0xD3
0xD4: {0x20, 0x30, 0x20, 0x30, 0x00, 0x00}, // 0xD4
0xD5: {0x00, 0x30, 0x20, 0x30, 0x20, 0x20}, // 0xD5
0xD6: {0x00, 0x00, 0x70, 0x50, 0x50, 0x50}, // 0xD6
0xD7: {0x50, 0x50, 0xD0, 0x50, 0x50, 0x50}, // 0xD7
0xD8: {0x20, 0xF0, 0x00, 0xF0, 0x20, 0x20}, // 0xD8
0xD9: {0x20, 0x20, 0xE0, 0x00, 0x00, 0x00}, // 0xD9
0xDA: {0x00, 0x00, 0x30, 0x20, 0x20, 0x20}, // 0xDA
0xDB: {0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0}, // 0xDB
0xDC: {0x00, 0x00, 0x00, 0xF0, 0xF0, 0xF0}, // 0xDC
0xDD: {0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0}, // 0xDD
0xDE: {0x30, 0x30, 0x30, 0x30, 0x30, 0x30}, // 0xDE
0xDF: {0xF0, 0xF0, 0xF0, 0x00, 0x00, 0x00}, // 0xDF
0xE0: {0x00, 0x00, 0x70, 0x60, 0x70, 0x00}, // 0xE0
0xE1: {0x20, 0x50, 0x60, 0x50, 0x60, 0x40}, // 0xE1
0xE2: {0x70, 0x50, 0x40, 0x40, 0x40, 0x00}, // 0xE2
0xE3: {0x70, 0x50, 0x50, 0x50, 0x50, 0x00}, // 0xE3
0xE4: {0x70, 0x40, 0x20, 0x40, 0x70, 0x00}, // 0xE4
0xE5: {0x00, 0x00, 0x30, 0x50, 0x20, 0x00}, // 0xE5
0xE6: {0x00, 0x00, 0x50, 0x50, 0x70, 0x40}, // 0xE6
0xE7: {0x00, 0x10, 0x60, 0x20, 0x20, 0x00}, // 0xE7
0xE8: {0x70, 0x20, 0x50, 0x20, 0x70, 0x00}, // 0xE8
0xE9: {0x20, 0x50, 0x70, 0x50, 0x20, 0x00}, // 0xE9
0xEA: {0x00, 0x20, 0x50, 0x50, 0x50, 0x00}, // 0xEA
0xEB: {0x30, 0x40, 0x20, 0x50, 0x20, 0x00}, // 0xEB
0xEC: {0x00, 0x00, 0x70, 0x50, 0x70, 0x00}, // 0xEC
0xED: {0x20, 0x70, 0x50, 0x70, 0x20, 0x00}, // 0xED
0xEE: {0x30, 0x40, 0x70, 0x40, 0x30, 0x00}, // 0xEE
0xEF: {0x20, 0x50, 0x50, 0x50, 0x50, 0x00}, // 0xEF
0xF0: {0x70, 0x00, 0x70, 0x00, 0x70, 0x00}, // 0xF0
0xF1: {0x20, 0x70, 0x20, 0x00, 0x70, 0x00}, // 0xF1
0xF2: {0x60, 0x10, 0x60, 0x00, 0x70, 0x00}, // 0xF2
0xF3: {0x30, 0x40, 0x30, 0x00, 0x70, 0x00}, // 0xF3
0xF4: {0x00, 0x10, 0x20, 0x20, 0x20, 0x20}, // 0xF4
0xF5: {0x20, 0x20, 0x20, 0x20, 0x40, 0x00}, // 0xF5
0xF6: {0x20, 0x00, 0x70, 0x00, 0x20, 0x00}, // 0xF6
0xF7: {0x00, 0x50, 0xA0, 0x50, 0xA0, 0x00}, // 0xF7
0xF8: {0x20, 0x50, 0x20, 0x00, 0x00, 0x00}, // 0xF8
0xF9: {0x00, 0x20, 0x70, 0x20, 0x00, 0x00}, // 0xF9
0xFA: {0x00, 0x00, 0x20, 0x00, 0x00, 0x00}, // 0xFA
0xFB: {0x30, 0x20, 0x20, 0x60, 0x20, 0x00}, // 0xFB
0xFC: {0x70, 0x50, 0x50, 0x00, 0x00, 0x00}, // 0xFC
0xFD: {0x60, 0x20, 0x40, 0x60, 0x00, 0x00}, // 0xFD
0xFE: {0x00, 0x00, 0x60, 0x60, 0x00, 0x00}, // 0xFE
0xFF: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0xFF
}
fixed5x7Map = map[rune][]uint16{
' ': {0x00, 0x00, 0x00, 0x00, 0x00}, // (space)
'!': {0x00, 0x00, 0x5F, 0x00, 0x00}, // !
'"': {0x00, 0x07, 0x00, 0x07, 0x00}, // "
'#': {0x14, 0x7F, 0x14, 0x7F, 0x14}, // #
'$': {0x24, 0x2A, 0x7F, 0x2A, 0x12}, // $
'%': {0x23, 0x13, 0x08, 0x64, 0x62}, // %
'&': {0x36, 0x49, 0x55, 0x22, 0x50}, // &
'\'': {0x00, 0x05, 0x03, 0x00, 0x00}, // '
'(': {0x00, 0x1C, 0x22, 0x41, 0x00}, // (
')': {0x00, 0x41, 0x22, 0x1C, 0x00}, // )
'*': {0x08, 0x2A, 0x1C, 0x2A, 0x08}, // *
'+': {0x08, 0x08, 0x3E, 0x08, 0x08}, // +
',': {0x00, 0x50, 0x30, 0x00, 0x00}, // ,
'-': {0x08, 0x08, 0x08, 0x08, 0x08}, // -
'.': {0x00, 0x60, 0x60, 0x00, 0x00}, // .
'/': {0x20, 0x10, 0x08, 0x04, 0x02}, // /
'0': {0x3E, 0x51, 0x49, 0x45, 0x3E}, // 0
'1': {0x00, 0x42, 0x7F, 0x40, 0x00}, // 1
'2': {0x42, 0x61, 0x51, 0x49, 0x46}, // 2
'3': {0x21, 0x41, 0x45, 0x4B, 0x31}, // 3
'4': {0x18, 0x14, 0x12, 0x7F, 0x10}, // 4
'5': {0x27, 0x45, 0x45, 0x45, 0x39}, // 5
'6': {0x3C, 0x4A, 0x49, 0x49, 0x30}, // 6
'7': {0x01, 0x71, 0x09, 0x05, 0x03}, // 7
'8': {0x36, 0x49, 0x49, 0x49, 0x36}, // 8
'9': {0x06, 0x49, 0x49, 0x29, 0x1E}, // 9
':': {0x00, 0x36, 0x36, 0x00, 0x00}, // :
';': {0x00, 0x56, 0x36, 0x00, 0x00}, // ;
'<': {0x00, 0x08, 0x14, 0x22, 0x41}, // <
'=': {0x14, 0x14, 0x14, 0x14, 0x14}, // =
'>': {0x41, 0x22, 0x14, 0x08, 0x00}, // >
'?': {0x02, 0x01, 0x51, 0x09, 0x06}, // ?
'@': {0x32, 0x49, 0x79, 0x41, 0x3E}, // @
'A': {0x7E, 0x11, 0x11, 0x11, 0x7E}, // A
'B': {0x7F, 0x49, 0x49, 0x49, 0x36}, // B
'C': {0x3E, 0x41, 0x41, 0x41, 0x22}, // C
'D': {0x7F, 0x41, 0x41, 0x22, 0x1C}, // D
'E': {0x7F, 0x49, 0x49, 0x49, 0x41}, // E
'F': {0x7F, 0x09, 0x09, 0x01, 0x01}, // F
'G': {0x3E, 0x41, 0x41, 0x51, 0x32}, // G
'H': {0x7F, 0x08, 0x08, 0x08, 0x7F}, // H
'I': {0x00, 0x41, 0x7F, 0x41, 0x00}, // I
'J': {0x20, 0x40, 0x41, 0x3F, 0x01}, // J
'K': {0x7F, 0x08, 0x14, 0x22, 0x41}, // K
'L': {0x7F, 0x40, 0x40, 0x40, 0x40}, // L
'M': {0x7F, 0x02, 0x04, 0x02, 0x7F}, // M
'N': {0x7F, 0x04, 0x08, 0x10, 0x7F}, // N
'O': {0x3E, 0x41, 0x41, 0x41, 0x3E}, // O
'P': {0x7F, 0x09, 0x09, 0x09, 0x06}, // P
'Q': {0x3E, 0x41, 0x51, 0x21, 0x5E}, // Q
'R': {0x7F, 0x09, 0x19, 0x29, 0x46}, // R
'S': {0x46, 0x49, 0x49, 0x49, 0x31}, // S
'T': {0x01, 0x01, 0x7F, 0x01, 0x01}, // T
'U': {0x3F, 0x40, 0x40, 0x40, 0x3F}, // U
'V': {0x1F, 0x20, 0x40, 0x20, 0x1F}, // V
'W': {0x7F, 0x20, 0x18, 0x20, 0x7F}, // W
'X': {0x63, 0x14, 0x08, 0x14, 0x63}, // X
'Y': {0x03, 0x04, 0x78, 0x04, 0x03}, // Y
'Z': {0x61, 0x51, 0x49, 0x45, 0x43}, // Z
'Ä': {0x7D, 0x12, 0x12, 0x7D, 0x00}, // Ä
'Ö': {0x3D, 0x42, 0x42, 0x42, 0x3D}, // Ö
'Ü': {0x3D, 0x40, 0x40, 0x40, 0x3D}, // Ü
'[': {0x00, 0x00, 0x7F, 0x41, 0x41}, // [
'\\': {0x02, 0x04, 0x08, 0x10, 0x20}, // "\"
']': {0x41, 0x41, 0x7F, 0x00, 0x00}, // ]
'^': {0x04, 0x02, 0x01, 0x02, 0x04}, // ^
'_': {0x40, 0x40, 0x40, 0x40, 0x40}, // _
'`': {0x00, 0x01, 0x02, 0x04, 0x00}, // `
'a': {0x20, 0x54, 0x54, 0x54, 0x78}, // a
'b': {0x7F, 0x48, 0x44, 0x44, 0x38}, // b
'c': {0x38, 0x44, 0x44, 0x44, 0x20}, // c
'd': {0x38, 0x44, 0x44, 0x48, 0x7F}, // d
'e': {0x38, 0x54, 0x54, 0x54, 0x18}, // e
'f': {0x08, 0x7E, 0x09, 0x01, 0x02}, // f
'g': {0x08, 0x14, 0x54, 0x54, 0x3C}, // g
'h': {0x7F, 0x08, 0x04, 0x04, 0x78}, // h
'i': {0x00, 0x44, 0x7D, 0x40, 0x00}, // i
'j': {0x20, 0x40, 0x44, 0x3D, 0x00}, // j
'k': {0x00, 0x7F, 0x10, 0x28, 0x44}, // k
'l': {0x00, 0x41, 0x7F, 0x40, 0x00}, // l
'm': {0x7C, 0x04, 0x18, 0x04, 0x78}, // m
'n': {0x7C, 0x08, 0x04, 0x04, 0x78}, // n
'o': {0x38, 0x44, 0x44, 0x44, 0x38}, // o
'p': {0x7C, 0x14, 0x14, 0x14, 0x08}, // p
'q': {0x08, 0x14, 0x14, 0x18, 0x7C}, // q
'r': {0x7C, 0x08, 0x04, 0x04, 0x08}, // r
's': {0x48, 0x54, 0x54, 0x54, 0x20}, // s
't': {0x04, 0x3F, 0x44, 0x40, 0x20}, // t
'u': {0x3C, 0x40, 0x40, 0x20, 0x7C}, // u
'v': {0x1C, 0x20, 0x40, 0x20, 0x1C}, // v
'w': {0x3C, 0x40, 0x30, 0x40, 0x3C}, // w
'x': {0x44, 0x28, 0x10, 0x28, 0x44}, // x
'y': {0x0C, 0x50, 0x50, 0x50, 0x3C}, // y
'z': {0x44, 0x64, 0x54, 0x4C, 0x44}, // z
'ä': {0x20, 0x55, 0x54, 0x55, 0x78}, // ä
'ö': {0x3A, 0x44, 0x44, 0x3A, 0x00}, // ö
'ü': {0x3A, 0x40, 0x40, 0x3A, 0x00}, // ü
'{': {0x00, 0x08, 0x36, 0x41, 0x00}, // {
'|': {0x00, 0x00, 0x7F, 0x00, 0x00}, // |
'}': {0x00, 0x41, 0x36, 0x08, 0x00}, // }
'€': {0x14, 0x3E, 0x55, 0x41, 0x22}, // €
'†': {0x08, 0x08, 0x2A, 0x1C, 0x08}, // -> (ALT + 0134) †
'‡': {0x08, 0x1C, 0x2A, 0x08, 0x08}, // <- (ALT + 0135) ‡
'°': {0x00, 0x00, 0x07, 0x05, 0x07}, // °
}
)
File diff suppressed because it is too large. Load diff
+232
View File
@@ -0,0 +1,232 @@
package pixel
import "image/color"
// Pixel with a particular color, matching the underlying hardware of a
// particular display. Each pixel is at least 1 byte in size.
// The color format is sRGB (or close to it) in all cases except for 1-bit.
type Color interface {
Monochrome | RGB444 | RGB565
BaseColor
}
// BaseColor contains all the methods needed in a color format. This can be used
// in display drivers that want to define their own Color type with just the
// pixel formats the display supports.
type BaseColor interface {
color.Color
// The number of bits when stored.
// This means for example that RGB555 (which is still stored as a 16-bit
// integer) returns 16, while RGB444 returns 12.
BitsPerPixel() int
// Return the given color in color.RGBA format, which is always sRGB. The
// alpha channel is always 255.
RGBAColor() color.RGBA
}
// NewColor returns the given color based on the RGB values passed in the
// parameters. The input value is assumed to be in sRGB color space.
func NewColor[T Color](r, g, b uint8) T {
// Ugly cast from color.RGBA to T. The type switch and interface casts are
// trivially optimized away after instantiation.
var value T
switch any(value).(type) {
case Monochrome:
return any(NewMonochrome(r, g, b)).(T)
case RGB444:
return any(NewRGB444(r, g, b)).(T)
case RGB565:
return any(NewRGB565(r, g, b)).(T)
/*
case RGB555:
return any(NewRGB555(r, g, b)).(T)
case RGB888:
return any(NewRGB888(r, g, b)).(T)
*/
default:
panic("unknown color format")
}
}
// ToGrayBits converts a color to a n-bit grayscale color.
func ToGrayBits(c color.Color, n uint8) uint16 {
r, g, b, _ := c.RGBA()
if n < 1 {
return 0 // no color
} else if n > 16 {
n = 16 // most that will fit
}
// Shift values to fit n bits.
s := 32 - n
// These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
// as those given by the JFIF specification and used by func RGBToYCbCr in
// image/color/ycbcr.go.
//
// Note that 19595 + 38470 + 7471 equals 65536.
//
// The 28 is 16 + 8 + n. The 16 is the same as used in RGBToYCbCr. The 8 + n is
// because the return value is n-bit color, not 16 bit color.
return uint16((19595*r + 38470*g + 7471*b + 1<<15) >> s)
}
// Monochrome color.
type Monochrome uint8
// Monochrome color names.
const (
Off Monochrome = iota
On
)
func NewMonochrome(r, g, b uint8) Monochrome {
if ToGrayBits(color.RGBA{r, g, b, 0xff}, 1) == 1 {
return On
}
return Off
}
func (Monochrome) BitsPerPixel() int {
return 1
}
func (c Monochrome) RGBA() (r, g, b, a uint32) {
if c == 0 {
return 0, 0, 0, 0
}
return 0xffff, 0xffff, 0xffff, 0xffff
}
func (c Monochrome) RGBAColor() color.RGBA {
if c == 0 {
return color.RGBA{}
}
return color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
}
func IsOn(c color.Color) bool {
if _, ok := c.(Monochrome); ok {
return c == On
}
return ToGrayBits(c, 1) == 1
}
// Color format that is supported by the ST7789 for example.
// It may be a bit faster to use than RGB565BE on very slow SPI buses.
//
// The color format is native endian as a uint16 (0000rrrr_ggggbbbb), not big
// endian which you might expect. I tried swapping the bytes, but it didn't have
// much of a performance impact and made the code harder to read. It is stored
// as a 12-bit big endian value in Image[RGB444] though.
type RGB444 uint16
func NewRGB444(r, g, b uint8) RGB444 {
return RGB444(r>>4)<<8 | RGB444(g>>4)<<4 | RGB444(b>>4)
}
func (c RGB444) BitsPerPixel() int {
return 12
}
func (c RGB444) RGBA() (r, g, b, a uint32) {
r = uint32(c>>8) & 0x0f
r |= (r << 4) | (r << 8) | (r << 12)
g = uint32(c>>4) & 0x0f
g |= (g << 4) | (g << 8) | (g << 12)
b = uint32(c>>0) & 0x0f
b |= (b << 4) | (b << 8) | (b << 12)
a |= 0xffff
return
}
func (c RGB444) RGBAColor() color.RGBA {
color := color.RGBA{
R: uint8(c>>8) << 4,
G: uint8(c>>4) << 4,
B: uint8(c>>0) << 4,
A: 255,
}
// Correct color rounding, so that 0xff roundtrips back to 0xff.
color.R |= color.R >> 4
color.G |= color.G >> 4
color.B |= color.B >> 4
return color
}
// RGB565 color.
type RGB565 uint16
func NewRGB565(r, g, b uint8) RGB565 {
val := uint16(r&0xF8)<<8 +
uint16(g&0xFC)<<3 +
uint16(b&0xF8)>>3
// Swap endianness (make big endian).
// This is done using a single instruction on ARM (rev16).
// TODO: this should only be done on little endian systems, but TinyGo
// doesn't currently (2023) support big endian systems so it's difficult to
// test. Also, big endian systems don't seem fasionable these days.
//val = bits.ReverseBytes16(val)
return RGB565(val)
}
// FromColor retusn the given color.
func FromColor[T Color](c color.Color) T {
var value T
switch any(value).(type) {
case Monochrome:
return monochromeModel(c).(T)
case RGB444:
return rgb444Model(c).(T)
case RGB565:
return rgb565Model(c).(T)
/*
case RGB555:
return rgb555Model(c).(T)
case RGB888:
return rgb888Model(c).(T)
*/
default:
panic("unknown color format")
}
}
func (c RGB565) BitsPerPixel() int {
return 16
}
func (c RGB565) RGBA() (r, g, b, a uint32) {
r = uint32(uint8(c>>12) << 3)
r |= (r >> 5)
r |= (r << 8)
g = uint32(uint8(c>>5) << 2)
g |= (g >> 6)
g |= (g << 8)
b = uint32(uint8(c>>0) << 3)
b |= (b >> 5)
b |= (b << 8)
a = 0xffff
return
}
func (c RGB565) RGBAColor() color.RGBA {
// Note: on ARM, the compiler uses a rev instruction instead of a rev16
// instruction. I wonder whether this can be optimized further to use rev16
// instead?
c = c<<8 | c>>8
color := color.RGBA{
R: uint8(c>>11) << 3,
G: uint8(c>>5) << 2,
B: uint8(c) << 3,
A: 255,
}
// Correct color rounding, so that 0xff roundtrips back to 0xff.
color.R |= color.R >> 5
color.G |= color.G >> 6
color.B |= color.B >> 5
return color
}
+476
View File
@@ -0,0 +1,476 @@
package pixel
import (
"image"
"image/color"
"image/draw"
"unsafe"
)
// Image buffer, used for working with the native image format of various
// displays. It works a lot like a slice: it can be rescaled while reusing the
// underlying buffer and should be passed around by value.
type Image[T Color] struct {
width int16
height int16
data unsafe.Pointer
}
// NewImage creates a new image of the given size.
func NewImage[T Color](width, height int) Image[T] {
if width < 0 || height < 0 || int(int16(width)) != width || int(int16(height)) != height {
// The width/height are stored as 16-bit integers and should never be
// negative.
panic("NewImage: width/height out of bounds")
}
var zeroColor T
var data unsafe.Pointer
switch {
case zeroColor.BitsPerPixel()%8 == 0:
// Typical formats like RGB888 and RGB565.
// Each color starts at a whole byte offset from the start.
buf := make([]T, width*height)
data = unsafe.Pointer(&buf[0])
default:
// Formats like RGB444 that have 12 bits per pixel.
// We access these as bytes, so allocate the buffer as a byte slice.
bufBits := width * height * zeroColor.BitsPerPixel()
bufBytes := (bufBits + 7) / 8
buf := make([]byte, bufBytes)
data = unsafe.Pointer(&buf[0])
}
return Image[T]{
width: int16(width),
height: int16(height),
data: data,
}
}
// NewImageFromBytes creates a new image of the given size using an existing data slice of bytes.
func NewImageFromBytes[T Color](width, height int, buf []byte) Image[T] {
if width < 0 || height < 0 || int(int16(width)) != width || int(int16(height)) != height {
// The width/height are stored as 16-bit integers and should never be
// negative.
panic("NewImageFromBytes: width/height out of bounds")
}
var zeroColor T
var data unsafe.Pointer
switch {
case zeroColor.BitsPerPixel()%8 == 0:
// Typical formats like RGB888 and RGB565.
// Each color starts at a whole byte offset from the start.
if len(buf) != width*height*int(unsafe.Sizeof(zeroColor)) {
panic("NewImageFromBytes: data slice size mismatch")
}
data = unsafe.Pointer(&buf[0])
default:
// Formats like RGB444 that have 12 bits per pixel.
// We access these as bytes, so allocate the buffer as a byte slice.
bufBits := width * height * zeroColor.BitsPerPixel()
bufBytes := (bufBits + 7) / 8
if len(buf) != bufBytes {
panic("NewImageFromBytes: data slice size mismatch")
}
data = unsafe.Pointer(&buf[0])
}
return Image[T]{
width: int16(width),
height: int16(height),
data: data,
}
}
// NewImageFromImage creates a new image from the provided image.
func NewImageFromImage[T Color](src image.Image) Image[T] {
var (
r = src.Bounds()
width = r.Dx()
height = r.Dy()
dst = NewImage[T](width, height)
)
draw.Draw(dst, r, src, image.Point{}, draw.Src)
return dst
}
// Rescale returns a new Image buffer based on the img buffer.
// The contents is undefined after the Rescale operation, and any modification
// to the returned image will overwrite the underlying image buffer in undefined
// ways. It will panic if width*height is larger than img.Len().
func (img Image[T]) Rescale(width, height int) Image[T] {
if width*height > img.Len() {
panic("Image.Rescale size out of bounds")
}
return Image[T]{
width: int16(width),
height: int16(height),
data: img.data,
}
}
// LimitHeight returns a subimage with the bottom part cut off, as specified by
// height.
func (img Image[T]) LimitHeight(height int) Image[T] {
if height < 0 || height > int(img.height) {
panic("Image.LimitHeight: out of bounds")
}
return Image[T]{
width: img.width,
height: int16(height),
data: img.data,
}
}
// Len returns the number of pixels in this image buffer.
func (img Image[T]) Len() int {
return int(img.width) * int(img.height)
}
// RawBuffer returns a byte slice that can be written directly to the screen
// using DrawRGBBitmap8.
func (img Image[T]) RawBuffer() []uint8 {
var zeroColor T
var numBytes int
switch {
case zeroColor.BitsPerPixel()%8 == 0:
// Each color starts at a whole byte offset.
numBytes = int(unsafe.Sizeof(zeroColor)) * int(img.width) * int(img.height)
default:
// Formats like RGB444 that aren't a whole number of bytes.
numBits := zeroColor.BitsPerPixel() * int(img.width) * int(img.height)
numBytes = (numBits + 7) / 8 // round up (see NewImage)
}
return unsafe.Slice((*byte)(img.data), numBytes)
}
// Size returns the image size.
func (img Image[T]) Size() (int, int) {
return int(img.width), int(img.height)
}
func (img Image[T]) setPixel(index int, c T) {
var zeroColor T
switch {
case zeroColor.BitsPerPixel() == 1:
// Monochrome.
offset := index / 8
bits := index % 8
ptr := (*byte)(unsafe.Add(img.data, offset))
if c != zeroColor {
*((*byte)(ptr)) |= (1 << (7 - uint8(bits)))
} else {
*((*byte)(ptr)) &^= (1 << (7 - uint8(bits)))
}
return
case zeroColor.BitsPerPixel()%8 == 0:
// Each color starts at a whole byte offset.
// This is the easy case.
offset := index * int(unsafe.Sizeof(zeroColor))
ptr := unsafe.Add(img.data, offset)
*((*T)(ptr)) = c
return
}
if c, ok := any(c).(RGB444); ok {
// Special case for RGB444.
bitIndex := index * zeroColor.BitsPerPixel()
if bitIndex%8 == 0 {
byteOffset := bitIndex / 8
ptr := (*[2]byte)(unsafe.Add(img.data, byteOffset))
ptr[0] = uint8(c >> 4)
ptr[1] = ptr[1]&0x0f | uint8(c)<<4 // change top bits
} else {
byteOffset := bitIndex / 8
ptr := (*[2]byte)(unsafe.Add(img.data, byteOffset))
ptr[0] = ptr[0]&0xf0 | uint8(c>>8) // change bottom bits
ptr[1] = uint8(c)
}
return
}
// TODO: the code for RGB444 should be generalized to support any bit size.
panic("todo: setPixel for odd bits per pixel")
}
// Set sets the pixel at x, y to the given color.
// Use FillSolidColor to efficiently fill the entire image buffer.
func (img Image[T]) SetPixel(x, y int, c T) {
if uint(x) >= uint(int(img.width)) || uint(y) >= uint(int(img.height)) {
return
}
index := y*int(img.width) + x
img.setPixel(index, c)
}
// Get returns the color at the given index.
func (img Image[T]) GetPixel(x, y int) T {
var zeroColor T
if uint(x) >= uint(int(img.width)) || uint(y) >= uint(int(img.height)) {
return zeroColor
}
index := y*int(img.width) + x // index into img.data
switch {
case zeroColor.BitsPerPixel() == 1:
// Monochrome.
var c Monochrome
offset := index / 8
bits := index % 8
ptr := (*byte)(unsafe.Add(img.data, offset))
c = Monochrome((*ptr >> (7 - uint8(bits))) & 0x1)
return any(c).(T)
case zeroColor.BitsPerPixel()%8 == 0:
// Colors like RGB565, RGB888, etc.
offset := index * int(unsafe.Sizeof(zeroColor))
ptr := unsafe.Add(img.data, offset)
return *((*T)(ptr))
}
if _, ok := any(zeroColor).(RGB444); ok {
// Special case for RGB444 that isn't stored in a neat byte multiple.
bitIndex := index * zeroColor.BitsPerPixel()
var c RGB444
if bitIndex%8 == 0 {
byteOffset := bitIndex / 8
ptr := (*[2]byte)(unsafe.Add(img.data, byteOffset))
c |= RGB444(ptr[0]) << 4
c |= RGB444(ptr[1] >> 4) // load top bits
} else {
byteOffset := bitIndex / 8
ptr := (*[2]byte)(unsafe.Add(img.data, byteOffset))
c |= RGB444(ptr[0]&0x0f) << 8 // load bottom bits
c |= RGB444(ptr[1])
}
return any(c).(T)
}
// TODO: generalize the above code.
panic("todo: Image.Get for odd bits per pixel")
}
// FillSolidColor fills the entire image with the given color.
// This may be faster than setting individual pixels.
func (img Image[T]) FillSolidColor(color T) {
var zeroColor T
switch {
case zeroColor.BitsPerPixel() == 1:
// Monochrome.
var colorByte uint8
if color != zeroColor {
colorByte = 0xff
}
numBytes := int(img.width) * int(img.height) / 8
for i := 0; i < numBytes; i++ {
// TODO: this can be optimized a lot.
// - The store can be done as a 32-bit integer, after checking for
// alignment.
// - Perhaps the loop can be unrolled to improve copy performance.
ptr := (*byte)(unsafe.Add(img.data, i))
*((*byte)(ptr)) = colorByte
}
return
case zeroColor.BitsPerPixel()%8 == 0:
// Fast pass for colors of 8, 16, 24, etc bytes in size.
ptr := img.data
for i := 0; i < img.Len(); i++ {
// TODO: this can be optimized a lot.
// - The store can be done as a 32-bit integer, after checking for
// alignment.
// - Perhaps the loop can be unrolled to improve copy performance.
*(*T)(ptr) = color
ptr = unsafe.Add(ptr, unsafe.Sizeof(zeroColor))
}
return
}
// Special case for RGB444.
if c, ok := any(color).(RGB444); ok {
// RGB444 can be stored in a more optimized way, by storing two colors
// at a time instead of setting each color individually. This avoids
// loading and masking the old color bits for the half-bytes.
var buf [3]uint8
buf[0] = uint8(c >> 4)
buf[1] = uint8(c)<<4 | uint8(c>>8)
buf[2] = uint8(c)
rawBuf := unsafe.Slice((*[3]byte)(img.data), img.Len()/2)
for i := 0; i < len(rawBuf); i++ {
rawBuf[i] = buf
}
if img.Len()%2 != 0 {
// The image contains an uneven number of pixels.
// This is uncommon, but it can happen and we have to handle it.
img.setPixel(img.Len()-1, color)
}
return
}
// Fallback for other color formats.
for i := 0; i < img.Len(); i++ {
img.setPixel(i, color)
}
}
// At implements the draw.Image interface.
func (img Image[T]) At(x, y int) color.Color {
return img.GetPixel(x, y)
}
// Set implements the draw.Image interface.
func (img Image[T]) Set(x, y int, c color.Color) {
var value T
switch any(value).(type) {
case Monochrome:
img.SetPixel(x, y, monochromeModel(c).(T))
case RGB444:
img.SetPixel(x, y, rgb444Model(c).(T))
case RGB565:
img.SetPixel(x, y, rgb565Model(c).(T))
/*
case RGB888:
img.SetPixel(x, y, rgb888Model(c).(T))
case RGB555:
img.SetPixel(x, y, rgb555Model(c).(T))
*/
default:
}
}
// Bounds returns the image bounding box.
func (img Image[T]) Bounds() image.Rectangle {
return image.Rect(0, 0, int(img.width), int(img.height))
}
// ColorModel returns the image color model.
func (img Image[T]) ColorModel() color.Model {
// Ugly cast from color.RGBA to T. The type switch and interface casts are
// trivially optimized away after instantiation.
var value T
switch any(value).(type) {
case Monochrome:
return MonochromeModel
case RGB444:
return RGB444Model
case RGB565:
return RGB565Model
/*
case RGB888:
return RGB888Model
case RGB555:
return RGB555Model
*/
default:
panic("unknown color format")
}
}
// Clear the image buffer.
func (img *Image[T]) Clear() {
var zeroColor T
img.FillSolidColor(zeroColor)
}
// VerticalLSB is a 1 bit (black and white) image.
//
// Each byte is 8 vertical pixels. Each stride is a horizontal band of 8
// pixels high with LSB first. So the first byte represents the following
// pixels, with lowest bit being the top left pixel.
//
// 0 x x x x x x x
// 1 x x x x x x x
// 2 x x x x x x x
// 3 x x x x x x x
// 4 x x x x x x x
// 5 x x x x x x x
// 6 x x x x x x x
// 7 x x x x x x x
//
// It is designed specifically to work with SSD1306 OLED display controller.
type VerticalLSB struct {
// Pix holds the image's pixels, as vertically LSB-first packed bitmap. It
// can be passed directly to ssd1306.Dev.Write()
Pix []byte
// Stride is the Pix stride (in bytes) between vertically adjacent 8 pixels
// horizontal bands.
Stride int
// Rect is the image's bounds.
Rect image.Rectangle
}
// NewVerticalLSB returns an initialized VerticalLSB instance.
func NewVerticalLSB(r image.Rectangle) *VerticalLSB {
w := r.Dx()
// Round down.
minY := r.Min.Y &^ 7
// Round up.
maxY := (r.Max.Y + 7) & ^7
bands := (maxY - minY) / 8
return &VerticalLSB{Pix: make([]byte, w*bands), Stride: w, Rect: r}
}
// ColorModel implements image.Image.
func (i *VerticalLSB) ColorModel() color.Model {
return MonochromeModel
}
// Bounds implements image.Image.
func (i *VerticalLSB) Bounds() image.Rectangle {
return i.Rect
}
// At implements image.Image.
func (i *VerticalLSB) At(x, y int) color.Color {
return i.BitAt(x, y)
}
// BitAt is the optimized version of At().
func (i *VerticalLSB) BitAt(x, y int) Monochrome {
if !(image.Point{x, y}.In(i.Rect)) {
return Off
}
offset, mask := i.PixOffset(x, y)
if i.Pix[offset]&mask != 0 {
return On
}
return Off
}
// Opaque scans the entire image and reports whether it is fully opaque.
func (i *VerticalLSB) Opaque() bool {
return true
}
// PixOffset returns the index of the first element of Pix that corresponds to
// the pixel at (x, y) and the corresponding mask.
func (i *VerticalLSB) PixOffset(x, y int) (int, byte) {
// Adjust band.
minY := i.Rect.Min.Y &^ 7
pY := (y - minY)
offset := pY/8*i.Stride + (x - i.Rect.Min.X)
bit := uint(pY & 7)
return offset, 1 << bit
}
// Set implements draw.Image
func (i *VerticalLSB) Set(x, y int, c color.Color) {
if ToGrayBits(c, 1) == 0 {
i.SetBit(x, y, Off)
}
i.SetBit(x, y, On)
}
// SetBit is the optimized version of Set().
func (i *VerticalLSB) SetBit(x, y int, b Monochrome) {
if !(image.Point{x, y}.In(i.Rect)) {
return
}
offset, mask := i.PixOffset(x, y)
if b == On {
i.Pix[offset] |= mask
} else {
i.Pix[offset] &^= mask
}
}
+33
View File
@@ -0,0 +1,33 @@
package pixel
import "image/color"
var (
MonochromeModel color.Model = color.ModelFunc(monochromeModel)
RGB444Model color.Model = color.ModelFunc(rgb444Model)
RGB565Model color.Model = color.ModelFunc(rgb565Model)
)
func monochromeModel(c color.Color) color.Color {
if _, ok := c.(Monochrome); ok {
return c
}
r, g, b, _ := c.RGBA()
return NewMonochrome(uint8(r>>8), uint8(g>>8), uint8(b>>8))
}
func rgb444Model(c color.Color) color.Color {
if _, ok := c.(RGB444); ok {
return c
}
r, g, b, _ := c.RGBA()
return RGB444(r>>12)<<8 | RGB444(g>>12)<<4 | RGB444(b>>12)
}
func rgb565Model(c color.Color) color.Color {
if _, ok := c.(RGB565); ok {
return c
}
r, g, b, _ := c.RGBA()
return RGB565(uint16((r>>8)&0xF8)<<8 + uint16((g>>8)&0xFC)<<3 + uint16((b>>8)&0xF8)>>3)
}
+152
View File
@@ -0,0 +1,152 @@
package draw
import (
"image"
"image/color"
"image/draw"
"github.com/BeatGlow/beatglow/draw/pixel"
"github.com/sirupsen/logrus"
)
type Scroller struct {
Canvas *Canvas
Width int
Wrap bool
origin image.Point
buff image.Image
scroll int
scrollDir int
scrollWidth int
scrollRequired bool
}
func NewScroller(dst Image, width int, wrap bool) *Scroller {
return &Scroller{
Canvas: NewCanvas(dst),
Width: width,
Wrap: wrap,
scrollDir: +1,
}
}
func (s *Scroller) log() *logrus.Entry {
return logrus.WithField("draw", "scroller")
}
func (s *Scroller) Text(x, y int, text string) {
if text == "" {
s.buff = nil
return
}
width, height := s.Canvas.Style.Font.MeasureText(text)
if s.Wrap && width >= s.Canvas.Image.Bounds().Size().X-x {
width += 42 // 3 * (s.Canvas.Font.GlyphWith + s.Canvas.Font.Spacing)
}
buff := pixel.NewImage[pixel.Monochrome](width, height)
logrus.Printf("scroll color: %#+v", s.Canvas.Style.Foreground)
s.Canvas.Style.Font.Draw(buff, image.Point{}, image.NewUniform(s.Canvas.Style.Foreground), text)
s.scrollRequired = width > (s.Canvas.Image.Bounds().Size().X - x)
if s.Wrap && s.scrollRequired {
s.buff = wrapAround{buff, image.Pt(width, height)}
} else {
s.buff = buff
}
s.origin = image.Pt(x, y)
s.scrollWidth = width
s.log().WithFields(logrus.Fields{
"width": width,
"height": height,
"origin": s.origin,
"scroll": s.scrollRequired,
}).Debug("scroll write text")
}
func (s *Scroller) Advance() {
if s.buff == nil {
return
}
var (
log = s.log()
r image.Rectangle
sp image.Point
)
if s.scrollRequired {
log = log.WithFields(logrus.Fields{
"scroll": s.scroll,
"scroll_new": s.scroll + s.scrollDir,
})
s.scroll += s.scrollDir
if s.scroll > s.scrollWidth {
s.scroll = 0
} else if s.scroll < -s.scrollWidth {
s.scroll = 0
}
// Copy the visible part of the buffer.
r = image.Rectangle{
Min: s.origin,
Max: s.origin.Add(image.Point{X: s.Width, Y: s.buff.Bounds().Dy()}),
}
sp = image.Point{X: s.scroll}
log.WithFields(logrus.Fields{
"bounds": r,
"origin": sp,
}).Trace("scroller advance")
} else {
// Just copy the whole buffered image.
r = image.Rectangle{
Min: s.origin,
Max: s.origin.Add(s.buff.Bounds().Size()),
}
log.WithFields(logrus.Fields{
"bounds": r,
"origin": sp,
}).Trace("scroller advance (static)")
}
draw.Draw(s.Canvas.Image, r, s.buff, sp, draw.Src)
}
func (s *Scroller) Reset() {
s.scroll = 0
}
func (s *Scroller) SetDirection(dir int) {
if dir < 0 {
dir = -1
}
if dir > 0 {
dir = 1
}
s.scrollDir = dir
}
type wrapAround struct {
image.Image
Size image.Point
}
func (i wrapAround) At(x, y int) color.Color {
for x < 0 {
x += i.Size.X
}
for x >= i.Size.X {
x -= i.Size.X
}
return i.Image.At(x, y)
}
func (i wrapAround) Bounds() image.Rectangle {
b := i.Image.Bounds()
b.Min.X -= i.Size.X
b.Max.X += i.Size.X
return b
}
+15
View File
@@ -0,0 +1,15 @@
package draw
import (
"image/color"
"github.com/BeatGlow/beatglow/draw/font"
)
// Text draws text using the specified font and color.
func Text(dst Image, font *font.Font, color color.Color, x, y int, text string) {
if font == nil {
return
}
font.Print(dst, color, x, y, text)
}