|
Author |
Topic: color drain || (Read 645 times) |
|
rgovostes
|
color drain ||
« on: Sep 4th, 2004, 11:37pm » |
|
Version two of an applet I posted a while ago. I tried to make it a little faster than last time, but it could still use some work. I implemented fading to make it appear animated. I took out HSB calculations to hopefully make it run faster, but I don't notice much of a difference (probably because I never ended up implementing some bitwise stuff)... Sorry it's not hosted, my site is down. Code:// Colors v2, Sep 04 '04 // by Ryan Govostes BImage nextImage, thisImage; float r, g, b; int fade = 0; float layer = 0; void setup() { size(350, 350); background(255); // Generate two images thisImage = doDraw(1, 10); nextImage = doDraw(1, 10); } void loop() { // Fade thisImage into nextImage - many thanks, toxi! if(fade < 256) { image(thisImage, 0, 0); tint(255, 255, 255, fade); image(nextImage, 0, 0); fade += 4; } else { thisImage = nextImage; nextImage = doDraw(1, 10); fade = 0; } } BImage doDraw(int complexity, int maxDetail_alpha) { BImage out = new BImage(width, height); color avgColor, newColor; float re, gr, bl, hu, sa, br; int rndMode, maxDetail; // Set the first pixel to a random color colourMode(HSB); out.pixels[0] = color(random(255), random(255), 255, 255); // Set each pixel to a slightly modified average of its neighbors for(int i = 1; i < pixels.length; i ++) { // Get the average color of neighbors colourMode(RGB); if(i < width) { // First row avgColor = out.pixels[i - 1]; // (left) } else if(i % width == 0) { // First column countColor(out.pixels[i - width]); // (up) countColor(out.pixels[i - (width - 1)]); // (up, right) avgColor = color((r * 0.50), (g * 0.50), (b * 0.50)); } else if(i % width == (width - 1)) { // Last column countColor(out.pixels[i - width]); // (up) countColor(out.pixels[i - (width + 1)]); // (up, left) countColor(out.pixels[i - 1]); // (left) avgColor = color((r * 0.33), (g * 0.33), (b * 0.33)); } else { // Anywhere else countColor(out.pixels[i - (width + 1)]); // (up, left) countColor(out.pixels[i - width]); // (up) countColor(out.pixels[i - (width - 1)]); // (up, right) countColor(out.pixels[i - 1]); // (left) avgColor = color((r * 0.25), (g * 0.25), (b * 0.25)); } r = 0; g = 0; b = 0; // Set starting maximum detail maxDetail = maxDetail_alpha; // Shift to new color newColor = avgColor; for(int j = 0; j < complexity; j ++) { // Store current color values re = red(newColor); hu = hue(newColor); gr = green(newColor); sa = saturation(newColor); bl = blue(newColor); br = brightness(newColor); // Preform value adjustments rndMode = int(random(2.9)); // 5.9 for HSB too; switch(rndMode) { case 0: { // Shift red re += random(-maxDetail, maxDetail); //re += noise(i % width, layer); break; } case 1: { // Shift green gr += random(-maxDetail, maxDetail); //gr += noise(i % width, layer); break; } case 2: { // Shift blue bl += random(-maxDetail, maxDetail); //bl += noise(i % width, layer); break; } case 3: { // Shift hue hu += random(-maxDetail, maxDetail); //hu += noise(i % width, layer); break; } case 4: { // Shift saturation sa += random(-maxDetail, maxDetail); //sa += noise(i % width, layer); break; } case 5: { // Shift brightness br += random(-maxDetail, maxDetail); //br += noise(i % width, layer); break; } } maxDetail -= maxDetail_alpha / (complexity + 1); // Store adjusted color if(rndMode < 3) { colourMode(RGB); //newColor = ((int)re << 16) + ((int)gr << 8) + (int)bl + 0xFF000000; // interesting effect newColor = color(re, gr, bl, 255); } else { colourMode(HSB); newColor = color(hu, sa, br, 255); } } // Insert new color (yay) out.pixels[i] = newColor; } return out; } // Increment the red/green/blue variables void countColor(color c) { r += (c >> 16) & 0xff; g += (c >> 8) & 0xff; b += c & 0xff; } // Replacement colorMode() function int colorMode; void colourMode(int i) { colorMode = i; colorMode(i, 255, 255, 255, 255); } |
|
|
|
|
|
|