Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function draw_encode() {
- var output = [];
- var grid = DRAW.grid;
- var color;
- var encoding = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-';
- // convert colour int values to a string according to the above encoding array (e.g. 000 -> aaa)
- for (var y = 0; y < DRAW.height; ++y) {
- for (var x = 0; x < DRAW.width; ++x) {
- color = grid[x][y];
- output.push(encoding.charAt(color[0]) + encoding.charAt(color[1]) + encoding.charAt(color[2]));
- }
- }
- // instantiate colours with the first colour in the output array
- var colors = [output[0]];
- var count = [1];
- var length = 1;
- for (var i = 1; i < output.length; ++i) {
- // color (temporary value) is the next output string (3chars)
- color = output[i];
- // if this colour is the same as the previous colour...
- if (color == colors[length - 1]) {
- // increment its count
- count[length - 1]++;
- } else {
- // add this colour to the color array
- colors.push(color);
- // add a new counter to the count array
- count.push(1);
- // increment the length (the length of the colors array)
- ++length;
- }
- }
- // empty output array
- output = [];
- for (i = 0; i < length; ++i) {
- // for each colour...
- color = colors[i];
- switch (count[i]) {
- // if there's only 1-5 of them, push a symbol preceding the colour
- case 1: output.push(color); break;
- case 2: output.push('!' + color); break;
- case 3: output.push('@' + color); break;
- case 4: output.push('#' + color); break;
- case 5: output.push('$' + color); break;
- // otherwise...
- default:
- // get the count and store in c
- var c = count[i];
- // if the count > 63, set c to 63
- if (c > 63) {
- c = 63;
- // decrement count by 63
- count[i] -= 63;
- // repeat this iteration
- --i;
- }
- // push % and another character from encoding using c as the offset.
- output.push('%' + encoding[c] + color);
- // what this essentially does is pushes % and another character from encoding.
- // The character chosen is [5,63] (depending on the value of c), and if c > 63,
- // it pushes %-aaa and reruns the iteration with count[i] decremented by 63.
- break;
- }
- }
- // concatenate the output array and return it
- return output.join('');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement