|
Author |
Topic: Help: A fast way to fade to black? (Read 376 times) |
|
Matt Constantine Guest
|
Help: A fast way to fade to black?
« on: Feb 26th, 2004, 8:37pm » |
|
I'm looking for a fast way to fade all the pixels towards black by x amount. I wrote this, but it only works if everything is a shade of blue not full-color: void fadescr(int amnt) { int v; color c; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { c = get(x,y); v = max(int(blue(c)) - amnt,0); set(x, y, v); } } } My attempts at doing this full-color were all too slow. I could only get a limited understanding of the color model through the reference. I'm using it in a way that I very gradualy fade throughout the interaction.
|
|
|
|
TomC
|
Re: Help: A fast way to fade to black?
« Reply #1 on: Feb 26th, 2004, 10:56pm » |
|
Short answer, see this technote - http://processing.org/discourse/yabb/board_Syntax_action_displa_y_num_1043036001.html - which points to this forum page, where you should read fry's post on bitshifting colours - http://processing.org/discourse/yabb/board_Synta_x_action_display_num_1037826044.html#3 Long answer. First of all, you need to work on every color component, not just blue. Code: // your method, but with all color components... void fadescr2(int amnt) { int v; color c; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { c = get(x,y); v = color(max(int(red(c)) - amnt,0),max(int(green(c)) - amnt,0),max(int(blue(c)) - amnt,0)); set(x, y, v); } } } |
| Then, when you've worked out that's a bit slow, you change to getting the red/green/blue how fry does it, linked above, using bitshifts. Code: // the bitshifting way (see technotes for good explanations) void fadescr3(int amnt) { int red, green, blue; int newred, newgreen, newblue; for (int i = 0; i < pixels.length; i++) { // pixels[i] is an integer of the form 0xaarrggbb // get the red bits... red = (pixels[i] >> 16) & 0x000000ff; // get the green bits... green = (pixels[i] >> 8) & 0x000000ff; // get the blue bits... blue = pixels[i] & 0x000000ff; // this was the right idea newred = max(red - amnt,0); newgreen = max(green - amnt,0); newblue = max(blue - amnt,0); // put them back together... (quicker than color(r,g,b), I think pixels[i] = (newred << 16) | (newgreen << 8) | newblue; } } |
| Then you stuff it all on one line, so it doesn't clutter up your code Example here: http://www.tom-carden.co.uk/p5/fade2black/applet/ There might be a quicker way than using max(a,b), but that seems fast enough to me.
|
|
|
|
TomC
|
Re: Help: A fast way to fade to black?
« Reply #2 on: Feb 26th, 2004, 11:09pm » |
|
Oh yes, a slightly different way to do this is to use a big black rectangle with a low alpha fill value... Pretty sure that's not an identical effect, but it fades to black here all right. Code: // assumes rectMode(CORNER); void fadescr(int amnt) { fill(0,0,0,amnt); rect(0,0,width,height); } |
| (also discussed in http://processing.org/discourse/yabb/board_general_action_displ_ay_num_1075865039.html ) update: thanks K. If you're interested, because applying black with alpha 10 leaves (255-10)/255 of the original colour, the pixel-by-pixel equivalent of the low-alpha rectangle is: Code: int red, green, blue; for (int i = 0; i < pixels.length; i++) { red = (pixels[i] >> 16) & 0x000000ff; green = (pixels[i] >> 8) & 0x000000ff; blue = pixels[i] & 0x000000ff; pixels[i] = (red*(255-amnt)/255 << 16) | (green*(255-amnt)/255 << 8) | blue*(255-amnt)/255; } |
|
|
« Last Edit: Feb 27th, 2004, 12:36am by TomC » |
|
|
|
|
kevinP
|
Re: Help: A fast way to fade to black?
« Reply #3 on: Feb 26th, 2004, 11:27pm » |
|
Great answer(s)! (Wasn't my question, but thanks anyway.)
|
Kevin Pfeiffer
|
|
|
|