Here's some code I put in the Code Archives some time ago:
It can do fade to black, fade to white, as well as a cross-fade from one image to another.
' BlitzMax Fader, by Marc van den Dikkenberg
'
' - Press any key to progress to the next fade effect.
' - Press Escape to exit.
'
' Note: the 'perc' variable is used to define the intensity of an effect percentage-wise, and
' and should be passed a value between 0 and 100 (percent).
' Use any two full-screen images to see these effects.
Graphics 800,600,32,60
SetColor 255,255,255
Local Counter:Int
pic1=LoadImage("pic1.png")
pic2=LoadImage("pic2.jpg")
DrawImage (pic1,0,0)
Flip
WaitKey()
While Not KeyHit(Key_Escape)
' do a crossfade
For Counter=0 To 100
CrossFade(pic1,pic2,Counter)
Flip
Next
WaitKey()
If KeyDown(key_escape) Then Exit
' ...and back again!
For Counter=0 To 100
CrossFade(pic2,pic1,Counter)
Flip
Next
WaitKey()
If KeyDown(key_escape) Then Exit
' Fade to white
For counter=0 To 100
DrawImage (pic1,0,0)
FadeToWhite(counter)
Flip
Next
WaitKey()
If KeyDown(key_escape) Then Exit
' Re-draw the image
DrawImage (pic1,0,0)
Flip
WaitKey()
' ...and fade to black
For Counter=0 To 200
DrawImage (pic1,0,0)
FadeToBlack(Counter/2)
Flip
Next
WaitKey()
If KeyDown(key_escape) Then Exit
Wend
Function FadeToWhite(perc:Float)
If perc>100 Then
perc=100
ElseIf perc<0 Then
perc=0
End If
If perc>0 Then
SetColor 255,255,255
SetBlend (alphablend)
SetAlpha (perc/100)
DrawRect (0,0,GraphicsWidth(),GraphicsHeight())
End If
End Function
Function FadeToBlack(perc:Float)
If perc>100 Then
perc=100
ElseIf perc<0 Then
perc=0
End If
If perc>0 Then
SetColor 0,0,0
SetBlend (alphablend)
SetAlpha (perc/100)
DrawRect (0,0,GraphicsWidth(),GraphicsHeight())
End If
End Function
Function CrossFade(pic1:timage,pic2:timage,perc:Float)
If perc<1 Then
perc=1
ElseIf perc>100 Then
perc=100
End If
SetBlend (SolidBlend)
SetColor 255,255,255
DrawImage (pic1,0,0)
SetBlend (Alphablend)
SetAlpha (perc/100)
DrawImage (pic2,0,0)
End Function
And a somewhat related one: Fade to Monochrome.
Converts a full-color image to a black-and-white version, or desaturate it to a shade in between. (Interesting effect, but much more CPU intensive)
' BlitzMax Color-to-B&W Fade Routine
' Takes a full screen image, and smoothly desaturates it.
'
' *Great* for using when pausing a game!
'
' Freeware, (C) 2005 by xlsior/Marc van den Dikkenberg
'
' - Press any key to start the fade effect.
' - Press Escape to exit.
Strict
Graphics 800,600,0
Local pixcolor:Long ' Pixel colors as read from the pixmap
Local pixred:Int
Local pixgreen:Int
Local pixblue:Int
Local pixavg:Int ' Averaged RGB values
Local orgformat:Int ' Keep track of the original PixelMap format, for conversion
Local monopix:Int ' Colors in ARGB format
Local workimg:timage
Local workpix:tpixmap
Local bwimg:timage
Local sourceimg:timage
sourceimg:timage=LoadImage("d:/working/background3.jpg",DYNAMICIMAGE)
workimg:timage=CreateImage (GraphicsWidth(),GraphicsHeight(),1,DYNAMICIMAGE)
DrawImage sourceimg,0,0
'
' blah blah blah, run a program, draw things to the backbuffer, until
' you want to invoke the effect:
'
GrabImage (workimg:timage,0,0)
Flip
workpix:tpixmap=LockImage(workimg:timage)
orgformat=PixmapFormat(workpix)
If orgformat<>PF_RGB888 Then
' if the Pixmap in a different format than these routined expect, then convert:
workpix:tpixmap=ConvertPixmap:tpixmap(workpix,PF_RGB888)
End If
For Local xx=0 To GraphicsWidth()-1
For Local yy=0 To GraphicsHeight()-1
pixcolor=ReadPixel(workpix,xx,yy)
pixred=(pixcolor & $00FF0000) Shr 16
pixgreen=(pixcolor & $FF00) Shr 8
pixblue=(pixcolor & $FF)
pixavg=(pixred*0.299)+(pixgreen*0.587)+(pixblue*0.114)
pixred=pixavg Shl 16
pixgreen=pixavg Shl 8
pixblue=pixavg
monopix=pixred | pixgreen | pixblue
WritePixel(workpix,xx,yy,monopix)
Next
Next
If orgformat<>PF_RGB888 Then
' The pixmap was converted to a different format originally.
' Now convert it back to what it was.
workpix:tpixmap=ConvertPixmap:tpixmap(workpix,orgformat)
End If
bwimg:timage=LoadImage(workpix:tpixmap)
UnlockImage(workimg:timage)
' At this point we have the following:
' workpix - A pixmap containing a black-and-white version
' bwimg - An image containing a black-and-white version
' workimg - The screen grabbed full-color version
' sourceimg - The original image loaded from disk
WaitKey()
For Local counter=0 To 100 Step 2
crossfade(workimg:timage,bwimg:timage,counter)
Flip
If KeyDown (key_Escape) Then Exit
Next
WaitKey()
For Local counter=0 To 100 Step 2
crossfade(bwimg:timage,workimg:timage,counter)
Flip
If KeyDown (key_Escape) Then Exit
Next
WaitKey()
Function CrossFade(pic1:timage,pic2:timage,perc:Float)
' Generic Crossfade routine, to fade between the full color and
' Black-and-white versions of the image.
' Parameters: sourcepic, destination pic, percentage faded
If perc<1 Then
perc=1
ElseIf perc>100 Then
perc=100
End If
SetBlend (SolidBlend)
SetColor 255,255,255
DrawImage (pic1,0,0)
SetBlend (Alphablend)
SetAlpha (perc/100)
DrawImage (pic2,0,0)
End Function