tweening colours over time

BlitzMax Forums/BlitzMax Programming/tweening colours over time

Hi all,

Say I have 2 sets of random colours -

I would like to be able to tween the colours to the other set of colours using time t (0..1) but without actually making the colours go crazy if you know what I mean?

Because if I calculate it seperately it seems to go through other colours along the way...

Any ideas?

Tween the color difference in the 3 components with the actual value of [0..1]

for example
c1: 200,20,50
c2: 100,200,160

=> dif: -100,180,110
act color = c1 + tween * dif

' color range

Const sw=640,sh=480

SetGraphicsDriver GLMax2DDriver()
Graphics sw,sh,0
SetClsColor 120,120,120
Cls

Global r1%=100 , g1%=220 , b1%=100
Global r2%=28 , g2%=55 , b2%=240

Global t#=0 , ypos%=40

While t<1.0
	SetColor rpos(r1,r2,t) , rpos(g1,g2,t) , rpos(b1,b2,t)
	DrawLine 10,ypos,sw-20,ypos
	t:+0.0024
	ypos:+1
Wend

Flip
WaitKey
End

Function rpos%(c1%,c2%,p#)
	If c2<c1 p=-p
	Return c1+(Abs(c2-c1)*p)
End Function


thanks all :)

Damn Jim! Thats nifty keen! I was looking for the code to do this exact thing a couple months back! I just dont know why the hell I didn't ask how to do it like One Eyed Jack did?!? Thanks everybody!

You are using RGB there, the results are not correct. You need to be looking at HSB for smooth transitions.

Thats what I was talking about, but I didn't really want to push the issue. HSB is probably slower too.

Well assuming it's a nonsical few instructions more, how would I go about achieving HSB transitions ?

Here are 2 functions I've converted from the archives:
Rem
	HSB2RGB
	RGB2HSB
	
	coverted from jfk EO-11110's code archive entry
	under Algorithms
EndRem

' ==========================================

' example

Local h!,s!,l! , rgb%

h=122 ; s=0.8 ; l=0.24
rgb=HSB2RGB(h,s,l)
Print "Hue="+h+"   Sat="+s+"   lum="+l
Print "RGB="+rgb
RGB2HSB rgb , h,s,l
Print "Hue="+h+"   Sat="+s+"   lum="+l

' ==========================================

Rem
	hue(0 to 360)
	saturation (0 to 1.0)
	luminance (0 to 1.0)
EndRem
Function HSB2RGB(h!,s!,l!) 
	Local i%,f!,p!,q!,t!,r!,g!,b!
	If s=0
		r=l*255.0 ; g=l*255.0 ; b=l*255.0
	 Else
		h=h/60.0 ; I=Floor(H) ; f=h-I
		p=l*(1.0-s)
		q=l*(1.0-s*f)
		t=l*(1.0-s*(1.0-f))
		Select i
			Case 0 ; r=l ; g=t ; b=p
			Case 1 ; r=q ; g=l ; b=p
			Case 2 ; r=p ; g=l ; b=t
			Case 3 ; r=p ; b=l ; g=q
			Case 4 ; r=t ; b=l ; g=p
			Default ; r=l ; g=p ; b=q
		End Select
		r:*255 ; g:*255 ; b:*255
	EndIf
	If r<0 r=0
	If r>255 r=255
	If g<0 g=0
	If g>255 g=255
	If b<0 b=0
	If b>255 b=255
	Return (Int(r) Shl 16) | (Int(g) Shl 8) | Int(b)
End Function


Rem
	takes 24 bit rgb color and passes results to
		hue(0 to 360)
		saturation(0 to 1.0)
		luminance(0 To 1.0)
EndRem
Function RGB2HSB(rgb, hue! Var,sat! Var,lumin! Var)
	rgb=rgb & $FFFFFF
	Local r!=(rgb Shr 16) & $FF
	Local g!=(rgb Shr 8) & $FF
	Local b!=rgb & $FF
	Local my_min!=Min(r,g)
	Local my_max!=Max(r,g)
	my_min!=Min(my_min!,b)
	my_max!=Max(my_max!,b)
	Local delta!=(my_max!-my_min!)
	If my_max!<>0
		sat!=delta!/my_max!
		lumin!=my_max!/255.0
		If delta<>0
			If r=my_max
				hue=(g-b)/delta
			ElseIf g=my_max
				hue=2+((b-r)/delta)
			Else
				hue=4+((r-g)/delta)
			EndIf
		EndIf
	EndIf
	hue:*60.0
	If hue<0 hue:+360.0
End Function


Thank you very much! most kind...