Skip to content

Instantly share code, notes, and snippets.

@juliandescottes
Last active January 13, 2019 21:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juliandescottes/67627f1e2ea093cd9fd25deebd7a6135 to your computer and use it in GitHub Desktop.
Save juliandescottes/67627f1e2ea093cd9fd25deebd7a6135 to your computer and use it in GitHub Desktop.
Piskel Feature request: Add unit8_t version to "Export as C file" for single color images #606
var export_to_unit8_t = function () {
var HTML_NEW_LINE = '
';
var width = pskl.app.piskelController.getWidth();
var height = pskl.app.piskelController.getHeight();
var frameCount = pskl.app.piskelController.getFrameCount();
if (frameCount > 1) {
console.error('This export only supports one frame');
}
var frame = pskl.utils.LayerUtils.mergeFrameAt(pskl.app.piskelController.getLayers(), 0);
var str = 'uint8_t frames[' + height + '] = {' + HTML_NEW_LINE;
var lines = [];
frame.forEachPixel(function (color, col, row) {
if (!lines[row]) {
lines[row] = [];
}
var line = lines[row];
var isTransparent = color === pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR);
line[col] = isTransparent ? '0' : '1';
});
str += lines.map(function (line) {
return ' B' + line.join('');
}).join(',' + HTML_NEW_LINE);
str += HTML_NEW_LINE + '};' + HTML_NEW_LINE;
window.open('data:text/html,<textarea style="width:100%;height:100%;">' + str + '</textarea>');
};
export_to_unit8_t();
@juliandescottes
Copy link
Author

juliandescottes commented Jan 13, 2017

As a bookmarklet:

javascript:(function(){var export_to_unit8_t=function(){var HTML_NEW_LINE=' ';var width=pskl.app.piskelController.getWidth();var height=pskl.app.piskelController.getHeight();var frameCount=pskl.app.piskelController.getFrameCount();if(frameCount>1){console.error('This export only supports one frame');}var frame=pskl.utils.LayerUtils.mergeFrameAt(pskl.app.piskelController.getLayers(),0);var str='uint8_t frames['+height+'] = {'+HTML_NEW_LINE;var lines=[];frame.forEachPixel(function(color,col,row){if(!lines[row]){lines[row]=[];}var line=lines[row];var isTransparent=color===pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR);line[col]=isTransparent?'0':'1';});str+=lines.map(function(line){return ' B'+line.join('');}).join(','+HTML_NEW_LINE);str+=HTML_NEW_LINE+'};'+HTML_NEW_LINE;window.open('data:text/html,<textarea style="width:100%;height:100%;">'+str+'</textarea>');};export_to_unit8_t();})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment