Enhanced SetClsColor

BlitzMax Forums/BlitzMax Programming/Enhanced SetClsColor

SetClsColor with alpha: (should be fixed now)

Rem
BBDoc: Like BRL.SetCLSColor, but allowes for alpha backgrounds.
About: Useful if you want to save images with alpha using GrabPixmap.
EndRem
Function CE_SetClsColor( red%,green%,blue% ,alpha#=1.0)
		red=Max(Min(red,255),0)
		green=Max(Min(green,255),0)
		blue=Max(Min(blue,255),0)
		alpha=Max(min(alpha,1.0),0.0)
		If TD3D7Max2DDriver(_max2dDriver)
			TD3D7Max2DDriver(_max2dDriver).clscolor=(Int(alpha*255) Shl 24) |(red Shl 16)|(green Shl 8)|blue
		Else
			glClearColor red/255.0,green/255.0,blue/255.0,alpha
		EndIf
End Function


This is interesting and useful...thanks!

Hi Jake, I must be doing something wrong as I get
Bitwise Operator can only be used with integers
.
I did have to add an End Function and brackets.
I changed to
		If TD3D7Max2DDriver(_max2dDriver)
			TD3D7Max2DDriver(_max2dDriver).clscolor=Int((alpha And 255) Shl 24) |(red Shl 16)|(green Shl 8)|blue
		Else
			glClearColor red/255.0,green/255.0,blue/255.0,alpha
		EndIf


which seems to work.

Shouldn't it be
(alpha & 255)

?

Yes it should be & instead of And.
see this example:
Print 12345 And 6789
Print 12345 & 6789

The output is different becouse "And" is optimized for logical operations, and "&" is the regular boolean operator.

How do I get this to work then and can somebody provide an example of its usefulness?

Damn, I forgot about two golden rules.
1.) Never post code before you got your first morning coffee
2.) Never do quick changes to posted code before test compiling it.

I rearranged my code while posting, so this errors. Sorry for that, should be fine now.

I'm being very dense but what is it used for and how does it differ from the normal setclscolor?

It appears to add an Alpha value, as well as RGB.

And the comment in the code posted also says "Useful if you want to save images with alpha using GrabPixmap.", which I believe is not how it currently works.

Yep, thanks for that.
This is where I am being dense. I can read the code and the comments but I would really like to see an example of it being used.

Hm, seems like I tested this only with OpenGL. DX-Alpha got ignored somehow. Here's the line from d3d7max2d:
clscolor=$ff000000|(red Shl 16)|(green Shl 8)|blue

Changing $ff000000 to something different doesn't make a difference.

Here's an example that, using the OpenGL driver, works:


'setgraphicsdriver glmax2ddriver()
graphics 800,600,0,0

while not keydown(key_escape)
	CE_SetClsColor(100,200,100,0)
	cls
	setcolor (255,0,0)
	drawrect (100,100,200,200)
	if keyhit(KEY_SPACE)
		local pm:TPixmap=GrabPixmap (0,0,400,400)
		SavePixmapPNG(pm,"test.png")
		notify ("Image ~qtest.png~q written!")
	End If
	flip
WEnd



Function CE_SetClsColor( red%,green%,blue% ,alpha#=1.0)
		red=Max(Min(red,255),0)
		green=Max(Min(green,255),0)
		blue=Max(Min(blue,255),0)
		alpha=Max(min(alpha,1.0),0.0)
		If TD3D7Max2DDriver(_max2dDriver)
			'from brl-d3d7max2d.bmx: clscolor=$ff000000|(red Shl 16)|(green Shl 8)|blue
			TD3D7Max2DDriver(_max2dDriver).clscolor=(int(alpha*255) Shl 24) |(red Shl 16)|(green Shl 8)|blue
		Else
			glClearColor red/255.0,green/255.0,blue/255.0,alpha
		EndIf
End Function