Skip to content

Commit

Permalink
Add color pngs
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Devine committed Aug 28, 2021
1 parent e307c54 commit 4c26590
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
32 changes: 22 additions & 10 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion palette/palette.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 4c26590

Please sign in to comment.