Managing color palettes in Aseprite for indexed color artwork
Palettes are collections of colors used in your sprite. While all sprites have palettes, they are especially important for Indexed color mode, where pixels store palette indices instead of actual colors.
A palette is an ordered collection of colors (up to 256) that defines the available colors for your artwork. In Indexed mode, each pixel references a palette entry rather than storing color data directly.
Even RGB sprites have palettes - they’re used by color quantization algorithms and as a color reference for artists.
-- New sprites start with a 256-color black palettelocal sprite = Sprite(32, 32, ColorMode.INDEXED)local palette = sprite.palettes[1]print(#palette) -- 256-- All colors are black by defaultfor i = 0, #palette - 1 do print(palette:getColor(i)) -- Color(0, 0, 0)end
local sprite = Sprite(32, 32, ColorMode.INDEXED)local palette = sprite.palettes[1]-- Resize palettepalette:resize(8) -- Now 8 colorsprint(#palette) -- 8-- Set colors by index (0-based)palette:setColor(0, Color(255, 0, 0)) -- Index 0: Redpalette:setColor(1, Color(0, 255, 0)) -- Index 1: Green palette:setColor(2, Color(0, 0, 255)) -- Index 2: Bluepalette:setColor(3, Color(255, 255, 0)) -- Index 3: Yellow-- Get colorslocal red = palette:getColor(0)print(red.red, red.green, red.blue) -- 255, 0, 0-- Alternative syntaxlocal green = palette:entry(1)
local palette = Palette(4)-- Set color with alpha channellocal semiTransparent = Color{red=100, green=50, blue=10, alpha=128}palette:setColor(3, semiTransparent)print(palette:getColor(3).alpha) -- 128-- Check if palette uses alphaif palette:hasAlpha() then print("Palette has colors with alpha < 255")endif palette:hasSemiAlpha() then print("Palette has colors with 0 < alpha < 255")end
local sprite = Sprite(32, 32, ColorMode.INDEXED)-- Get palette for specific framelocal framePalette = sprite:palette(1) -- Palette at frame 1print(#framePalette)-- Palettes can change between frameslocal palette5 = sprite:palette(5) -- May be different
local sprite = Sprite(32, 32, ColorMode.INDEXED)-- Get all paletteslocal palettes = sprite:getPalettes()for i, pal in ipairs(palettes) do print("Palette", i, "has", #pal, "colors")end-- Reset to single palettesprite:resetPalettes() -- Removes all but first paletteprint(#sprite.palettes) -- 1-- Delete specific palettesprite:deletePalette(5) -- Remove palette starting at frame 5
-- Copy palettelocal original = Palette(8)local copy = Palette(original)-- Copy colors to another palettelocal source = Palette(16)local dest = Palette(16)source:copyColorsTo(dest)
-- Create grayscale palettelocal grayscale = Palette:createGrayscale()print(#grayscale) -- 256 colors from black to white-- Check if palette is completely blacklocal palette = Palette(16)if palette:isBlack() then print("All colors are black")end-- Make palette blackpalette:makeBlack()
local palette = Palette(16)palette:setColor(0, Color(255, 0, 0)) -- Red at index 0palette:setColor(1, Color(0, 255, 0)) -- Green at index 1palette:setColor(2, Color(0, 0, 255)) -- Blue at index 2-- Find exact matchlocal index = palette:findExactMatch(255, 0, 0, 255, -1)print(index) -- 0 (red is at index 0)-- Check if color existslocal exists = palette:findExactMatch(Color(0, 255, 0))print(exists) -- true (green exists)
local palette = Palette(4)-- Resize palettepalette:resize(8) -- Now has 8 slots-- Add individual colorpalette:addEntry(Color(255, 128, 0)) -- Adds at endprint(#palette) -- 9-- Add non-repeated colors from another palettelocal source = Palette(16)-- ... set colors in source ...palette:addNonRepeatedColors(source) -- Adds unique colors only
local sprite = Sprite(32, 32, ColorMode.INDEXED)local palette = sprite.palettes[1]local remap = { [0]=1, [1]=0, [2]=3, [3]=2 } -- Swap indices-- Apply remap to palettepalette:applyRemap(remap)-- This swaps the colors at the indices
-- Get default palettelocal defaultPal = app.defaultPaletteprint(#defaultPal) -- Usually DB32 (32 colors)-- Set new defaultlocal db16 = Palette{ fromResource = "DB16" }app.defaultPalette = db16-- New sprites will use DB16 palette