fast color replace

BlitzPlus Forums/BlitzPlus Programming/fast color replace

here's a trick to replace one colour by another lightningfast i saw that skn3[ac] did a code that do the same job in the newsletter (a real nice piece of code)

i've got 1-2ms for the entire 800x600 screen and 3ms for a 1280x1024 my code don't use any special b+ commands except some optional parameters for cretateimage() i have comented the line to replace for b2D compatibility so just comment the B+code and uncomment the b2D code if you wan't to use it in b2D/ 3d(not tested)

 ;//-------------------------DEMO PART--------------------
Graphics 800,600,0,1
Global imge=CreateImage(800,600,1,2) ;blitz +
;Global imge=CreateImage(800,600) ;blitz 2D
SetBuffer ImageBuffer(imge)
Color 255,0,0
For a=1 To 100
	Text Rnd(800),Rnd(600),"this is the color to replace"
Next
SetBuffer BackBuffer()
DrawBlock imge,0,0
Color 255,255,255
Text 0,0,"press any key to replace the color "
Flip
WaitKey 
Global k
;call the Function
WaitKey
k=MilliSecs()
imge=replacecolor(255,0,0,255,255,0,imge)
timer=MilliSecs()-k
DrawBlock imge,0,0
Color 255,255,255:Text 0,0,"taken "+timer

Flip
WaitKey
;-----------------
End
;----------------/DEMOPART------------------//

	;---------------------
	;function
	;---------------------
Function replacecolor(rz,gz,bz,rr,gg,bb,image)
	w=ImageWidth(image)
	h=ImageHeight(image)
	temp=CreateImage(w,h,1,2) ; blitz +
	;temp=CreateImage(w,h); blitz 2d
	SetBuffer ImageBuffer(temp) 
	Color rr,gg,bb				
	Rect 0,0,w,h,1				
	MaskImage image,rz,gz,bz 
	DrawImage image,0,0		 
	SetBuffer BackBuffer()	 
	FreeImage image			 
	Return temp				 
End Function


hope someone like this trick :)

I guess with modification this code could finally be the only fast fade-to/from-black 2D code ...

not realy, you can only change one color at a time so to change 100 colors you'll have to run it 100 times. still way to slow for a fade code

I have yet to find a fullscreen 2D fade to/from black that works smoothely

Ford Escort....

Very clever! I can see this being an extremely useful function. If it could be modified to include x,y,w,h parameters instead of the whole screen it would be even more handy :)

Regards

;
;usage:
;
;zone_replacecolor(x,y,width,height,red,green,blue,red,green,blue,buffer)
;
;buffer is the buffer to draw
;ex: zone_replacecolor(0,0,100,100,0,0,0,255,255,255,imagebuffer(mypicture))
; zone_replacecolor(0,0,100,100,0,0,0,255,255,255,backbuffer())
;
Function zone_replacecolor(x,y,w,h,rz,gz,bz,rr,gg,bb,buffer)
usedbuffer=GraphicsBuffer()
temp=CreateImage(w,h,1,2)
SetBuffer buffer
GrabImage temp,x,y
Color rr,gg,bb
Rect x,y,w,h,1
MaskImage temp,rz,gz,bz
DrawImage temp,x,y
SetBuffer usedbuffer
FreeImage temp
End Function

Amazing! This could be used as an extremely fast 'Flood Fill' function just for specific areas of the screen.

I reckon you should stick this in the code archives.

Regards