Export indices to grayscale image

I’m using a shader in my game to enable certain items to have their colour replaced (basically like using an indexed palette in Aseprite). What I do is use the red value to index into the replacement palette. If I make any changes to the base image I have to update the numbers in my game to match the red values of the modified image and hope I didn’t create any collisions due to colours having the same amount of red.

It would be really neat if I could export a PNG or whatever as grayscale with the RGB values set to the palette index instead of the actual colour, this way I’d be able to do palette swaps in my game much more easily.

Have you tried using different layers? (i.e., one for the color differences and the others for the drawing.) You can then export the layer with the differences as a separate file.
Anyway, I think I’m missing something.

1 Like

Yeah, I just want to export the index data from an indexed sprite as a PNG so I can use it as a texture in a shader

I’m assuming: You have a base sprite with layers + the layer named ‘Differences’ (with the red tone values):

In case your base sprite is on Indexed Color Mode:

  • Select the layer ‘Differences’ in the timeline
  • File > Export > Export As select PNG and Layer: Selected Layers
  • Go to Home tab then open the exported PNG
  • Convert the file to RGBA (Sprite > Color Mode > RGB Color)
  • Go to palette options button and select New Palette from Sprite and Create new palette…. 256
  • Convert the file to Indexed (Sprite > Color Mode > Indexed)
  • Save the file

In case your base sprite is on RGBA Color Mode:

  • Select the layer ‘Differences’ in the timeline
  • File > Export > Export As select PNG and Layer: Selected Layers
  • Go to Home tab then open the exported PNG
  • Convert the file to indexed (Sprite > Color Mode > indexed)
  • Save the file

Result on both cases: you have the ‘Differences’ image as an indexed PNG file.
I think this process is scriptable.

I hope I understood the original request correctly. If not, please send me a sample file showing a base drawing with the layer for color changes to support@aseprite.org.

Sorry, this is not at all what I’m asking. There is no “Differences”.
Say I have a sprite with 3 colours from an indexed palette, instead of exporting it with the actual colours, I want a texture where the colours would be RGB (0, 0, 0), (1, 1, 1), (2, 2, 2) where the numbers are the index from the palette rather than the actual RGB

Ohh, I see. You can try this script:
To use it you must meet the following conditions:

  • Opened sprite with Color Mode Indexed.
  • The cel with the image to be converted must be the active one.
local sprite = app.sprite

if sprite == nil or sprite.colorMode ~= ColorMode.INDEXED then
  return
end

local cel = app.cel

if cel == nil then
  return
end

local image = cel.image

if image == nil then
  return
end

local w = image.width
local h = image.height
local spriteRGB = Sprite(sprite.width, sprite.height, ColorMode.RGB)
local imageRGB = Image(w, h, ColorMode.RGB)
local pos = cel.position
for y = 0, h-1 do
  for x = 0, w-1 do
    local c = image:getPixel(x, y)
    imageRGB:drawPixel(x, y, app.pixelColor.rgba(c, c, c, 255))
  end
end

spriteRGB:newCel(spriteRGB.layers[1], 1, imageRGB, pos)
spriteRGB:saveAs("<your new filename>" .. "-hybrid.png")