Advertisement
kylemsguy

Annotated draw_encode from TCaS

Jan 1st, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function draw_encode() {
  2.     var output = [];
  3.     var grid = DRAW.grid;
  4.     var color;
  5.     var encoding = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-';
  6.  
  7.     // convert colour int values to a string according to the above encoding array (e.g. 000 -> aaa)
  8.     for (var y = 0; y < DRAW.height; ++y) {
  9.         for (var x = 0; x < DRAW.width; ++x) {
  10.             color = grid[x][y];
  11.             output.push(encoding.charAt(color[0]) + encoding.charAt(color[1]) + encoding.charAt(color[2]));
  12.         }
  13.     }
  14.    
  15.     // instantiate colours with the first colour in the output array
  16.     var colors = [output[0]];
  17.     var count = [1];
  18.     var length = 1;
  19.     for (var i = 1; i < output.length; ++i) {
  20.         // color (temporary value) is the next output string (3chars)
  21.         color = output[i];
  22.         // if this colour is the same as the previous colour...
  23.         if (color == colors[length - 1]) {
  24.             // increment its count
  25.             count[length - 1]++;
  26.         } else {
  27.             // add this colour to the color array
  28.             colors.push(color);
  29.             // add a new counter to the count array
  30.             count.push(1);
  31.             // increment the length (the length of the colors array)
  32.             ++length;
  33.         }
  34.     }
  35.    
  36.     // empty output array
  37.     output = [];
  38.     for (i = 0; i < length; ++i) {
  39.         // for each colour...
  40.         color = colors[i];
  41.         switch (count[i]) {
  42.             // if there's only 1-5 of them, push a symbol preceding the colour
  43.             case 1: output.push(color); break;
  44.             case 2: output.push('!' + color); break;
  45.             case 3: output.push('@' + color); break;
  46.             case 4: output.push('#' + color); break;
  47.             case 5: output.push('$' + color); break;
  48.             // otherwise...
  49.             default:
  50.                 // get the count and store in c
  51.                 var c = count[i];
  52.                 // if the count > 63, set c to 63
  53.                 if (c > 63) {
  54.                     c = 63;
  55.                     // decrement count by 63
  56.                     count[i] -= 63;
  57.                     // repeat this iteration
  58.                     --i;
  59.                 }
  60.  
  61.                 // push % and another character from encoding using c as the offset.
  62.                 output.push('%' + encoding[c] + color);
  63.  
  64.                 // what this essentially does is pushes % and another character from encoding.
  65.                 // The character chosen is [5,63] (depending on the value of c), and if c > 63,
  66.                 // it pushes %-aaa and reruns the iteration with count[i] decremented by 63.
  67.                 break;
  68.         }
  69.     }
  70.    
  71.     // concatenate the output array and return it
  72.     return output.join('');
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement