SetColor/GetColor with hex value instead of R,G,B

BlitzMax Forums/BlitzMax Beginners Area/SetColor/GetColor with hex value instead of R,G,B

Ranoka(Posted 2011) [#1]
Is it possible to store color values as a single variable, or do you have to store the R, G and B values separately?

SetColor/GetColor with hex value instead of R,G,B

So instead of

Local R:Int = 255
Local G:Int = 255
Local B:Int = 255

SetColor(R, G, B)


Something like

Local Col:Uint = FFFFFF

SetColor(Col)


This would be helpful because I want to store dynamic colors.


Jesse(Posted 2011) [#2]
no, but you can make your own function to do that. it is really simple:
function colorSet(n:int)
     setcolor (n shr 16) & $FF, (n shr 8) & $FF, n & $FF
end function


function colorGet:int()
   local r:int,g:int,b:int   
   getColor r,g,b
  return (r shl 16)|(g shl 8)| b
end function


untested might have some syntax bugs

Last edited 2011


Ranoka(Posted 2011) [#3]
This is perfect.

The only tiny syntax bug, was the extra parenthesis on the colorSet function.

This is more convenient than storing 3 different variables.
And bit shifting is fast, so shouldn't be much slower.

Thanks for the fast response!

I also found out that you use the dollar symbol, like $FFFFFF for hex numbers.

Last edited 2011


Zeke(Posted 2011) [#4]
Graphics 800 , 600

Function SetColor(r:Int , g:Int = - 1 , b:Int = - 1)
	If g = - 1
		Local col:Byte Ptr = Varptr r
		brl.max2d.SetColor col[2],col[1],col[0]
	Else
		brl.max2d.SetColor r , g , b
	EndIf
End Function

Cls
SetColor($FF0000)
DrawText "Red text" , 10 , 10
SetColor(0 , 0 , 255)
DrawText "Blue text" , 50 , 50
Flip
WaitKey


Last edited 2011


Ranoka(Posted 2011) [#5]
Thanks for sharing Zeke,
That's nice because you can use both hex and r,g,b without using a new function (Shame there isn't function overloading).
I didn't think of doing it that way.