// make tartan pattern image // // mouse cursor move : pattern change // mouse left click : Save the pattern image // mouse right click : Display only the pattern image boolean imgSaveEnable = true; PGraphics pg; int old_mouseX, old_mouseY; long rseed; int msgtimer; String msg; void setup() { size(640, 640); pg = createGraphics(128, 128); old_mouseX = -1; old_mouseY = -1; rseed = 0; msgtimer = 0; msg = ""; frameRate(60); } void draw() { background(0); int new_mouseX = (mouseX / 2) * 2; int new_mouseY = (mouseY / 2) * 2; if (old_mouseX != new_mouseX || old_mouseY != new_mouseY) { rseed = new_mouseY * width + new_mouseX; randomSeed(rseed); boolean cross = (random(100) > 70)? false : true; int bg_r = (int)random(256); int bg_g = (int)random(256); int bg_b = (int)random(256); int layerlength = (int)random(1, 4); Layer[] layer = new Layer[layerlength]; for (int i=0; i< layerlength; i++) { layer[i] = new Layer(cross); } pg.beginDraw(); pg.background(bg_r, bg_g, bg_b); for (int i=0; i< layerlength; i++) { int w = layer[i].img.width; int h = layer[i].img.height; int blendmode = layer[i].blendmode; pg.blend(layer[i].img, 0, 0, w, h, 0, 0, w, h, blendmode); } pg.endDraw(); old_mouseX = new_mouseX; old_mouseY = new_mouseY; } if (mousePressed && (mouseButton == RIGHT)) { image(pg, 128 * 2, 128 * 2); } else { for (int y = 0; y < height; y += pg.height) { for (int x = 0; x < width; x += pg.width) { image(pg, x, y); } } } if (imgSaveEnable) { textSize(18); text("Left Click : Save the pattern image", 16, 32); if (msgtimer > 0) { text(msg, 16, 64); msgtimer--; } } } void mouseClicked () { if (imgSaveEnable) { if ( mouseButton == LEFT ) { String imagename = str((int)rseed) + ".png"; pg.save(imagename); msg = imagename + " is saved."; msgtimer = 60; } } } class Layer { PImage img; int ofs, bwidth, l_r, l_g, l_b; int blendmode; int[] blendKind = {MULTIPLY, SCREEN}; Layer(boolean cross) { if (!cross) { img = get_pattern(pg.width, pg.height, cross); } else { img = createImage(pg.width, pg.height, ARGB); PImage pimg = get_pattern(img.width / 2, img.height / 2, cross); pimg.loadPixels(); img.loadPixels(); // flip copy for (int y=0; y < pimg.height; y++ ) { for (int x=0; x < pimg.width; x++ ) { int src = y * pimg.width + x; int dst0 = y * img.width + x; int dst1 = y * img.width + (img.width - 1 - x); int dst2 = (img.height -1 - y) * img.width + x; int dst3 = (img.height - 1 - y) * img.width + (img.width - 1 - x); img.pixels[dst0] = pimg.pixels[src]; img.pixels[dst1] = pimg.pixels[src]; img.pixels[dst2] = pimg.pixels[src]; img.pixels[dst3] = pimg.pixels[src]; } } pimg.updatePixels(); img.updatePixels(); } } PImage get_pattern(int w, int h, boolean cross) { PImage pimg = createImage(w, h, ARGB); ofs = (int)random(pimg.height); bwidth = (int)random(pimg.height / 2); l_r = (int)random(256); l_g = (int)random(256); l_b = (int)random(256); blendmode = blendKind[(int)random(blendKind.length)]; if (cross || random(100) < 50 || bwidth < 4) { draw_fillline(pimg, l_r, l_g, l_b, ofs, bwidth); } else { draw_hatchingline(pimg, l_r, l_g, l_b, ofs, bwidth); } return pimg; } void draw_fillline(PImage pimg, int l_r, int l_g, int l_b, int ofs, int bwidth) { pimg.loadPixels(); // horizontal line for (int y = ofs; y < ofs + bwidth; y++) { for (int x = 0; x < pimg.width; x++) { int adrs = (y % pimg.height) * pimg.width + (x % pimg.width); pimg.pixels[adrs] = color(l_r, l_g, l_b); } } // vertical line for (int x = ofs; x < ofs + bwidth; x++) { for (int y = 0; y < pimg.height; y++) { int adrs = (y % pimg.height) * pimg.width + (x % pimg.width); pimg.pixels[adrs] = color(l_r, l_g, l_b); } } // cross (RGB / 2) for (int y = ofs; y < ofs + bwidth; y++) { for (int x = ofs; x < ofs + bwidth; x++) { int adrs = (y % pimg.height) * pimg.width + (x % pimg.width); pimg.pixels[adrs] = color(l_r / 2, l_g / 2, l_b / 2); } } pimg.updatePixels(); } void draw_hatchingline(PImage pimg, int l_r, int l_g, int l_b, int ofs, int bwidth) { pimg.loadPixels(); for (int y=ofs; y < ofs + bwidth; y++) { for (int x=0; x < pimg.width; x++) { if (y % 4 != x % 4) continue; int adrs = (y % pimg.height) * pimg.width + (x % pimg.width); pimg.pixels[adrs] = color(l_r, l_g, l_b); } } for (int x=ofs; x < ofs + bwidth; x++) { for (int y=0; y < pimg.height; y++) { if (y % 4 != (x + 2) % 4) continue; int adrs = (y % pimg.height) * pimg.width + (x % pimg.width); pimg.pixels[adrs] = color(l_r, l_g, l_b); } } pimg.updatePixels(); } }