From e46a2b7c36d3365c0446d9618cb58a0beba180a0 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 20 Feb 2025 19:04:50 -0500 Subject: [PATCH] feat(ansi): sixel: add Palette field to Decoder --- ansi/sixel/decoder.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ansi/sixel/decoder.go b/ansi/sixel/decoder.go index b9494b3d..503d354b 100644 --- a/ansi/sixel/decoder.go +++ b/ansi/sixel/decoder.go @@ -11,7 +11,11 @@ import ( // Decoder is a Sixel image decoder. It reads Sixel image data from an // io.Reader and decodes it into an image.Image. -type Decoder struct{} +type Decoder struct { + // Palette is the color palette used to decode the Sixel image. If the + // palette is nil, the default palette is used from [DefaultPalette]. + Palette color.Palette +} // Decode will parse sixel image data into an image or return an error. Because // the sixel image format does not have a predictable size, the end of the sixel @@ -75,7 +79,10 @@ func (d *Decoder) Decode(r io.Reader) (image.Image, error) { } img := image.NewRGBA(bounds) - palette := DefaultPalette() + palette := d.Palette + if palette == nil { + palette = colorPalette[:] + } var currentX, currentBandY, currentPaletteIndex int // data buffer used to decode Sixel commands @@ -520,6 +527,7 @@ var colorPalette = [256]color.Color{ func DefaultPalette() color.Palette { // Undefined colors in sixel images use a set of default colors: 0-15 // are sixel-specific, 16-255 are the same as the xterm 256-color values - palette := append(color.Palette(nil), colorPalette[:]...) + palette := make([]color.Color, len(colorPalette)) + palette = append(palette, colorPalette[:]...) return palette[:] }