Skip to content

Commit b08c654

Browse files
committed
Add Random Pixels example
1 parent cfbc790 commit b08c654

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Random Pixels.lua

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
----------------------------------------------------------------------
2+
-- Randomize pixels of the active cel/image.
3+
--
4+
-- It works for all color modes (RGB/GRAY/INDEXED).
5+
----------------------------------------------------------------------
6+
7+
if app.apiVersion < 1 then
8+
return app.alert("This script requires Aseprite v1.2.10-beta3")
9+
end
10+
11+
local cel = app.activeCel
12+
if not cel then
13+
return app.alert("There is no active image")
14+
end
15+
16+
math.randomseed(os.time())
17+
18+
-- The best way to modify a cel image is to clone it (the new cloned
19+
-- image will be an independent image, without undo information).
20+
-- Then we can change the cel image generating only one undoable
21+
-- action.
22+
local img = cel.image:clone()
23+
24+
-- For RGB mode we change the RGB values keeping the Alpha value intact
25+
if img.colorMode == ColorMode.RGB then
26+
local rgba = app.pixelColor.rgba
27+
local rgbaA = app.pixelColor.rgbaA
28+
for it in img:pixels() do
29+
it(rgba(math.random(256)-1,
30+
math.random(256)-1,
31+
math.random(256)-1, rgbaA(it())))
32+
end
33+
-- For GRAY mode we change the grayscale value keeping the Alpha value intact
34+
elseif img.colorMode == ColorMode.GRAY then
35+
local graya = app.pixelColor.graya
36+
local grayaA = app.pixelColor.grayaA
37+
for it in img:pixels() do
38+
it(graya(math.random(256)-1, grayaA(it())))
39+
end
40+
-- For INDEXED mode we change the color index of non-transparent
41+
-- pixels with a ranom index that is not transparent too
42+
elseif img.colorMode == ColorMode.INDEXED then
43+
local n = #app.activeSprite.palettes[1]
44+
if n > 2 then
45+
local mask = img.spec.transparentColor
46+
for it in img:pixels() do
47+
-- For pixels that are not the mask
48+
if it() ~= mask then
49+
-- We try to put a new index value that is not the mask color
50+
-- (for transparent layers, because the background layer can
51+
-- have the mask color as a solid color)
52+
local c = math.random(n)-1
53+
if cel.layer.isTransparent then
54+
while c == mask do
55+
c = math.random(n)-1
56+
end
57+
end
58+
it(c) -- Here we set the pixel value
59+
end
60+
end
61+
end
62+
end
63+
64+
-- Here we change the cel image, this generates one undoable action
65+
cel.image = img
66+
67+
-- Here we redraw the screen to show the modified pixels, in a future
68+
-- this shouldn't be necessary, but just in case...
69+
app.refresh()

0 commit comments

Comments
 (0)