color[] tiles; int tileSize = 32; int tileCount = 256 / tileSize; int selectedTile = -1; void setup() { size(260, 260); tiles = new color[tileCount * tileCount]; for (int i = 0; i < tileCount * tileCount; i++) { tiles[i] = color(random(255), random(255), random(255)); } framerate(30); } void draw() { if (selectedTile != -1) { ArrayList neighbors = neighborsOf(selectedTile); for (int i = 0; i < neighbors.size(); i++) { color selectedColor = tiles[selectedTile]; int each = ((Integer)neighbors.get(i)).intValue(); color eachColor = tiles[each]; int redDiff = (int)(red(selectedColor) - red(eachColor)); int greenDiff = (int)(green(selectedColor) - green(eachColor)); int blueDiff = (int)(blue(selectedColor) - blue(eachColor)); float fraction = 0.1; int newEachRed = (int)(red(eachColor) + fraction * redDiff); int newEachGreen = (int)(green(eachColor) + fraction * greenDiff); int newEachBlue = (int)(blue(eachColor) + fraction * blueDiff); int newSelectedRed = (int)(red(selectedColor) - fraction * redDiff); int newSelectedGreen = (int)(green(selectedColor) - fraction * greenDiff); int newSelectedBlue = (int)(blue(selectedColor) - fraction * blueDiff); tiles[each] = color(newEachRed, newEachGreen, newEachBlue); tiles[selectedTile] = color(newSelectedRed, newSelectedGreen, newSelectedBlue); } } background(255); for (int y = 0; y < tileCount; y++) { for (int x = 0; x < tileCount; x++) { noStroke(); fill(tiles[y * tileCount + x]); rect(x * 32 + 4, y * tileSize + 4, 28, 28); } } } void mousePressed() { int mouseXWithoutBorder = mouseX - 2; int mouseYWithoutBorder = mouseY - 2; int tileX = mouseXWithoutBorder / tileSize; int tileY = mouseYWithoutBorder / tileSize; tileX = (int)constrain(tileX, 0, tileCount - 1); tileY = (int)constrain(tileY, 0, tileCount - 1); selectedTile = tileY * tileCount + tileX; } ArrayList neighborsOf(int tile) { ArrayList neighbors = new ArrayList(); int tileX = tile % tileCount; int tileY = tile / tileCount; if (tileY > 0) { if (tileX > 0) neighbors.add(new Integer(tile - tileCount - 1)); neighbors.add(new Integer(tile - tileCount)); if (tileX < (tileCount - 1)) neighbors.add(new Integer(tile - tileCount + 1)); } if (tileX > 0) neighbors.add(new Integer(tile - 1)); if (tileX < (tileCount - 1)) neighbors.add(new Integer(tile + 1)); if (tileY < (tileCount - 1)) { if (tileX > 0) neighbors.add(new Integer(tile + tileCount - 1)); neighbors.add(new Integer(tile + tileCount)); if (tileX < (tileCount - 1)) neighbors.add(new Integer(tile + tileCount + 1)); } return neighbors; }