pal

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package pal implements reading and writing of Total Annihilation .PAL palette files.

A TA palette is a fixed 1024-byte blob: 256 entries of 4 bytes each, laid out as R, G, B, A in little-endian order. The alpha byte is unused by the game (always 0 in Cavedog's files) and color index 0 acts as transparent.

The same on-disk layout is also reused for the .ALP shadow-alpha, .LHT light and .SHD shadow lookup tables — they are 1024-byte index→index mappings, not RGB palettes, so this package treats them as raw byte tables when asked.

Index

Constants

View Source
const EntryCount = 256

EntryCount is the number of color entries in a TA palette (always 256).

View Source
const FileSize = EntryCount * 4

FileSize is the on-disk size in bytes of a TA palette file (256 × 4).

Variables

This section is empty.

Functions

func LoadLookupFromFile

func LoadLookupFromFile(path string) ([]byte, error)

LoadLookupFromFile is the file-path equivalent of LoadLookupFromReader.

func LoadLookupFromReader

func LoadLookupFromReader(r io.Reader) ([]byte, error)

LoadLookupFromReader parses a 1024-byte color-index lookup table (.ALP, .LHT or .SHD). These files share the .PAL size but each byte is an index into the main palette rather than a color channel.

The returned slice has length FileSize and is the raw file bytes — readers that need the table as a 256×4 grid (the canonical ALP/LHT layout) can index directly.

func RenderLookupSwatch

func RenderLookupSwatch(table []byte, palette *Palette, cellSize int) (*image.RGBA, error)

RenderLookupSwatch renders a 1024-byte color-index lookup table as a 256×4 grid of cellSize×cellSize squares using the given palette for the index→RGB mapping. Returns an RGBA image of size 256*cellSize × 4*cellSize. Suitable for .ALP/.LHT/.SHD which are all 256×4 index tables.

func WritePNG

func WritePNG(w io.Writer, img image.Image) error

WritePNG encodes an image to PNG.

Types

type Palette

type Palette struct {
	Colors [EntryCount]color.RGBA
	Raw    []byte
}

Palette is a parsed TA .PAL file. The Raw slice keeps the original bytes so callers that care about the unused alpha byte (or want to round-trip the file byte-for-byte) can recover it.

func LoadFromBytes

func LoadFromBytes(data []byte) (*Palette, error)

LoadFromBytes parses a .PAL file from a byte slice.

func LoadFromFile

func LoadFromFile(path string) (*Palette, error)

LoadFromFile parses a .PAL file at path.

func LoadFromReader

func LoadFromReader(r io.Reader) (*Palette, error)

LoadFromReader parses a .PAL file from r.

Color index 0 is reported with alpha=0 to match how every other kbot loader treats the TA palette (the engine uses index 0 as the transparent sentinel). All other entries are returned fully opaque.

func (*Palette) ColorModel

func (p *Palette) ColorModel() color.Palette

ColorModel returns the palette as a Go image/color Palette.

func (*Palette) Equals

func (p *Palette) Equals(other *Palette) bool

Equals reports whether two palettes have the same RGB values (alpha is ignored because color index 0 always carries the transparent override).

func (*Palette) Histogram

func (p *Palette) Histogram() (unique int, duplicates int)

Histogram returns a summary of how many distinct RGB triples the palette contains. Index 0 is excluded because it is the transparent sentinel. The result is useful when comparing palettes since Cavedog's defaults reserve certain ranges for team colors and shadows.

func (*Palette) IsLikelyTAPalette

func (p *Palette) IsLikelyTAPalette() bool

IsLikelyTAPalette returns true if the file's alpha bytes are all zero, which is the case for Cavedog-shipped TA palettes (and a common sanity check).

func (*Palette) RenderSwatch

func (p *Palette) RenderSwatch(cellSize int) *image.RGBA

RenderSwatch renders the 256 entries as a 16×16 grid of cellSize×cellSize squares. cellSize<=0 defaults to 16 (256×256 image).

func (*Palette) Write

func (p *Palette) Write(w io.Writer) error

Write encodes p back to a .PAL file. When Raw is set (e.g. for a palette loaded with LoadFromReader) the original bytes are emitted verbatim so any unused alpha bytes are preserved. Otherwise the alpha byte for every entry is zero — matching Cavedog's files.

func (*Palette) WriteGPL

func (p *Palette) WriteGPL(w io.Writer, name string) error

WriteGPL encodes the palette in the GIMP Palette (.gpl) text format.

func (*Palette) WriteJASC

func (p *Palette) WriteJASC(w io.Writer) error

WriteJASC encodes the palette in the JASC-PAL plain-text format, the de facto exchange format used by Paint Shop Pro, GIMP and most palette editors. Color count is always 256.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL