Colour transition

BlitzMax Forums/BlitzMax Programming/Colour transition

siread(Posted 2009) [#1]
Does anyone have a good algorithm for smoothly changing one colour into another?


xlsior(Posted 2009) [#2]
I've used this to generate gradients:


buttonwidth:int=200
buttonheight:int=100

' Start and End color in HEX format
RGBIn=$8b9eb1
RGBOut=$768796
bgr:Float=(RGBin & $FF0000) Shr 16
bgg:Float=(RGBin & $00FF00) Shr 8
bgb:Float=(RGBin & $0000FF)
egr:Float=(RGBout & $FF0000) Shr 16
egg:Float=(RGBout & $00FF00) Shr 8
egb:Float=(RGBout & $0000FF)

For t:Float = 0 To ButtonWidth
newMod:Float=((1.0/ButtonWidth)*t)
oldMod:Float=1-((1.0/ButtonWidth)*t)
SetColor (bgr*oldmod)+(egr*newmod),(bgg*oldmod)+(egg*newmod),(bgb*oldmod)+(egb*newmod)
DrawLine t,0,t,ButtonHeight*2
Next



although depending on what you need it for (size, amount, how frequently?) and whether you use DX or OpenGL, there are low-level commands that draw quads with gradients applied to them which would be more efficient.


siread(Posted 2009) [#3]
Thanks Marc, that's great. :)


Thareh(Posted 2009) [#4]
I create gradients by creating a pixmap 1x2 pixels for example, one pixel for each color.
Then I convert it into a TImage and use DrawImageRect().
It's the fastest way I've found so far :P


plash(Posted 2009) [#5]
I create gradients by creating a pixmap 1x2 pixels for example, one pixel for each color.
Then I convert it into a TImage and use DrawImageRect().
If you want to get down and dirty, you could set the vertex colors and get a much faster gradient.

This is specific to OpenGL, but it shouldn't be hard to make one for DirectX as well (but the implementation would be nasty without some modifications to brl.d3d7max2d):


Then you could throw in my modifications of brl.glmax2d and make the gradients rotate and scale (given access to transform vars).


Thareh(Posted 2009) [#6]
Yeah, Vertex colors are fast. But they get screwed up by SetColor and other stuff later on.