Changing Alpha Values

BlitzMax Forums/BlitzMax Programming/Changing Alpha Values

BLaBZ(Posted 2010) [#1]
I'd like to convert my images/textures alpha areas to a certain color,

any ideas on how one might go about changing an images alpha values?


Jesse(Posted 2010) [#2]
the only way is to go through each of the pixels and set it that way. The alpha does not have a color setting. If you are planning to use SetColor, the image itself will be affected. A more feasible option would be to crate a pure white image color $FFFFFFFF display it before the image with the "SetAlpha" to what ever value you want to set it and "SetColor" to whatever color you want the back image to be:

SetBlend ALPHABLEND
.
.
.
SetAlpha .5
SetColor 0,255,0 
DrawImage whiteImg,100,100 ' draws it as green semi transparent
setAlpha 1.0
setColor 255,255,255
DrawImage img,100,100' the image displayed over a semi green background.



zambani(Posted 2010) [#3]
Try converting your Timage to TPixmap. Then read in each pixels alpha value and set the color of pixel.
Below is something I whipped up. May not be the fastest but it works as advertised :-)
SuperStrict
Graphics(800, 600, 0, 30)

Local iImage:TImage = LoadImage("r:/temp/barn.png")	'Change to your image
Local oImage:TImage = LoadImage(alphaToColor(iImage, $ff, 0, 0))
SetBlend(ALPHABLEND)

While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
	Cls()
	DrawImage(oImage, 0, 0)
	Flip(-1)
Wend

Function alphaToColor:TPixmap(im:TImage, r:Int, g:Int, b:Int)
	'Returns Pixmap with color changed based on alpha
	Local i:TPixmap
	Local i2:TPixmap = CreatePixmap(im.width, im.height, PF_RGBA8888)
	Local p:Int, pr:Int,al:Int
	i = LockImage(im)
	Local x:Int, y:Int
	For y = 0 Until i.height
		For x = 0 Until i.width
			p = 0
			pr = i.ReadPixel(x, y)
			al = (pr & $ff000000)     'al now has alpha value
			If al = 0	'USE THIS IF STATMENT TO TEST WHEN YOU TOWANT REPLACE THE ALPHA WITH YOUR SPECIFIED COLOR
				pr = $ff000000 | (r Shl 16) | (g Shl 8) | b
			End If
			i2.WritePixel(x, y, pr)
		Next
	Next
	UnlockImage(im)
	Return i2
End Function



Jesse(Posted 2010) [#4]
@zambani
this works good with images that are not anti aliased other wise you are going to be left with a nasty outline around the image.


zambani(Posted 2010) [#5]
@Jesse,
True, True!