Skip to content

Commit b927296

Browse files
committed
Add Random Palette example
1 parent b08c654 commit b927296

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Random Palette.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
----------------------------------------------------------------------
2+
-- Randomize colors in the palette.
3+
----------------------------------------------------------------------
4+
5+
local spr = app.activeSprite
6+
if not spr then
7+
return app.alert("There is no active sprite")
8+
end
9+
10+
-- Each Palette:setColor() call will be grouped in one undoable
11+
-- transaction. In this way the Edit > Undo History will contain only
12+
-- one item that can be undone.
13+
app.transaction(
14+
function()
15+
math.randomseed(os.time())
16+
local pal = spr.palettes[1]
17+
for i = 0,#pal-1 do
18+
-- Here we change each color of the palette with random RGB
19+
-- values from 0-255 for each Red, Green, Blue component.
20+
pal:setColor(i, Color{ r=math.random(256)-1,
21+
g=math.random(256)-1,
22+
b=math.random(256)-1 })
23+
end
24+
end)
25+
26+
-- Here we redraw the screen to show the new palette, in a future this
27+
-- shouldn't be necessary, but just in case...
28+
app.refresh()

0 commit comments

Comments
 (0)