pixel-sprite-maker: spritec - .sprite text format -> png/jpg/svg + sprite sheets

- .sprite format: single-char palette keys (hex/rgb()/CSS names/none), grid
  rows, '.' transparent by default, max 256x256 per sprite
- render/sheet/info/preview subcommands; sheets name themselves
  <base>_<cellW>x<cellH>_<cols>x<rows>.<ext> (row-major)
- SVG output RLE-merges pixel runs; JPG composites over --bg
- go tests for parser, colors, scaling, svg, sheet layout

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MmdG9GqfSWCzts7AkDwRDh
This commit is contained in:
2026-07-14 02:21:57 +02:00
parent 581e37acd8
commit 23135a96d1
21 changed files with 1444 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
build/
*.exe
mesh-tool/examples/downloads/

View File

@@ -0,0 +1,86 @@
# pixel-sprite-maker (`spritec`)
Turns human/agent-readable `.sprite` text files into pixel art (**PNG, JPG, SVG**)
and combines several sprites into **sprite sheets / animation strips**.
Written in Go, zero dependencies, single static binary.
## Build
```bash
# Arch/Garuda: sudo pacman -S go
cd pixel-sprite-maker
go build -o build/spritec . # or run the VS Code task "build pixel-sprite-maker"
go test ./...
```
## The `.sprite` format
One file = one sprite. Designed so an agent can *see* the result in the text:
```
# comment lines start with '#'
sprite: coin # optional name
palette:
k = #1A1205 dark outline
y = #FFD700 gold
Y = gold CSS names work too — text after the color is a comment
grid:
..kkkk..
.kyyyYk.
kyyyYYyk
..kkkk..
```
Rules:
- **One character = one pixel.** Every grid row must be the same width.
- Palette keys are exactly one character (any unicode). `#`, `=`, `:` and
whitespace are not allowed as keys — so the number of colors is
practically unlimited (a-z, A-Z, 0-9, `!@%&*`, unicode…).
- `.` is **transparent by default**; override it in the palette if you want.
- Colors: `#RGB`, `#RGBA`, `#RRGGBB`, `#RRGGBBAA`, `rgb(r,g,b)`,
`rgba(r,g,b,a)` (alpha 0-255 or 0.0-1.0), all CSS named colors, `none`.
- Max **256x256** pixels per sprite. Sheets may exceed this; single frames may not.
## Commands
```bash
spritec render coin.sprite -o coin.png --scale 8 # png/jpg/svg from extension
spritec render coin.sprite --format svg # -> coin.svg
spritec render heart.sprite -o heart.jpg --bg '#222034' # jpg has no alpha: pick bg
spritec info coin.sprite # validate + palette stats
spritec preview coin.sprite # draw in the terminal (truecolor)
# Sprite sheets / animations — row-major (left→right, then top→bottom):
spritec sheet walk_1.sprite walk_2.sprite walk_3.sprite walk_4.sprite -o walk # 1 row
spritec sheet walk_*.sprite --cols 2 -o walkgrid # 2D: 2 columns x 2 rows
```
Flags: `-o` output, `--format png|jpg|svg`, `--scale n` (integer nearest-neighbour
upscale), `--bg color` + `--quality 1-100` (jpg only), `--cols n` (sheet only).
## Sheet output naming — read the layout from the file name
```
<name>_<cellW>x<cellH>_<cols>x<rows>.<ext>
walk_8x8_4x1.png = 8x8 px per frame, 4 columns, 1 row (animation strip)
tiles_16x16_4x2.png = 16x16 px per frame, 4 columns, 2 rows
```
Frame order is always **row-major**: index = `row * cols + col`, frame 0 is
top-left. If the frame count doesn't fill the grid, trailing cells are
transparent. In game code:
```
frameX = (index % cols) * cellW
frameY = (index / cols) * cellH
```
## Exit codes & errors
`0` on success, `1` on any error, `2` on missing command. Parse errors name
the exact line/row/column and what was expected, so an agent can fix the
file without guessing.
Examples live in [`examples/`](examples/); generated output in `examples/out/`.

View File

@@ -0,0 +1,17 @@
# A small gold coin, 8x8.
# '.' is transparent by default and never needs a palette entry.
sprite: coin
palette:
k = #1A1205 dark outline
y = #FFD700 gold
Y = #FFF3A0 highlight
s = #B8860B shadow
grid:
..kkkk..
.kyyyYk.
kyyyYYsk
kyyYyysk
kyYyyysk
kYyyyssk
.kysssk.
..kkkk..

View File

@@ -0,0 +1,25 @@
# A 16x16 heart with shading, shows named colors and rgb().
sprite: heart
palette:
k = #2B0A10 outline
r = crimson main red
R = rgb(255, 90, 110) light red
d = #8B1A2B dark red
w = #FFFFFFCC semi-transparent shine
grid:
................
..kkk....kkk....
.krrRk..kRrrk...
krrRRrkkrRrrrk..
krRwRrrrrrrrdk..
krRwwRrrrrrrdk..
krRwRrrrrrrddk..
krrRrrrrrrrddk..
.krrrrrrrrrdk...
..krrrrrrrdk....
...krrrrrdk.....
....krrrdk......
.....krdk.......
......kk........
................
................

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 B

View File

@@ -0,0 +1,34 @@
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 8 8" shape-rendering="crispEdges">
<rect x="2" y="0" width="4" height="1" fill="#1a1205"/>
<rect x="1" y="1" width="1" height="1" fill="#1a1205"/>
<rect x="2" y="1" width="3" height="1" fill="#ffd700"/>
<rect x="5" y="1" width="1" height="1" fill="#fff3a0"/>
<rect x="6" y="1" width="1" height="1" fill="#1a1205"/>
<rect x="0" y="2" width="1" height="1" fill="#1a1205"/>
<rect x="1" y="2" width="3" height="1" fill="#ffd700"/>
<rect x="4" y="2" width="2" height="1" fill="#fff3a0"/>
<rect x="6" y="2" width="1" height="1" fill="#b8860b"/>
<rect x="7" y="2" width="1" height="1" fill="#1a1205"/>
<rect x="0" y="3" width="1" height="1" fill="#1a1205"/>
<rect x="1" y="3" width="2" height="1" fill="#ffd700"/>
<rect x="3" y="3" width="1" height="1" fill="#fff3a0"/>
<rect x="4" y="3" width="2" height="1" fill="#ffd700"/>
<rect x="6" y="3" width="1" height="1" fill="#b8860b"/>
<rect x="7" y="3" width="1" height="1" fill="#1a1205"/>
<rect x="0" y="4" width="1" height="1" fill="#1a1205"/>
<rect x="1" y="4" width="1" height="1" fill="#ffd700"/>
<rect x="2" y="4" width="1" height="1" fill="#fff3a0"/>
<rect x="3" y="4" width="3" height="1" fill="#ffd700"/>
<rect x="6" y="4" width="1" height="1" fill="#b8860b"/>
<rect x="7" y="4" width="1" height="1" fill="#1a1205"/>
<rect x="0" y="5" width="1" height="1" fill="#1a1205"/>
<rect x="1" y="5" width="1" height="1" fill="#fff3a0"/>
<rect x="2" y="5" width="3" height="1" fill="#ffd700"/>
<rect x="5" y="5" width="2" height="1" fill="#b8860b"/>
<rect x="7" y="5" width="1" height="1" fill="#1a1205"/>
<rect x="1" y="6" width="1" height="1" fill="#1a1205"/>
<rect x="2" y="6" width="1" height="1" fill="#ffd700"/>
<rect x="3" y="6" width="3" height="1" fill="#b8860b"/>
<rect x="6" y="6" width="1" height="1" fill="#1a1205"/>
<rect x="2" y="7" width="4" height="1" fill="#1a1205"/>
</svg>

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 508 B

View File

@@ -0,0 +1,57 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" shape-rendering="crispEdges">
<rect x="2" y="0" width="3" height="1" fill="#101018"/>
<rect x="10" y="0" width="3" height="1" fill="#101018"/>
<rect x="2" y="1" width="1" height="1" fill="#101018"/>
<rect x="3" y="1" width="2" height="1" fill="#f2c09a"/>
<rect x="10" y="1" width="1" height="1" fill="#101018"/>
<rect x="11" y="1" width="2" height="1" fill="#f2c09a"/>
<rect x="2" y="2" width="1" height="1" fill="#101018"/>
<rect x="3" y="2" width="2" height="1" fill="#f2c09a"/>
<rect x="10" y="2" width="1" height="1" fill="#101018"/>
<rect x="11" y="2" width="2" height="1" fill="#f2c09a"/>
<rect x="1" y="3" width="1" height="1" fill="#101018"/>
<rect x="2" y="3" width="3" height="1" fill="#2e5fb7"/>
<rect x="5" y="3" width="1" height="1" fill="#101018"/>
<rect x="9" y="3" width="1" height="1" fill="#101018"/>
<rect x="10" y="3" width="3" height="1" fill="#2e5fb7"/>
<rect x="13" y="3" width="1" height="1" fill="#101018"/>
<rect x="2" y="4" width="3" height="1" fill="#2e5fb7"/>
<rect x="10" y="4" width="3" height="1" fill="#2e5fb7"/>
<rect x="2" y="5" width="3" height="1" fill="#22406e"/>
<rect x="10" y="5" width="3" height="1" fill="#22406e"/>
<rect x="2" y="6" width="1" height="1" fill="#22406e"/>
<rect x="4" y="6" width="1" height="1" fill="#22406e"/>
<rect x="9" y="6" width="1" height="1" fill="#22406e"/>
<rect x="13" y="6" width="1" height="1" fill="#22406e"/>
<rect x="2" y="7" width="1" height="1" fill="#101018"/>
<rect x="4" y="7" width="1" height="1" fill="#101018"/>
<rect x="9" y="7" width="1" height="1" fill="#101018"/>
<rect x="13" y="7" width="1" height="1" fill="#101018"/>
<rect x="2" y="8" width="3" height="1" fill="#101018"/>
<rect x="10" y="8" width="3" height="1" fill="#101018"/>
<rect x="2" y="9" width="1" height="1" fill="#101018"/>
<rect x="3" y="9" width="2" height="1" fill="#f2c09a"/>
<rect x="10" y="9" width="1" height="1" fill="#101018"/>
<rect x="11" y="9" width="2" height="1" fill="#f2c09a"/>
<rect x="2" y="10" width="1" height="1" fill="#101018"/>
<rect x="3" y="10" width="2" height="1" fill="#f2c09a"/>
<rect x="10" y="10" width="1" height="1" fill="#101018"/>
<rect x="11" y="10" width="2" height="1" fill="#f2c09a"/>
<rect x="2" y="11" width="3" height="1" fill="#2e5fb7"/>
<rect x="5" y="11" width="1" height="1" fill="#101018"/>
<rect x="9" y="11" width="1" height="1" fill="#101018"/>
<rect x="10" y="11" width="3" height="1" fill="#2e5fb7"/>
<rect x="13" y="11" width="1" height="1" fill="#101018"/>
<rect x="1" y="12" width="1" height="1" fill="#101018"/>
<rect x="2" y="12" width="3" height="1" fill="#2e5fb7"/>
<rect x="10" y="12" width="3" height="1" fill="#2e5fb7"/>
<rect x="2" y="13" width="3" height="1" fill="#22406e"/>
<rect x="10" y="13" width="3" height="1" fill="#22406e"/>
<rect x="2" y="14" width="1" height="1" fill="#22406e"/>
<rect x="4" y="14" width="1" height="1" fill="#22406e"/>
<rect x="10" y="14" width="2" height="1" fill="#22406e"/>
<rect x="2" y="15" width="1" height="1" fill="#101018"/>
<rect x="4" y="15" width="1" height="1" fill="#101018"/>
<rect x="9" y="15" width="1" height="1" fill="#101018"/>
<rect x="12" y="15" width="1" height="1" fill="#101018"/>
</svg>

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@@ -0,0 +1,16 @@
# Frame 1/4 of a tiny walking guy, 8x8. Legs together.
sprite: walk_1
palette:
k = #101018 outline / hair
s = #F2C09A skin
b = #2E5FB7 shirt
d = #22406E pants
grid:
..kkk...
..kss...
..kss...
.kbbbk..
..bbb...
..ddd...
..d.d...
..k.k...

View File

@@ -0,0 +1,16 @@
# Frame 2/4 - right leg forward.
sprite: walk_2
palette:
k = #101018 outline / hair
s = #F2C09A skin
b = #2E5FB7 shirt
d = #22406E pants
grid:
..kkk...
..kss...
..kss...
.kbbbk..
..bbb...
..ddd...
.d...d..
.k...k..

View File

@@ -0,0 +1,16 @@
# Frame 3/4 - legs together again (same pose as 1, arms swung).
sprite: walk_3
palette:
k = #101018 outline / hair
s = #F2C09A skin
b = #2E5FB7 shirt
d = #22406E pants
grid:
..kkk...
..kss...
..kss...
..bbbk..
.kbbb...
..ddd...
..d.d...
..k.k...

View File

@@ -0,0 +1,16 @@
# Frame 4/4 - left leg forward.
sprite: walk_4
palette:
k = #101018 outline / hair
s = #F2C09A skin
b = #2E5FB7 shirt
d = #22406E pants
grid:
..kkk...
..kss...
..kss...
.kbbbk..
..bbb...
..ddd...
..dd....
.k..k...

View File

@@ -0,0 +1,3 @@
module gitea.brasse-pc.eu/brasse/agent-tools/pixel-sprite-maker
go 1.24

313
pixel-sprite-maker/main.go Normal file
View File

@@ -0,0 +1,313 @@
// spritec turns agent-friendly .sprite text files into PNG/JPG/SVG pixel
// art, and combines several sprites into sprite sheets / animation strips.
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"gitea.brasse-pc.eu/brasse/agent-tools/pixel-sprite-maker/sprite"
)
var version = "dev"
const usage = `spritec - pixel sprite maker for agents
Usage:
spritec render <file.sprite> [flags] render one sprite to png/jpg/svg
spritec sheet <a.sprite> <b.sprite> ... [flags]
combine sprites into one sheet image
spritec info <file.sprite> validate a file and print its stats
spritec preview <file.sprite> draw the sprite in the terminal
spritec version print version
Render flags:
-o <path> output file; its extension picks the format (.png .jpg .svg)
--format <f> png | jpg | svg (default: from -o, else png)
--scale <n> integer upscale factor, default 1
--bg <color> jpg background color (jpg has no alpha), default #FFFFFF
--quality <n> jpg quality 1-100, default 90
Sheet flags (in addition to the render flags):
-o <name> output base name; layout is appended automatically
--cols <n> number of columns, row-major order (default: one row)
Sheet output naming:
<name>_<cellW>x<cellH>_<cols>x<rows>.<ext>
e.g. walk_16x16_4x2.png = 16x16 px per frame, 4 columns, 2 rows,
frames are read left-to-right then top-to-bottom (row-major).
The .sprite format:
# comment
sprite: coin optional name
palette:
. = none '.' is transparent by default
k = #000000 hex, rgb(...), CSS names ('gold') and 'none' work
y = gold anything after the color is a comment
grid:
..kk..
.kyyk.
.kyyk.
..kk..
Each grid character is one pixel; every row must be the same width.
Max sprite size: 256x256. Sheets may be bigger, single frames may not.
`
func main() {
if len(os.Args) < 2 {
fmt.Print(usage)
os.Exit(2)
}
switch os.Args[1] {
case "render":
cmdRender(os.Args[2:])
case "sheet":
cmdSheet(os.Args[2:])
case "info":
cmdInfo(os.Args[2:])
case "preview":
cmdPreview(os.Args[2:])
case "version", "--version", "-v":
fmt.Println("spritec", version)
case "help", "--help", "-h":
fmt.Print(usage)
default:
die("unknown command %q — run 'spritec help'", os.Args[1])
}
}
// parseInterspersed lets flags appear before or after positional args
// (stdlib flag stops at the first positional otherwise).
func parseInterspersed(fs *flag.FlagSet, args []string) {
var flags, pos []string
for i := 0; i < len(args); i++ {
a := args[i]
if len(a) > 1 && a[0] == '-' {
flags = append(flags, a)
name := strings.TrimLeft(a, "-")
if !strings.Contains(name, "=") {
f := fs.Lookup(name)
isBool := false
if f != nil {
if bv, ok := f.Value.(interface{ IsBoolFlag() bool }); ok && bv.IsBoolFlag() {
isBool = true
}
}
if !isBool && i+1 < len(args) {
i++
flags = append(flags, args[i])
}
}
} else {
pos = append(pos, a)
}
}
fs.Parse(append(flags, pos...))
}
type renderFlags struct {
out string
format string
scale int
bg string
quality int
}
func addRenderFlags(fs *flag.FlagSet) *renderFlags {
rf := &renderFlags{}
fs.StringVar(&rf.out, "o", "", "output path")
fs.StringVar(&rf.format, "format", "", "png|jpg|svg")
fs.IntVar(&rf.scale, "scale", 1, "integer upscale factor")
fs.StringVar(&rf.bg, "bg", "#FFFFFF", "jpg background color")
fs.IntVar(&rf.quality, "quality", 90, "jpg quality 1-100")
return rf
}
// resolveFormat picks the output format from --format or the -o extension.
func (rf *renderFlags) resolveFormat() (string, error) {
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(rf.out), "."))
if ext == "jpeg" {
ext = "jpg"
}
f := strings.ToLower(rf.format)
switch {
case f == "" && ext == "":
return "png", nil
case f == "":
f = ext
case ext != "" && ext != f:
return "", fmt.Errorf("--format %s conflicts with output extension .%s", f, ext)
}
switch f {
case "png", "jpg", "svg":
return f, nil
}
return "", fmt.Errorf("unsupported format %q (png, jpg or svg)", f)
}
func writeGrid(path, format string, g sprite.PixelGrid, rf *renderFlags) error {
bg, err := sprite.ParseColor(rf.bg)
if err != nil {
return fmt.Errorf("--bg: %w", err)
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
switch format {
case "png":
err = sprite.WritePNG(f, g, rf.scale)
case "jpg":
err = sprite.WriteJPG(f, g, rf.scale, bg, rf.quality)
case "svg":
err = sprite.WriteSVG(f, g, rf.scale)
}
if err != nil {
return err
}
return f.Close()
}
func cmdRender(args []string) {
fs := flag.NewFlagSet("render", flag.ExitOnError)
rf := addRenderFlags(fs)
parseInterspersed(fs, args)
if fs.NArg() != 1 {
die("render takes exactly one .sprite file")
}
in := fs.Arg(0)
s, err := sprite.ParseFile(in)
if err != nil {
die("%v", err)
}
format, err := rf.resolveFormat()
if err != nil {
die("%v", err)
}
out := rf.out
if out == "" {
out = strings.TrimSuffix(in, filepath.Ext(in)) + "." + format
}
if err := writeGrid(out, format, s, rf); err != nil {
die("%v", err)
}
w, h := s.Bounds()
fmt.Printf("%s (%dx%d px, scale %d -> %dx%d)\n", out, w, h, rf.scale, w*rf.scale, h*rf.scale)
}
func cmdSheet(args []string) {
fs := flag.NewFlagSet("sheet", flag.ExitOnError)
rf := addRenderFlags(fs)
cols := fs.Int("cols", 0, "columns in the sheet (default: all sprites in one row)")
parseInterspersed(fs, args)
if fs.NArg() < 1 {
die("sheet needs at least one .sprite file")
}
var sprites []*sprite.Sprite
for _, path := range fs.Args() {
s, err := sprite.ParseFile(path)
if err != nil {
die("%v", err)
}
sprites = append(sprites, s)
}
sh, err := sprite.NewSheet(sprites, *cols)
if err != nil {
die("%v", err)
}
format, err := rf.resolveFormat()
if err != nil {
die("%v", err)
}
base := rf.out
if base == "" {
base = "sheet"
}
base = strings.TrimSuffix(base, filepath.Ext(base))
out := sh.FileBase(base) + "." + format
if err := writeGrid(out, format, sh, rf); err != nil {
die("%v", err)
}
w, h := sh.Bounds()
fmt.Printf("%s (%d frames of %dx%d px in %dx%d grid, total %dx%d px)\n",
out, len(sprites), sh.CellW, sh.CellH, sh.Cols, sh.Rows, w*rf.scale, h*rf.scale)
}
func cmdInfo(args []string) {
if len(args) != 1 {
die("info takes exactly one .sprite file")
}
s, err := sprite.ParseFile(args[0])
if err != nil {
die("%v", err)
}
fmt.Printf("sprite: %s\n", s.Name)
fmt.Printf("size: %dx%d px\n", s.W, s.H)
fmt.Printf("palette: %d colors\n", len(s.Keys))
usage := s.UsageCount()
for _, k := range s.Keys {
note := ""
if usage[k] == 0 {
note = " (unused)"
}
fmt.Printf(" %s = %-9s %5d px%s\n", string(k), sprite.FormatColor(s.Palette[k]), usage[k], note)
}
if _, defined := usage['.']; defined && !contains(s.Keys, '.') {
fmt.Printf(" . = none %5d px (implicit transparent)\n", usage['.'])
}
fmt.Println("valid: yes")
}
func contains(keys []rune, k rune) bool {
for _, x := range keys {
if x == k {
return true
}
}
return false
}
// cmdPreview draws the sprite with truecolor half-blocks, two pixel rows
// per terminal line. Transparent pixels show the terminal background.
func cmdPreview(args []string) {
if len(args) != 1 {
die("preview takes exactly one .sprite file")
}
s, err := sprite.ParseFile(args[0])
if err != nil {
die("%v", err)
}
const reset = "\x1b[0m"
var b strings.Builder
for y := 0; y < s.H; y += 2 {
for x := 0; x < s.W; x++ {
top, bot := s.At(x, y), sprite.Transparent
if y+1 < s.H {
bot = s.At(x, y+1)
}
topOn, botOn := top.A >= 128, bot.A >= 128
switch {
case topOn && botOn:
fmt.Fprintf(&b, "\x1b[38;2;%d;%d;%dm\x1b[48;2;%d;%d;%dm▀%s", top.R, top.G, top.B, bot.R, bot.G, bot.B, reset)
case topOn:
fmt.Fprintf(&b, "\x1b[38;2;%d;%d;%dm▀%s", top.R, top.G, top.B, reset)
case botOn:
fmt.Fprintf(&b, "\x1b[38;2;%d;%d;%dm▄%s", bot.R, bot.G, bot.B, reset)
default:
b.WriteByte(' ')
}
}
b.WriteByte('\n')
}
fmt.Printf("%s %dx%d px\n%s", s.Name, s.W, s.H, b.String())
}
func die(format string, a ...any) {
fmt.Fprintf(os.Stderr, "spritec: "+format+"\n", a...)
os.Exit(1)
}

View File

@@ -0,0 +1,270 @@
package sprite
import (
"fmt"
"image/color"
"strconv"
"strings"
)
// Transparent is the color used for pixels that should not be drawn.
var Transparent = color.NRGBA{0, 0, 0, 0}
// ParseColor accepts:
//
// none | transparent | - -> fully transparent
// #RGB #RGBA #RRGGBB #RRGGBBAA -> hex
// rgb(r,g,b) rgba(r,g,b,a) -> 0-255 channels, alpha 0-255 or 0.0-1.0
// CSS named colors -> "red", "rebeccapurple", ...
func ParseColor(s string) (color.NRGBA, error) {
t := strings.ToLower(strings.TrimSpace(s))
if t == "" {
return Transparent, fmt.Errorf("empty color value")
}
switch t {
case "none", "transparent", "-":
return Transparent, nil
}
if strings.HasPrefix(t, "#") {
return parseHex(t)
}
if strings.HasPrefix(t, "rgb(") || strings.HasPrefix(t, "rgba(") {
return parseRGBFunc(t)
}
if c, ok := cssColors[t]; ok {
return c, nil
}
return Transparent, fmt.Errorf("unknown color %q (use #RRGGBB, rgb(r,g,b), a CSS color name, or 'none')", s)
}
func parseHex(t string) (color.NRGBA, error) {
h := t[1:]
var r, g, b, a uint64
var err error
dup := func(s string) (uint64, error) {
v, err := strconv.ParseUint(s, 16, 8)
return v*16 + v, err
}
a = 255
switch len(h) {
case 3, 4:
if r, err = dup(h[0:1]); err == nil {
if g, err = dup(h[1:2]); err == nil {
b, err = dup(h[2:3])
}
}
if err == nil && len(h) == 4 {
a, err = dup(h[3:4])
}
case 6, 8:
if r, err = strconv.ParseUint(h[0:2], 16, 8); err == nil {
if g, err = strconv.ParseUint(h[2:4], 16, 8); err == nil {
b, err = strconv.ParseUint(h[4:6], 16, 8)
}
}
if err == nil && len(h) == 8 {
a, err = strconv.ParseUint(h[6:8], 16, 8)
}
default:
return Transparent, fmt.Errorf("hex color %q must be #RGB, #RGBA, #RRGGBB or #RRGGBBAA", t)
}
if err != nil {
return Transparent, fmt.Errorf("invalid hex color %q", t)
}
return color.NRGBA{uint8(r), uint8(g), uint8(b), uint8(a)}, nil
}
func parseRGBFunc(t string) (color.NRGBA, error) {
open := strings.Index(t, "(")
close := strings.Index(t, ")")
if close < open {
return Transparent, fmt.Errorf("invalid color %q: missing ')'", t)
}
parts := strings.Split(t[open+1:close], ",")
if len(parts) != 3 && len(parts) != 4 {
return Transparent, fmt.Errorf("invalid color %q: want rgb(r,g,b) or rgba(r,g,b,a)", t)
}
var ch [4]uint8
ch[3] = 255
for i, p := range parts {
p = strings.TrimSpace(p)
if i == 3 && strings.Contains(p, ".") {
// CSS-style fractional alpha 0.0 - 1.0
f, err := strconv.ParseFloat(p, 64)
if err != nil || f < 0 || f > 1 {
return Transparent, fmt.Errorf("invalid alpha %q in %q (want 0.0-1.0 or 0-255)", p, t)
}
ch[3] = uint8(f*255 + 0.5)
continue
}
v, err := strconv.ParseUint(p, 10, 8)
if err != nil {
return Transparent, fmt.Errorf("invalid channel %q in %q (want 0-255)", p, t)
}
ch[i] = uint8(v)
}
return color.NRGBA{ch[0], ch[1], ch[2], ch[3]}, nil
}
// FormatColor renders a color the way it should appear in a .sprite file.
func FormatColor(c color.NRGBA) string {
if c.A == 0 {
return "none"
}
if c.A == 255 {
return fmt.Sprintf("#%02X%02X%02X", c.R, c.G, c.B)
}
return fmt.Sprintf("#%02X%02X%02X%02X", c.R, c.G, c.B, c.A)
}
// cssColors is the full CSS Color Module Level 4 named-color list.
var cssColors = map[string]color.NRGBA{
"aliceblue": {240, 248, 255, 255},
"antiquewhite": {250, 235, 215, 255},
"aqua": {0, 255, 255, 255},
"aquamarine": {127, 255, 212, 255},
"azure": {240, 255, 255, 255},
"beige": {245, 245, 220, 255},
"bisque": {255, 228, 196, 255},
"black": {0, 0, 0, 255},
"blanchedalmond": {255, 235, 205, 255},
"blue": {0, 0, 255, 255},
"blueviolet": {138, 43, 226, 255},
"brown": {165, 42, 42, 255},
"burlywood": {222, 184, 135, 255},
"cadetblue": {95, 158, 160, 255},
"chartreuse": {127, 255, 0, 255},
"chocolate": {210, 105, 30, 255},
"coral": {255, 127, 80, 255},
"cornflowerblue": {100, 149, 237, 255},
"cornsilk": {255, 248, 220, 255},
"crimson": {220, 20, 60, 255},
"cyan": {0, 255, 255, 255},
"darkblue": {0, 0, 139, 255},
"darkcyan": {0, 139, 139, 255},
"darkgoldenrod": {184, 134, 11, 255},
"darkgray": {169, 169, 169, 255},
"darkgreen": {0, 100, 0, 255},
"darkgrey": {169, 169, 169, 255},
"darkkhaki": {189, 183, 107, 255},
"darkmagenta": {139, 0, 139, 255},
"darkolivegreen": {85, 107, 47, 255},
"darkorange": {255, 140, 0, 255},
"darkorchid": {153, 50, 204, 255},
"darkred": {139, 0, 0, 255},
"darksalmon": {233, 150, 122, 255},
"darkseagreen": {143, 188, 143, 255},
"darkslateblue": {72, 61, 139, 255},
"darkslategray": {47, 79, 79, 255},
"darkslategrey": {47, 79, 79, 255},
"darkturquoise": {0, 206, 209, 255},
"darkviolet": {148, 0, 211, 255},
"deeppink": {255, 20, 147, 255},
"deepskyblue": {0, 191, 255, 255},
"dimgray": {105, 105, 105, 255},
"dimgrey": {105, 105, 105, 255},
"dodgerblue": {30, 144, 255, 255},
"firebrick": {178, 34, 34, 255},
"floralwhite": {255, 250, 240, 255},
"forestgreen": {34, 139, 34, 255},
"fuchsia": {255, 0, 255, 255},
"gainsboro": {220, 220, 220, 255},
"ghostwhite": {248, 248, 255, 255},
"gold": {255, 215, 0, 255},
"goldenrod": {218, 165, 32, 255},
"gray": {128, 128, 128, 255},
"green": {0, 128, 0, 255},
"greenyellow": {173, 255, 47, 255},
"grey": {128, 128, 128, 255},
"honeydew": {240, 255, 240, 255},
"hotpink": {255, 105, 180, 255},
"indianred": {205, 92, 92, 255},
"indigo": {75, 0, 130, 255},
"ivory": {255, 255, 240, 255},
"khaki": {240, 230, 140, 255},
"lavender": {230, 230, 250, 255},
"lavenderblush": {255, 240, 245, 255},
"lawngreen": {124, 252, 0, 255},
"lemonchiffon": {255, 250, 205, 255},
"lightblue": {173, 216, 230, 255},
"lightcoral": {240, 128, 128, 255},
"lightcyan": {224, 255, 255, 255},
"lightgoldenrodyellow": {250, 250, 210, 255},
"lightgray": {211, 211, 211, 255},
"lightgreen": {144, 238, 144, 255},
"lightgrey": {211, 211, 211, 255},
"lightpink": {255, 182, 193, 255},
"lightsalmon": {255, 160, 122, 255},
"lightseagreen": {32, 178, 170, 255},
"lightskyblue": {135, 206, 250, 255},
"lightslategray": {119, 136, 153, 255},
"lightslategrey": {119, 136, 153, 255},
"lightsteelblue": {176, 196, 222, 255},
"lightyellow": {255, 255, 224, 255},
"lime": {0, 255, 0, 255},
"limegreen": {50, 205, 50, 255},
"linen": {250, 240, 230, 255},
"magenta": {255, 0, 255, 255},
"maroon": {128, 0, 0, 255},
"mediumaquamarine": {102, 205, 170, 255},
"mediumblue": {0, 0, 205, 255},
"mediumorchid": {186, 85, 211, 255},
"mediumpurple": {147, 112, 219, 255},
"mediumseagreen": {60, 179, 113, 255},
"mediumslateblue": {123, 104, 238, 255},
"mediumspringgreen": {0, 250, 154, 255},
"mediumturquoise": {72, 209, 204, 255},
"mediumvioletred": {199, 21, 133, 255},
"midnightblue": {25, 25, 112, 255},
"mintcream": {245, 255, 250, 255},
"mistyrose": {255, 228, 225, 255},
"moccasin": {255, 228, 181, 255},
"navajowhite": {255, 222, 173, 255},
"navy": {0, 0, 128, 255},
"oldlace": {253, 245, 230, 255},
"olive": {128, 128, 0, 255},
"olivedrab": {107, 142, 35, 255},
"orange": {255, 165, 0, 255},
"orangered": {255, 69, 0, 255},
"orchid": {218, 112, 214, 255},
"palegoldenrod": {238, 232, 170, 255},
"palegreen": {152, 251, 152, 255},
"paleturquoise": {175, 238, 238, 255},
"palevioletred": {219, 112, 147, 255},
"papayawhip": {255, 239, 213, 255},
"peachpuff": {255, 218, 185, 255},
"peru": {205, 133, 63, 255},
"pink": {255, 192, 203, 255},
"plum": {221, 160, 221, 255},
"powderblue": {176, 224, 230, 255},
"purple": {128, 0, 128, 255},
"rebeccapurple": {102, 51, 153, 255},
"red": {255, 0, 0, 255},
"rosybrown": {188, 143, 143, 255},
"royalblue": {65, 105, 225, 255},
"saddlebrown": {139, 69, 19, 255},
"salmon": {250, 128, 114, 255},
"sandybrown": {244, 164, 96, 255},
"seagreen": {46, 139, 87, 255},
"seashell": {255, 245, 238, 255},
"sienna": {160, 82, 45, 255},
"silver": {192, 192, 192, 255},
"skyblue": {135, 206, 235, 255},
"slateblue": {106, 90, 205, 255},
"slategray": {112, 128, 144, 255},
"slategrey": {112, 128, 144, 255},
"snow": {255, 250, 250, 255},
"springgreen": {0, 255, 127, 255},
"steelblue": {70, 130, 180, 255},
"tan": {210, 180, 140, 255},
"teal": {0, 128, 128, 255},
"thistle": {216, 191, 216, 255},
"tomato": {255, 99, 71, 255},
"turquoise": {64, 224, 208, 255},
"violet": {238, 130, 238, 255},
"wheat": {245, 222, 179, 255},
"white": {255, 255, 255, 255},
"whitesmoke": {245, 245, 245, 255},
"yellow": {255, 255, 0, 255},
"yellowgreen": {154, 205, 50, 255},
}

View File

@@ -0,0 +1,186 @@
package sprite
import (
"bufio"
"fmt"
"image/color"
"io"
"os"
"path/filepath"
"strings"
)
// MaxSize is the maximum width/height of a single sprite in pixels.
const MaxSize = 256
// Sprite is a parsed .sprite file: a palette of single-rune keys and a
// rectangular grid of those keys.
type Sprite struct {
Name string
W, H int
Palette map[rune]color.NRGBA
Keys []rune // palette keys in file order
Rows [][]rune // H rows of exactly W palette keys
}
// At returns the color of pixel (x, y). Out-of-range pixels are transparent.
func (s *Sprite) At(x, y int) color.NRGBA {
if x < 0 || y < 0 || x >= s.W || y >= s.H {
return Transparent
}
return s.Palette[s.Rows[y][x]]
}
// Bounds returns the sprite size in pixels.
func (s *Sprite) Bounds() (w, h int) { return s.W, s.H }
// ParseFile reads a .sprite file from disk. The sprite name defaults to the
// file name without extension when the file has no "sprite:" line.
func ParseFile(path string) (*Sprite, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
s, err := Parse(f)
if err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
if s.Name == "" {
s.Name = strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
}
return s, nil
}
// Parse reads the .sprite text format:
//
// # comment lines start with '#'
// sprite: coin (optional name)
// palette:
// . = none (key '.' is transparent by default)
// k = #000000 text after the color is ignored
// y = gold
// grid:
// ..kk..
// .kyyk.
//
// Palette keys are exactly one character and may not be '#', '=', ':' or
// whitespace. Every grid row must be the same width; max size is 256x256.
func Parse(r io.Reader) (*Sprite, error) {
s := &Sprite{Palette: map[rune]color.NRGBA{}}
const (
secNone = iota
secPalette
secGrid
)
section := secNone
sc := bufio.NewScanner(r)
sc.Buffer(make([]byte, 0, 64*1024), 1024*1024)
lineNo := 0
for sc.Scan() {
lineNo++
line := strings.TrimSpace(sc.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue // comment or blank ('#' is not a legal palette key)
}
lower := strings.ToLower(line)
switch {
case lower == "palette:":
section = secPalette
continue
case lower == "grid:":
section = secGrid
continue
case strings.HasPrefix(lower, "sprite:"):
s.Name = strings.TrimSpace(line[len("sprite:"):])
continue
}
switch section {
case secPalette:
if err := s.parsePaletteLine(line, lineNo); err != nil {
return nil, err
}
case secGrid:
row := []rune(line)
s.Rows = append(s.Rows, row)
default:
return nil, fmt.Errorf("line %d: unexpected %q before a 'palette:' or 'grid:' section", lineNo, line)
}
}
if err := sc.Err(); err != nil {
return nil, err
}
return s, s.validate()
}
func (s *Sprite) parsePaletteLine(line string, lineNo int) error {
eq := strings.Index(line, "=")
if eq < 0 {
return fmt.Errorf("line %d: palette entry %q must look like '<key> = <color>'", lineNo, line)
}
keyPart := []rune(strings.TrimSpace(line[:eq]))
if len(keyPart) != 1 {
return fmt.Errorf("line %d: palette key %q must be exactly one character", lineNo, strings.TrimSpace(line[:eq]))
}
key := keyPart[0]
if key == '#' || key == '=' || key == ':' {
return fmt.Errorf("line %d: %q is not allowed as a palette key", lineNo, key)
}
if _, dup := s.Palette[key]; dup {
return fmt.Errorf("line %d: palette key %q defined twice", lineNo, key)
}
val := strings.TrimSpace(line[eq+1:])
// The color is the first token; anything after it is a free-text comment.
token := val
if strings.HasPrefix(strings.ToLower(val), "rgb") {
if close := strings.Index(val, ")"); close >= 0 {
token = val[:close+1]
}
} else if i := strings.IndexAny(val, " \t"); i >= 0 {
token = val[:i]
}
c, err := ParseColor(token)
if err != nil {
return fmt.Errorf("line %d: %w", lineNo, err)
}
s.Palette[key] = c
s.Keys = append(s.Keys, key)
return nil
}
func (s *Sprite) validate() error {
if len(s.Rows) == 0 {
return fmt.Errorf("no 'grid:' section with at least one row found")
}
// '.' is transparent unless the file overrides it.
if _, ok := s.Palette['.']; !ok {
s.Palette['.'] = Transparent
}
s.H = len(s.Rows)
s.W = len(s.Rows[0])
if s.W > MaxSize || s.H > MaxSize {
return fmt.Errorf("sprite is %dx%d pixels; the maximum is %dx%d", s.W, s.H, MaxSize, MaxSize)
}
for y, row := range s.Rows {
if len(row) != s.W {
return fmt.Errorf("grid row %d is %d pixels wide, expected %d (all rows must match row 1)", y+1, len(row), s.W)
}
for x, key := range row {
if _, ok := s.Palette[key]; !ok {
return fmt.Errorf("grid row %d, column %d: %q is not defined in the palette", y+1, x+1, string(key))
}
}
}
return nil
}
// UsageCount returns how many grid pixels use each palette key.
func (s *Sprite) UsageCount() map[rune]int {
n := map[rune]int{}
for _, row := range s.Rows {
for _, k := range row {
n[k]++
}
}
return n
}

View File

@@ -0,0 +1,75 @@
package sprite
import (
"fmt"
"image"
"image/color"
"image/jpeg"
"image/png"
"io"
)
// PixelGrid is anything that can be rendered pixel by pixel: a single
// Sprite or a composed Sheet.
type PixelGrid interface {
Bounds() (w, h int)
At(x, y int) color.NRGBA
}
// Image renders a PixelGrid to an NRGBA image, scaled up by the integer
// factor scale (nearest neighbour, keeps pixels crisp).
func Image(g PixelGrid, scale int) *image.NRGBA {
if scale < 1 {
scale = 1
}
w, h := g.Bounds()
img := image.NewNRGBA(image.Rect(0, 0, w*scale, h*scale))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
c := g.At(x, y)
for dy := 0; dy < scale; dy++ {
for dx := 0; dx < scale; dx++ {
img.SetNRGBA(x*scale+dx, y*scale+dy, c)
}
}
}
}
return img
}
// WritePNG encodes g as PNG with transparency preserved.
func WritePNG(w io.Writer, g PixelGrid, scale int) error {
return png.Encode(w, Image(g, scale))
}
// WriteJPG encodes g as JPEG. JPEG has no alpha channel, so transparent
// pixels are composited over bg first.
func WriteJPG(w io.Writer, g PixelGrid, scale int, bg color.NRGBA, quality int) error {
if quality < 1 || quality > 100 {
return fmt.Errorf("jpg quality %d out of range 1-100", quality)
}
src := Image(g, scale)
b := src.Bounds()
flat := image.NewRGBA(b)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
flat.Set(x, y, blendOver(src.NRGBAAt(x, y), bg))
}
}
return jpeg.Encode(w, flat, &jpeg.Options{Quality: quality})
}
// blendOver composites src over an opaque background color.
func blendOver(src, bg color.NRGBA) color.NRGBA {
if src.A == 255 {
return src
}
a := uint32(src.A)
inv := 255 - a
return color.NRGBA{
R: uint8((uint32(src.R)*a + uint32(bg.R)*inv) / 255),
G: uint8((uint32(src.G)*a + uint32(bg.G)*inv) / 255),
B: uint8((uint32(src.B)*a + uint32(bg.B)*inv) / 255),
A: 255,
}
}

View File

@@ -0,0 +1,58 @@
package sprite
import (
"fmt"
"image/color"
)
// Sheet lays out several equally sized sprites in a row-major grid:
// index 0 is top-left, then left-to-right, then next row. A 1D strip is
// simply a sheet with one row (or one column).
type Sheet struct {
Sprites []*Sprite
Cols, Rows int
CellW, CellH int
}
// NewSheet composes sprites into a sheet with the given number of columns.
// cols <= 0 puts everything in a single row. Every sprite must have the
// same dimensions so the sheet can be indexed cell by cell.
func NewSheet(sprites []*Sprite, cols int) (*Sheet, error) {
if len(sprites) == 0 {
return nil, fmt.Errorf("a sheet needs at least one sprite")
}
w, h := sprites[0].Bounds()
for _, sp := range sprites[1:] {
sw, sh := sp.Bounds()
if sw != w || sh != h {
return nil, fmt.Errorf("sprite %q is %dx%d but %q is %dx%d — all sprites in a sheet must be the same size",
sp.Name, sw, sh, sprites[0].Name, w, h)
}
}
if cols <= 0 || cols > len(sprites) {
cols = len(sprites)
}
rows := (len(sprites) + cols - 1) / cols
return &Sheet{Sprites: sprites, Cols: cols, Rows: rows, CellW: w, CellH: h}, nil
}
// Bounds returns the total sheet size in pixels.
func (sh *Sheet) Bounds() (w, h int) { return sh.Cols * sh.CellW, sh.Rows * sh.CellH }
// At returns the pixel at (x, y). Cells past the last sprite (when the
// sprite count doesn't fill the grid) are transparent.
func (sh *Sheet) At(x, y int) color.NRGBA {
col, row := x/sh.CellW, y/sh.CellH
idx := row*sh.Cols + col
if idx >= len(sh.Sprites) {
return Transparent
}
return sh.Sprites[idx].At(x%sh.CellW, y%sh.CellH)
}
// FileBase appends the layout to an output name so the file itself
// documents cell size and orientation: <base>_<cellW>x<cellH>_<cols>x<rows>
// e.g. walk_16x16_4x2 = 16x16 cells, 4 columns, 2 rows, read row by row.
func (sh *Sheet) FileBase(base string) string {
return fmt.Sprintf("%s_%dx%d_%dx%d", base, sh.CellW, sh.CellH, sh.Cols, sh.Rows)
}

View File

@@ -0,0 +1,207 @@
package sprite
import (
"bytes"
"encoding/xml"
"image/color"
"strings"
"testing"
)
const coin = `
# a tiny coin
sprite: coin
palette:
k = #000000
y = gold
Y = #FFF3A0 highlight
grid:
.kk.
kyYk
kyyk
.kk.
`
func parseOK(t *testing.T, src string) *Sprite {
t.Helper()
s, err := Parse(strings.NewReader(src))
if err != nil {
t.Fatalf("parse failed: %v", err)
}
return s
}
func TestParseBasics(t *testing.T) {
s := parseOK(t, coin)
if s.Name != "coin" {
t.Errorf("name = %q, want coin", s.Name)
}
if s.W != 4 || s.H != 4 {
t.Errorf("size = %dx%d, want 4x4", s.W, s.H)
}
if got := s.At(0, 0); got.A != 0 {
t.Errorf("(0,0) should be transparent via the implicit '.', got %v", got)
}
if got := s.At(1, 1); got != (color.NRGBA{255, 215, 0, 255}) {
t.Errorf("(1,1) = %v, want gold", got)
}
if got := s.At(2, 1); got != (color.NRGBA{255, 243, 160, 255}) {
t.Errorf("(2,1) = %v, want #FFF3A0", got)
}
}
func TestParseErrors(t *testing.T) {
cases := map[string]string{
"ragged rows": "palette:\n a = red\ngrid:\naa\naaa\n",
"unknown key": "palette:\n a = red\ngrid:\nab\naa\n",
"bad color": "palette:\n a = notacolor\ngrid:\na\n",
"no grid": "palette:\n a = red\n",
"dup key": "palette:\n a = red\n a = blue\ngrid:\na\n",
"multirune key": "palette:\n ab = red\ngrid:\na\n",
"colon key": "palette:\n : = red\ngrid:\n.\n",
}
for name, src := range cases {
if _, err := Parse(strings.NewReader(src)); err == nil {
t.Errorf("%s: expected an error", name)
}
}
}
func TestMaxSize(t *testing.T) {
row := strings.Repeat("a", 257)
src := "palette:\n a = red\ngrid:\n" + row + "\n"
if _, err := Parse(strings.NewReader(src)); err == nil {
t.Error("257 px wide sprite should be rejected")
}
ok := "palette:\n a = red\ngrid:\n" + strings.Repeat(strings.Repeat("a", 256)+"\n", 256)
s := parseOK(t, ok)
if s.W != 256 || s.H != 256 {
t.Errorf("size = %dx%d, want 256x256", s.W, s.H)
}
}
func TestParseColorForms(t *testing.T) {
cases := map[string]color.NRGBA{
"none": {0, 0, 0, 0},
"-": {0, 0, 0, 0},
"#F00": {255, 0, 0, 255},
"#F00A": {255, 0, 0, 170},
"#00FF00": {0, 255, 0, 255},
"#00FF0080": {0, 255, 0, 128},
"rgb(1,2,3)": {1, 2, 3, 255},
"rgba(1,2,3,64)": {1, 2, 3, 64},
"rgba(1, 2, 3, .5)": {1, 2, 3, 128},
"RebeccaPurple": {102, 51, 153, 255},
}
for in, want := range cases {
got, err := ParseColor(in)
if err != nil {
t.Errorf("ParseColor(%q): %v", in, err)
continue
}
if got != want {
t.Errorf("ParseColor(%q) = %v, want %v", in, got, want)
}
}
for _, bad := range []string{"", "#12345", "rgb(300,0,0)", "purpleish"} {
if _, err := ParseColor(bad); err == nil {
t.Errorf("ParseColor(%q): expected error", bad)
}
}
}
func TestImageAndScale(t *testing.T) {
s := parseOK(t, coin)
img := Image(s, 3)
if b := img.Bounds(); b.Dx() != 12 || b.Dy() != 12 {
t.Fatalf("scaled bounds = %v, want 12x12", b)
}
// pixel (1,1) is gold -> block at (3..5, 3..5)
if got := img.NRGBAAt(4, 4); got != (color.NRGBA{255, 215, 0, 255}) {
t.Errorf("scaled gold pixel = %v", got)
}
if got := img.NRGBAAt(0, 0); got.A != 0 {
t.Errorf("corner should stay transparent, got %v", got)
}
}
func TestSVGOutput(t *testing.T) {
s := parseOK(t, coin)
var buf bytes.Buffer
if err := WriteSVG(&buf, s, 10); err != nil {
t.Fatal(err)
}
out := buf.String()
if !strings.Contains(out, `viewBox="0 0 4 4"`) || !strings.Contains(out, `width="40"`) {
t.Errorf("svg header wrong:\n%s", out)
}
// row 2 (y=1) has runs k, yY?, no: k y Y k -> y and Y differ, so no merge.
// row 3 (y=2) k yy k -> the two golds merge into one rect of width 2.
if !strings.Contains(out, `<rect x="1" y="2" width="2" height="1" fill="#ffd700"/>`) {
t.Errorf("expected RLE-merged gold rect:\n%s", out)
}
var doc struct{ XMLName xml.Name }
if err := xml.Unmarshal(buf.Bytes(), &doc); err != nil {
t.Errorf("svg is not valid XML: %v", err)
}
}
func TestSheetLayoutAndNaming(t *testing.T) {
a := parseOK(t, coin)
b := parseOK(t, coin)
c := parseOK(t, coin)
sh, err := NewSheet([]*Sprite{a, b, c}, 2)
if err != nil {
t.Fatal(err)
}
if sh.Cols != 2 || sh.Rows != 2 {
t.Errorf("layout = %dx%d, want 2x2", sh.Cols, sh.Rows)
}
if w, h := sh.Bounds(); w != 8 || h != 8 {
t.Errorf("bounds = %dx%d, want 8x8", w, h)
}
if got := sh.FileBase("walk"); got != "walk_4x4_2x2" {
t.Errorf("FileBase = %q, want walk_4x4_2x2", got)
}
// cell (1,1) in frame 2 (top-right) is gold
if got := sh.At(5, 1); got != (color.NRGBA{255, 215, 0, 255}) {
t.Errorf("sheet pixel in frame 2 = %v, want gold", got)
}
// 4th cell (bottom-right) has no sprite -> transparent
if got := sh.At(7, 7); got.A != 0 {
t.Errorf("empty cell should be transparent, got %v", got)
}
}
func TestSheetSizeMismatch(t *testing.T) {
a := parseOK(t, coin)
b := parseOK(t, "palette:\n a = red\ngrid:\naa\n")
if _, err := NewSheet([]*Sprite{a, b}, 0); err == nil {
t.Error("mismatched sprite sizes should be rejected")
}
}
func TestSheetDefaultSingleRow(t *testing.T) {
a := parseOK(t, coin)
sh, err := NewSheet([]*Sprite{a, a, a}, 0)
if err != nil {
t.Fatal(err)
}
if sh.Cols != 3 || sh.Rows != 1 {
t.Errorf("default layout = %dx%d, want 3x1", sh.Cols, sh.Rows)
}
}
func TestJPGComposite(t *testing.T) {
s := parseOK(t, coin)
var buf bytes.Buffer
if err := WriteJPG(&buf, s, 1, color.NRGBA{255, 255, 255, 255}, 90); err != nil {
t.Fatal(err)
}
if buf.Len() == 0 {
t.Error("empty jpg output")
}
if err := WriteJPG(&buf, s, 1, Transparent, 150); err == nil {
t.Error("quality 150 should be rejected")
}
}

View File

@@ -0,0 +1,46 @@
package sprite
import (
"fmt"
"io"
)
// WriteSVG encodes g as an SVG where each horizontal run of same-colored
// pixels becomes one <rect>. shape-rendering="crispEdges" keeps the pixel
// look at any zoom. scale only affects the document width/height; the
// viewBox stays in pixel units.
func WriteSVG(w io.Writer, g PixelGrid, scale int) error {
if scale < 1 {
scale = 1
}
gw, gh := g.Bounds()
_, err := fmt.Fprintf(w,
`<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" viewBox="0 0 %d %d" shape-rendering="crispEdges">`+"\n",
gw*scale, gh*scale, gw, gh)
if err != nil {
return err
}
for y := 0; y < gh; y++ {
for x := 0; x < gw; {
c := g.At(x, y)
run := 1
for x+run < gw && g.At(x+run, y) == c {
run++
}
if c.A > 0 {
opacity := ""
if c.A < 255 {
opacity = fmt.Sprintf(` fill-opacity="%.3f"`, float64(c.A)/255)
}
_, err = fmt.Fprintf(w, `<rect x="%d" y="%d" width="%d" height="1" fill="#%02x%02x%02x"%s/>`+"\n",
x, y, run, c.R, c.G, c.B, opacity)
if err != nil {
return err
}
}
x += run
}
}
_, err = io.WriteString(w, "</svg>\n")
return err
}