Color changing help.

Blitz3D Forums/Blitz3D Programming/Color changing help.

Ross C(Posted 2008) [#1]
Hey, i'm attempting, not very well, to change the colour of my cannon, based on an overheat value.

what i'm doing currently, is getting the overheat value and dividing it by the maximum overheat value, which will give my a number between 0 and 1, so i can multiply that against 255 on the green and blue channel, so my cannon will go red, the more it overheats:

overheat_value = 300
max_overheat_value = 600

cannon_color = overheat_value / max_overheat_value

cannon_color = 0.5

EntityColor cannon_,255,255 - (255*cannon_color), 255 - (255*cannon_color)
which equals:
Entitycolor cannon_255,128,128


But, it would be alot better if i could adjust the the color to go from blue (255,255,255), to yellow to orange and then to red. 255,255,255 for blue, because that's the texture color.

NOW, there is a pattern, which is good:

blue (white basically) = 255,255,255
yellow = 255,255,0
orange = 255,128,64
red = 255,0,0

So, how can i, having a value of between 0 (no overheat, white basically) to 1 (total overheat, Red) change between those colors, simply by having that value?

I'm aware i could use lots of IF statements, but does anyone have a more elegant solution?


Ross C(Posted 2008) [#2]
S'ok, i got if with some IF statements:

	heat_red = 255
	If cannon_heat_color <=0.5 Then ; for fading to yellow
		heat_blue = (512.0 - (512 * cannon_heat_color)) - 255
		heat_green = 255
	ElseIf cannon_heat_color > 0.5 And cannon_heat_color < 0.75 Then
		heat_blue = 64*((cannon_heat_color - 0.5)*2)
		heat_green =  255 - (255 * ((cannon_heat_color-0.5)*2)) ; should only goto 128
	ElseIf cannon_heat_color => 0.75 Then
		heat_blue = 64 - (64 * ((cannon_heat_color-0.75)*4))
		heat_green = 128 - (128 * ((cannon_heat_color-0.75)*4))
	End If
	
	 
	EntityColor cannon_,heat_red,heat_green,heat_blue



Stevie G(Posted 2008) [#3]
Code uses 4pt bezier intepolation .. with a t ( timestep ) between 0..1.

Graphics 640,480,16,1

Global gR, gG, gB

SetBuffer BackBuffer()

For l = 0 To 500
	COLORinterpolate( l / 500.0 , 255,255,255,255,255,0,255,128,64,255,0,0 )
	Color gR, gG, gB
	Line 0, l, 640,l
Next

Flip

MouseWait

End


Function COLORinterpolate( t#, r1,g1,b1 , r2, g2, b2 , r3, g3, b3 , r4, g4, b4 )

	gR = r1 * (1-t)^3 + 3 * r2 * (1-t)^2 * t + 3 * r3 * (1-t) * t^2 + r4 * t^3 
	gG = g1 * (1-t)^3 + 3 * g2 * (1-t)^2 * t + 3 * g3 * (1-t) * t^2 + g4 * t^3 
	gB = b1 * (1-t)^3 + 3 * b2 * (1-t)^2 * t + 3 * b3 * (1-t) * t^2 + b4 * t^3 
	
End Function



Naughty Alien(Posted 2008) [#4]
very nice function..thanks Stevie...


Ross C(Posted 2008) [#5]
Holy jesus... Thanks :o) That's neat...

I take it for a 5 point bezier is just more of the same in that function? In fact i don't think it's that simple :o)

Where do you get this stuff from :D

Ok, it works great! That function is definetly something worth keeping.


Stevie G(Posted 2008) [#6]
I use this kinda stuff alot for creating my own bezier patch terrains etc.. but it's a very handy form of interpolation for loads of other stuff as you can see.

5pt is doable and there is more to it than you suggest ;)

Stevie


H. T. U.(Posted 2008) [#7]
That needs to be in the code archives! I spent 3 days making some code that did something similar.

P.S. FYI RossC, for more than say 3 If statements, Select is quicker.


Ross C(Posted 2008) [#8]
Thanks for the info :o) There's probably alot of other things i could optimise first though :o)