Changing color of an image...

Blitz3D Forums/Blitz3D Programming/Changing color of an image...

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Bah, that code was really messed up in the var's, so I did a partial rewrite, along with addin' a function to just change the color of the image.
(my function there actually made a new image and returned it, this one changes the image it was given to handle)

Graphics 640,480,0,2

Global img = CreateImage(128,128)

SetBuffer ImageBuffer(img)
Color 255,0,0
Rect 0,0,128,128,1
Color 0,255,255
Rect 32,32,64,64,1


; Make IMG2 by copyin' img and changin' its color
Global img2 = ImageColor(img,150,150,150,0,255,255)

; Now lets just change IMG2's color itself
ChangeColor(img2,255,255,0,255,0,0)


SetBuffer BackBuffer()
While Not KeyHit(1)
Cls

DrawImage img,0,0
DrawImage img2,128,0

Flip
Wend
End



; Function - basically uses a multiply effect to change the colors
; image = handle of the source image
; r\g\b = the rgb you want to multiply the image with
; mr\mg\mg = the rgb of the color you want to keep out of the effect


;--------------------------------------------------;
; Creates new image, changes color, and returns it ;
;--------------------------------------------------;
Function ImageColor(image,R,G,B,MR,MG,MB)

	ii = CopyImage(image)
	
	x = ImageWidth(ii)
	y = ImageHeight(ii)

	SetBuffer ImageBuffer(ii)
	
	For i = 0 To y
		For j = 0 To x

			GetColor j,i
			
			rr = ColorRed()
			gg = ColorGreen()
			bb = ColorBlue()  

			sR# = rr / 255.0
			sG# = gg / 255.0
			sB# = bb / 255.0
			
			fR# = R * sR
			fG# = G * sG
			fB# = B * sB

			If rr <> MR And gg<> MG And bb<> MB Then  
				Color fR, fG, fB
				Rect j,i,1,1
			EndIf
		Next
	Next
	 
	SetBuffer BackBuffer()
	Return ii
	
End Function



;---------------------------------------
; Changes the Color of the given image ;
;---------------------------------------
Function ChangeColor(image,R,G,B,MR,MG,MB)

	x = ImageWidth(image)
	y = ImageHeight(image)

	SetBuffer ImageBuffer(image)
	
	For i = 0 To y
		For j = 0 To x

			GetColor j,i
			
			rr = ColorRed()
			gg = ColorGreen()
			bb = ColorBlue()  

			sR# = rr / 255.0
			sG# = gg / 255.0
			sB# = bb / 255.0
			
			fR# = R * sR
			fG# = G * sG
			fB# = B * sB

			If rr <> MR And gg<> MG And bb<> MB Then  
				Color fR, fG, fB
				Rect j,i,1,1
			EndIf
		Next
	Next
	 
	SetBuffer BackBuffer()
End Function