Skip to content

Instantly share code, notes, and snippets.

@juliandescottes
Last active April 26, 2024 23:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliandescottes/980e3e644ee5aa6d2d07 to your computer and use it in GitHub Desktop.
Save juliandescottes/980e3e644ee5aa6d2d07 to your computer and use it in GitHub Desktop.
Fill current sprite with random colors
var rand = function (base) {
base = base || 1;
return (Math.random() * base) | 0;
};
var pad = function (str) {
if (str.length == 1) {
return "0" + str;
}
return str;
}
var randomColor = function () {
var r = rand(256);
var g = rand(256);
var b = rand(256);
return '#' + pad(r.toString(16)) + pad(g.toString(16)) + pad(b.toString(16));
};
var colorCount = 10;
var colors = [];
while (colors.length < colorCount) {
colors.push(randomColor());
}
var layers = pskl.app.piskelController.getPiskel().getLayers();
layers.forEach(function (l) {
l.getFrames().forEach(function (f) {
f.forEachPixel(function (color, col, row) {
var cIndex = rand(colorCount);
f.setPixel(col, row, colors[cIndex]);
})
});
});
$.publish(Events.PISKEL_RESET);
$.publish(Events.PISKEL_SAVE_STATE, {
type : pskl.service.HistoryService.SNAPSHOT
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment