Colorfade (to/from) black algorithm

BlitzPlus Forums/BlitzPlus Programming/Colorfade (to/from) black algorithm

Hansie(Posted 2003) [#1]

On the Amiga I programmed plenty of demo's where I faded to/from black an image in realtime.

Holy christ, I just cannot remember the algorithm I used, I remember I used bit-by-bit fade ... but I just cant remember how.

help out, please! and I'm talking BlitzPlus here ...

Ross C(Posted 2003) [#2]
There's one in the code archives. In fact there's quite a few. :)

Hansie(Posted 2003) [#3]
@ Ross C

yeah, I checked them all out, but they all seem to be slow or in other ways limited to Blitz3D

Kevin_(Posted 2003) [#4]
Dont forget that on the Amiga all you had to do was change the value in a colour register. On the PC it is quite different. You have to manually change the RGB of each pixel colour. However, some people have managed to get this working at a reasonable speed.

Hansie(Posted 2003) [#5]

yeah, you're right. I still think Assembler on the Amiga logically, which just complicates things ...

Kevin_(Posted 2003) [#6]
Programming in Assembler on the Amiga A500 was great because everyone had the same chipset which meant you had cross-compatability. Those days are gone I'm afraid due to every PC being different. You can get away with some Assembler on the PC but if you hit the hardware direct then you are asking for trouble.


Ross C(Posted 2003) [#7]
@Hansie, it really depends on the size of the image. Fading to black will be quite quick, as there's no reading from vram required. Just blend the piexels to/from back. :)

Hansie(Posted 2003) [#8]
@Ross C

yeah, but what are the fastest BlitzPlus commands for blending pixels?

Ross C(Posted 2003) [#9]
I think you would use lockedpixels command. I've only just recently bought Blitz plus, so i'm not up to speed yet :)

Mr Brine(Posted 2003) [#10]
Yo dude

You could use the gamma channel commands to fade to black
this is pretty fast.

Mr Brine

darklordz(Posted 2003) [#11]
yea but they only work in fullscreen!@

Bremer(Posted 2003) [#12]
Lockedpixels command together with RPF and WPF. You can boost the speed if you store the image RGB values in an array or a bank and then either read it from there or peek it from the bank. You can do full screen fades at a decent speed if you program it right.

Seldon(Posted 2003) [#13]
@Hansie: it isn't just a matter of assembly. Original Amiga chipset can offer just 256 colours (HAM and HAM24 is a trick) so you only need to change the register of each colour. With a PC you could also do that if you opened a palette based 256 colours screen, but that is *not* possible in 16/24 bits modes (also with Amiga).

@zawran: If you use LockedPixels you can access the image buffer like a bank, right ? I mean you can use Poke/Peek, with no copy needed into a bank ?

Hansie(Posted 2003) [#14]

I have never used the GAMMA commands in BlitzPlus. Has anyone experience with these?

Bremer(Posted 2003) [#15]
Yes, but they only work in full screen mode, so I forgot about them again because I tend to either use windowed or give the user an option, so its no good.

Mr Brine(Posted 2003) [#16]
Hi hansie,

Ive had some experince, I cant remember a great deal about em but I did code this kewl little function that lets you manipulate the gamma channels in a nice user friendly way, any how check it out:


;r/g/bint : rgb intensity (0 - minimum, 255 - maximum)
Function SetGammaRGBIntensity(rint#, gint#, bint#)

Local r#, g#, b#

Local ri# = rint / 255.0
Local gi# = gint / 255.0
Local bi# = bint / 255.0

For w = 0 To 255

SetGamma w, w, w, r, g, b
r = r + ri
g = g + gi
b = b + bi



End Function


so set the color palette to normal call:

SetGammaRGBIntensity(255, 255, 255)

to set the color palette to black call:

SetGammaRGBIntensity(0, 0, 0)

to fade the color palette to black:

for x = 255 to 0 step -1

SetGammaRGBIntensity(x, x, x)


You can also get other palette effects by setting the different intensity values to different values. Things to note:

- its only useable in full screen mode.
- the amount of processor time this function takes is pretty variable, the updategamma function seems to only update values in the gamma channel it needs to. Tests on my computer (p4 2ghz with a gforce fx5600 running at 60fps) showed it was consuming approx 10 - 30% of availavle processor time depending on which rgb intensity values were being modified.


Mr Brine

Hansie(Posted 2003) [#17]

Thanx :-D

Hansie(Posted 2004) [#18]

Do you have a working sample for Windows mode?

Bremer(Posted 2004) [#19]
Here is some pseudo code for you:

for each pixel in buffer
read pixel from buffer
transform pixel to individual r,g,b values
minus fadenumber from each value
make sure the values does not pass boundry
write combined rgb value back into buffer

I Hope thats of any use for you.

Hansie(Posted 2004) [#20]
using only lockbuffer(), unlockbuffer(), readpixelfast() and writepixelfast()??

Bremer(Posted 2004) [#21]
yes, why not? You read all the pixels of the screen, then either add or subtract something from the red, green and blue values depending on whether you are fading to white or black and then write them back again. For a 640x480 screen you should be getting about 30fps on a P4 2.4ghz, thats what I can get on mine anyways.

big pete(Posted 2004) [#22]
Keep on coding another few months Hansie, and maybe you'll even be able to finish that fading routine you promised some day... Didn't you already claim it was finished a few months ago? LOL

Bremer(Posted 2004) [#23]
If you want to speed things up, then you can pull the image into an array before you start you fading and then read the data from the array instead of reading the pixels off the buffer. That way you should be able to get about 45fps on a P4 2.4ghz instead of the 30fps, and it only takes one frame to read the image over, so that might be worth it if you are targeting lower end systems as well.

Murilo(Posted 2004) [#24]
That's what I did zawran. My fade routine can fade between images or to any colour, and it works in both a window and full screen. I first make a bank containing the RBG values of the source image (which takes no time at all), and they simply cross fade between them.

Hansie(Posted 2004) [#25]
@big pete

don't forget your daily medication! the red pill is important!

Bremer(Posted 2004) [#26]
@Hansie, I think between what have been mentioned here and the code archieves you should be able to figure something out. And it would be wisely to code it as functions capable of receiving: bufferhandles, x/y starting points and width/height. Because this way you are preparing the function to be reusable for just about any situation that you would need to fade something.

Good luck and happy coding.