> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.y1S3nFYjA77/rev.23
 * 
 * authors: 
 *   GoToLoop

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/**
 * Grid Palette (v5.23)
 * by  des812 (2014/Apr)
 * mod GoToLoop
 *
 * forum.processing.org/two/discussion/4491/
 * is-it-possible-to-nest-multiple-versions-of-
 * a-2d-array-in-another-array
 *
 * studio.processingtogether.com/sp/pad/export/
 * ro.9tBDwfhlJ2R9D/latest
 */
 
static final int GRAYS = 9;
static final color[] grays = new color[GRAYS];
 
//static // Java only
{
  for (int i = GRAYS; i-- != 0;
    grays[i] = min(0400 - 040*i, 0xFF));
 
  println(grays);
  println();
}
 
static final int DIM = 100, FPS = 12, QUALITY = 0;
static final int PROWS = 1, PCOLS = 3;
static final int ROWS = 3, COLS = 5;
final color[][][] grids = new color[PROWS*PCOLS][ROWS][COLS];
 
int idx;
 
{ // Palette 0x1:
  fillPalette(grids[0], new int[] {
    0, 0, 7, 3, 0, // row 0
    3, 8, 1, 1, 6, // row 1
    4, 1, 0, 0, 0  // row 2
  }  
  );
}
 
{ // Palette 0x1:
  fillPalette(grids[1], new int[] {
    2, 2, 0, 3, 2, // row 0
    2, 5, 2, 2, 2, // row 1
    0, 5, 2, 0, 0  // row 2
  }
  );
}
 
{ // Palette 0x2:
  fillPalette(grids[2], new int[] {
    0, 0, 0, 0, 0, // row 0
    0, 3, 3, 3, 3, // row 1
    3, 3, 3, 8, 4  // row 2
  }
  );
}
 
static final void fillPalette(color[][] grid, int[] seq) {
  int i = 0;
  for (color[] row: grid)
    for (int c = 0; c != COLS;
      row[c++] = grays[seq[i++]]);
}
 
void setup() {
  size(COLS*DIM, ROWS*DIM, JAVA2D);
  frameRate(FPS);
  noLoop();
  smooth(QUALITY);
 
  noStroke();
  rectMode(CORNER);

  online = 1/2 == 1/2.;
}
 
void draw() {
  final color[][] grid = grids[idx];
 
  for (color[] row: grid) {
    for (int c = COLS; c-- != 0;) {
      fill(row[c]);
      rect(c*DIM, 0, DIM, DIM);
    }
 
    translate(0, DIM);
  }
 
  displayInfo();
}
 
void displayInfo() {
  final String msg = "Palette Index: #" + idx
    + "\t[" + (idx/PCOLS | 0) + ", " + idx%PCOLS + "]";
 
  println(msg);
  if (!online)  frame.setTitle(msg);
}
 
void mousePressed() {
  jumpPalette(mouseButton == LEFT? -1 : 1);  
  redraw();
}
 
void mouseWheel(MouseEvent e) {
  jumpPalette(e.getCount()*PCOLS);
  redraw();
}
 
void keyPressed() {
  final int k = keyCode;
 
  if      (k == 'A' | k == LEFT)   jumpPalette(-1);
  else if (k == 'D' | k == RIGHT)  jumpPalette(1);
  else if (k == 'W' | k == UP)     jumpPalette(-PCOLS);
  else if (k == 'S' | k == DOWN)   jumpPalette(PCOLS);
 
  redraw();
}
 
void jumpPalette(int step) {
  idx = (idx + step + grids.length) % grids.length;
}