Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
let image = new Image();
image.src = 'base64/...';
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0);
var hexDigits = new Array
("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
function rgb2hex(rgb) {
return "#" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
}
function hex(x) {
return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}
tiles_per_line = (image.width / 5);
out = '';
emoji_out = '';
room_out = '';
layer_out = '';
chars = '';
function get_char(base_code) {
let char = String.fromCharCode(base_code).toLowerCase();
if(chars.indexOf(char) == -1) {
chars += char;
return char;
}
return get_char(base_code + 1);
}
for(let tile = 0; tile < (image.width * image.height / 25); tile++)
{
let base_x = (tile % tiles_per_line) * 5
let base_y = Math.floor(tile / tiles_per_line) * 5
let colors = [];
out += '\nGSMuralTile' + tile + '\n';
let tout = '';
for(let y = 0; y < 5; y++) {
for(let x = 0; x < 5; x++)
{
let pixel_color = canvas.getContext('2d').getImageData(base_x + x, base_y + y, 1, 1).data;
if(pixel_color[3] == 255) {
let hex_color = rgb2hex(pixel_color);
let index = colors.indexOf(hex_color);
if(index == -1)
{
index = colors.length
colors.push(hex_color);
}
tout += index;
} else {
tout += '.';
}
}
tout += '\n';
}
out += colors.join(' ') + '\n' + tout;
let char = get_char(1000 + tile);
emoji_out += char + ' = GSMuralTile' + tile + '\n';
room_out += (base_x == 0 ? '\n' : '') + char;
layer_out += 'GSMuralTile' + tile + ', ';
}
out += '\n\n' + emoji_out + '\n\n' + room_out + '\n\n' + layer_out;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment