Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vaimalaviya1233/8cfd5adf739a6b1f679cdba8040d84f6 to your computer and use it in GitHub Desktop.
Save vaimalaviya1233/8cfd5adf739a6b1f679cdba8040d84f6 to your computer and use it in GitHub Desktop.
-- Aseprite script for permuting the palette
-- permute_palette_colours.lua
-- by increpare - public domain
-- This script randomly permutes the color palette in the active indexed sprite.
-- Ensure there is an active sprite
local sprite = app.activeSprite
if not sprite then
app.alert("There is no active sprite.")
return
end
-- Only work in Indexed mode
if sprite.colorMode ~= ColorMode.INDEXED then
app.alert("The sprite must be in Indexed mode to permute its palette.")
return
end
local palette = sprite.palettes[1]
-- Read all palette colors into a table
local colors = {}
for i = 0, #palette - 1 do
table.insert(colors, palette:getColor(i))
end
-- Seed random number generator
math.randomseed(os.time())
-- Perform a Fisher-Yates shuffle on the colors table
for i = #colors, 2, -1 do
local j = math.random(i)
colors[i], colors[j] = colors[j], colors[i]
end
-- Apply the new shuffled palette colors in a transaction
app.transaction(function()
for i = 0, #palette - 1 do
palette:setColor(i, colors[i+1])
end
app.refresh()
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment