Skip to main content
A Palette is a collection of colors used in indexed color mode sprites.

Constructor

Palette([numberOfColors])

numberOfColors
number
Number of colors in palette (default: 256).
local palette = Palette(16)
local defaultPalette = Palette()  -- 256 colors

Palette(otherPalette)

otherPalette
Palette
required
Palette to copy.
local copy = Palette(originalPalette)

Palette

fromFile
string
required
Path to palette file.
local palette = Palette{ fromFile="palette.gpl" }

Palette

fromResource
string
required
Built-in palette resource ID.
local palette = Palette{ fromResource="DB32" }

Properties

frame
Frame
Associated frame.
frameNumber
number
Frame number (1-based).

Methods

palette:resize(numberOfColors)

numberOfColors
number
required
New palette size.
Resizes the palette.
palette:resize(32)

palette:getColor(index)

index
number
required
Color index (0-based).
Returns the color at the specified index.
local color = palette:getColor(0)
print("First color:", color.red, color.green, color.blue)

palette:setColor(index, color)

index
number
required
Color index (0-based).
color
Color
required
Color to set.
Sets the color at the specified index.
palette:setColor(0, Color{ r=255, g=0, b=0 })
palette:setColor(1, Color{ r=0, g=255, b=0 })

palette:saveAs(filename)

filename
string
required
File path.
Saves the palette to a file.
palette:saveAs("my-palette.gpl")

#palette

Returns the number of colors in the palette.
local size = #palette
print("Palette has", size, "colors")

Examples

Creating a Custom Palette

local palette = Palette(8)

-- Set colors
palette:setColor(0, Color{ r=0, g=0, b=0 })         -- Black
palette:setColor(1, Color{ r=255, g=255, b=255 })   -- White
palette:setColor(2, Color{ r=255, g=0, b=0 })       -- Red
palette:setColor(3, Color{ r=0, g=255, b=0 })       -- Green
palette:setColor(4, Color{ r=0, g=0, b=255 })       -- Blue
palette:setColor(5, Color{ r=255, g=255, b=0 })     -- Yellow
palette:setColor(6, Color{ r=255, g=0, b=255 })     -- Magenta
palette:setColor(7, Color{ r=0, g=255, b=255 })     -- Cyan

local sprite = Sprite(32, 32, ColorMode.INDEXED)
sprite:setPalette(palette)

Generating a Gradient Palette

local palette = Palette(256)

for i=0,255 do
  local t = i / 255
  palette:setColor(i, Color{
    r = math.floor(t * 255),
    g = math.floor((1-t) * 255),
    b = 128
  })
end

palette:saveAs("gradient.gpl")

Working with Sprite Palettes

local sprite = app.sprite
local palette = sprite.palettes[1]

print("Palette size:", #palette)

-- Modify palette
for i=0,#palette-1 do
  local color = palette:getColor(i)
  -- Darken all colors
  color.hsvValue = color.hsvValue * 0.8
  palette:setColor(i, color)
end

Loading Built-in Palettes

-- Load built-in palette
local db32 = Palette{ fromResource="DB32" }
local sprite = Sprite(64, 64, ColorMode.INDEXED)
sprite:setPalette(db32)

Iterating Through Palette Colors

local palette = sprite.palettes[1]

for i=0,#palette-1 do
  local color = palette:getColor(i)
  print(string.format(
    "Color %d: RGB(%d, %d, %d)",
    i, color.red, color.green, color.blue
  ))
end