Got it! You want to:
- Parse lines from a text file of the form
[ca/:2,1,1,1,1,3,1,3,4,2,1]
- Extract fields:
- A1 = ID (body/head variant index)
- A2-A5 = skin color RGBA
- A6 = body shape variant index
- A7-A10 = eye color RGBA
- A11 = eye shape variant index
- Then load the appropriate PNGs (line + fill) for each part according to these IDs.
- Tint the fills according to the RGBA colors.
- Compose the layers and save the output.
Here's a Go example that:
- Reads the text file line-by-line.
- Parses the
[ca/:...] lines extracting integers.
- For each line, generates a composed paperdoll sprite accordingly.
package main
import (
"bufio"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
// LoadPNG loads an image from file
func LoadPNG(path string) (image.Image, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
img, err := png.Decode(f)
if err != nil {
return nil, err
}
return img, nil
}
// TintFillLayer applies tint to fill area
func TintFillLayer(src image.Image, tint color.RGBA) *image.RGBA {
bounds := src.Bounds()
dst := image.NewRGBA(bounds)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
original := color.RGBAModel.Convert(src.At(x, y)).(color.RGBA)
if original.A == 0 {
dst.SetRGBA(x, y, color.RGBA{0, 0, 0, 0})
} else {
r := uint8(uint16(original.R) * uint16(tint.R) / 255)
g := uint8(uint16(original.G) * uint16(tint.G) / 255)
b := uint8(uint16(original.B) * uint16(tint.B) / 255)
dst.SetRGBA(x, y, color.RGBA{r, g, b, original.A})
}
}
}
return dst
}
// ComposeLayers draws layers in order: bottom to top
func ComposeLayers(layers []image.Image) *image.RGBA {
if len(layers) == 0 {
return nil
}
bounds := layers[0].Bounds()
canvas := image.NewRGBA(bounds)
draw.Draw(canvas, bounds, image.Transparent, image.Point{}, draw.Src)
for _, layer := range layers {
draw.Draw(canvas, bounds, layer, bounds.Min, draw.Over)
}
return canvas
}
// Parsed data struct from one line (assuming format described)
type PaperDollData struct {
ID int
SkinColor color.RGBA
BodyShape int
EyeColor color.RGBA
EyeShape int
}
// Parse a single line of format:
// [ca/:2,1,1,1,1,3,1,3,4,2,1]
// Returns parsed PaperDollData or error
func ParseLine(line string) (PaperDollData, error) {
// Regex to extract content inside [ca/: ... ]
re := regexp.MustCompile(`\[ca/:([^\]]+)\]`)
matches := re.FindStringSubmatch(line)
if matches == nil || len(matches) < 2 {
return PaperDollData{}, errors.New("line does not match expected format")
}
numsStr := matches[1] // e.g. "2,1,1,1,1,3,1,3,4,2,1"
fields := strings.Split(numsStr, ",") // should be 11 numbers
if len(fields) < 11 {
return PaperDollData{}, fmt.Errorf("expected 11 fields, got %d", len(fields))
}
parseIntField := func(i int) (int, error) {
return strconv.Atoi(strings.TrimSpace(fields[i]))
}
parseColorField := func(start int) (color.RGBA, error) {
r, err := parseIntField(start)
if err != nil { return color.RGBA{}, err }
g, err := parseIntField(start+1)
if err != nil { return color.RGBA{}, err }
b, err := parseIntField(start+2)
if err != nil { return color.RGBA{}, err }
a, err := parseIntField(start+3)
if err != nil { return color.RGBA{}, err }
return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}, nil
}
id, err := parseIntField(0)
if err != nil { return PaperDollData{}, err }
skinColor, err := parseColorField(1)
if err != nil { return PaperDollData{}, err }
bodyShape, err := parseIntField(5)
if err != nil { return PaperDollData{}, err }
eyeColor, err := parseColorField(6)
if err != nil { return PaperDollData{}, err }
eyeShape, err := parseIntField(10)
if err != nil { return PaperDollData{}, err }
return PaperDollData{
ID: id,
SkinColor: skinColor,
BodyShape: bodyShape,
EyeColor: eyeColor,
EyeShape: eyeShape,
}, nil
}
// Helper to build variant filename with a 2-digit zero padded int (e.g. "03")
func variantFilename(part string, variant int, layer string) string {
// layer can be "line" or "fill"
return filepath.Join("assets", part, fmt.Sprintf("%s_%02d_%s.png", part, variant, layer))
}
func processPaperDoll(data PaperDollData, outFileName string) error {
// Body parts: we'll load head, body, eyes
// Variants 1-based indexing assumed in input (map to your files)
// Hair not included in this example; extend similarly.
// For example parts: "head", "body", "eye"
parts := []string{"head", "body", "eye"}
// Map parts to variant number and tint color
variants := map[string]int{
"head": data.ID, // Use ID as head variant
"body": data.BodyShape,
"eye": data.EyeShape,
}
colors := map[string]color.RGBA{
"head": data.SkinColor,
"body": color.RGBA{100, 100, 100, 255}, // Fixed color, or could extend input data
"eye": data.EyeColor,
}
var composedLayers []image.Image
// Draw order: body, head, eyes
drawOrder := []string{"body", "head", "eye"}
for _, part := range drawOrder {
variant := variants[part]
if variant < 1 || variant > 10 {
return fmt.Errorf("variant %d for part %s out of range [1,10]", variant, part)
}
// Load fill
fillPath := variantFilename(part, variant, "fill")
fillImg, err := LoadPNG(fillPath)
if err != nil {
return fmt.Errorf("failed to load fill for %s:%d: %w", part, variant, err)
}
// Tint fill
tintedFill := TintFillLayer(fillImg, colors[part])
// Load line
linePath := variantFilename(part, variant, "line")
lineImg, err := LoadPNG(linePath)
if err != nil {
return fmt.Errorf("failed to load line for %s:%d: %w", part, variant, err)
}
// Append fill, then line (so line is on top)
composedLayers = append(composedLayers, tintedFill, lineImg)
}
// Compose full image
finalImg := ComposeLayers(composedLayers)
// Save output
outFile, err := os.Create(outFileName)
if err != nil {
return err
}
defer outFile.Close()
return png.Encode(outFile, finalImg)
}
func main() {
inputFile := "input.txt" // Your input text file with lines like [ca/:2,1,1,1,1,3,1,3,4,2,1]
f, err := os.Open(inputFile)
if err != nil {
panic(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
counter := 1
for scanner.Scan() {
line := scanner.Text()
if !strings.Contains(line, "[ca/:") {
continue // skip non-data lines
}
data, err := ParseLine(line)
if err != nil {
fmt.Printf("Failed to parse line: %v\n", err)
continue
}
outName := fmt.Sprintf("output_sprite_%03d.png", counter)
err = processPaperDoll(data, outName)
if err != nil {
fmt.Printf("Failed to process paperdoll for line %d: %v\n", counter, err)
continue
}
fmt.Printf("Generated %s\n", outName)
counter++
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading input file: %v\n", err)
}
}
What you need on disk:
Your folders organized as:
assets/
head/
head_01_fill.png
head_01_line.png
head_02_fill.png
...
body/
body_01_fill.png
body_01_line.png
...
eye/
eye_01_fill.png
eye_01_line.png
...
with variant numbers from 01 to 10 (2 digit zero-padded).
Explanation:
- The program reads each line with your
[ca/:...] format.
- It extracts relevant integers + RGBA info.
- It loads the corresponding fill and line images per part variant.
- It tints the fill images by the color in the input.
- It composes fills below line art.
- It saves an output PNG per line.
Adjust the hardcoded "body" color or add those colors to your input as needed.
If you want me to extend this with hair parts, or body color support, just ask!