From 4c26590b97167279999ca74c9a9fa816a38a9e7a Mon Sep 17 00:00:00 2001 From: Patrick Devine Date: Sat, 28 Aug 2021 14:58:48 -0700 Subject: [PATCH] Add color pngs --- block.go | 32 ++++++++++++++++++++++---------- palette/palette.go | 2 +- sprite.go | 6 ++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/block.go b/block.go index a5e8272..d8a9b1c 100644 --- a/block.go +++ b/block.go @@ -3,9 +3,10 @@ package sprite import ( "os" "strings" + "image/color" "image/png" - //palette "github.com/pdevine/go-asciisprite/palette" + palette "github.com/pdevine/go-asciisprite/palette" tm "github.com/pdevine/go-asciisprite/termbox" ) @@ -133,21 +134,22 @@ func NewSurfaceFromPng(fn string) Surface { // } - b := img.Bounds() - maxR := (b.Max.Y-b.Min.Y) + (b.Max.Y-b.Min.Y)%2 - maxC := (b.Max.X-b.Min.X) + (b.Max.X-b.Min.X)%2 + bnd := img.Bounds() + maxR := (bnd.Max.Y-bnd.Min.Y) + (bnd.Max.Y-bnd.Min.Y)%2 + maxC := (bnd.Max.X-bnd.Min.X) + (bnd.Max.X-bnd.Min.X)%2 // all block sprites must be even m := make([][]rune, maxR, maxR) - for y := 0; y < b.Max.Y-b.Min.Y; y++ { + for y := 0; y < bnd.Max.Y-bnd.Min.Y; y++ { m[y] = make([]rune, maxC, maxC) - for x := 0; x < b.Max.X-b.Min.X; x++ { - c := img.At(x+b.Min.X, y+b.Min.Y) + for x := 0; x < bnd.Max.X-bnd.Min.X; x++ { + c := img.At(x+bnd.Min.X, y+bnd.Min.Y) //m[y][x] = rune(palette.Index(c)) - r, g, b, _ := c.RGBA() - if r != 0 || g != 0 || b != 0 { - m[y][x] = 'X' + r, g, b, a := c.RGBA() + i := palette.Index(color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}) + if i > -1 { + m[y][x] = getRuneFromColorMap(i) } } } @@ -317,6 +319,7 @@ func (s Surface) Line(x0, y0, x1, y1 int, ch rune) error { return nil } +// Draw a rectangle on a Surface func (s Surface) Rectangle(x0, y0, x1, y1 int, ch rune) error { if x0 >= s.Width || x1 >= s.Width { // XXX - put a real error here @@ -332,3 +335,12 @@ func (s Surface) Rectangle(x0, y0, x1, y1 int, ch rune) error { return nil } +func getRuneFromColorMap(idx int) rune { + for k, v := range ColorMap { + if v == tm.Attribute(idx) { + return k + } + } + ColorMap[rune(idx)] = tm.Attribute(idx) + return rune(idx) +} diff --git a/palette/palette.go b/palette/palette.go index f75ee85..97cb985 100644 --- a/palette/palette.go +++ b/palette/palette.go @@ -264,7 +264,7 @@ var Xterm = []color.Color{ func Index(t color.RGBA) int { for cnt, c := range Xterm { if t == c { - return cnt + return cnt+1 } } return -1 diff --git a/sprite.go b/sprite.go index 8b8eeb6..ab405b9 100644 --- a/sprite.go +++ b/sprite.go @@ -279,3 +279,9 @@ func (sg *SpriteGroup) Remove(s Sprite) { func (sg *SpriteGroup) RemoveAll() { sg.Sprites = []Sprite{} } + +// MoveToTop renders a sprite over all other sprites in the SpriteGroup. +func (sg *SpriteGroup) MoveToTop(s Sprite) { + sg.Remove(s) + sg.Sprites = append(sg.Sprites, s) +}