How to achieve smooth color changing animation on ColorRect?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By anbarasu chinna

I have created a simple shooting game. Mid of the game, player can level up automatically. To give better user experience on level up I tried to change the background color of ColorRect with following code.

extends ColorRect

  var totalTime = 0
  var endTime = 25

   func _process(delta):
      if Globals.State == Globals.GameState.Running:
           totalTime += delta
           if totalTime > endTime:
              totalTime = 0
              randomize()
              var r = rand_range(50, 200) / 255
              var g = rand_range(50, 200) / 255
              var b = rand_range(50, 200) / 255
              set_frame_color(Color(r,g,b,1))

This code working fine and changing color of the ColorRect instantly. I do not want the instant color changing behavior. I want the color should be changed smoothly over time with animation.

How to achieve this.
Thanks in advance.

:bust_in_silhouette: Reply From: Adam_S

This is very easy to achieve with the Tween node. The code would be something like this:

$Tween.interpolate_property($ColorRect,"color",$ColorRect.color,Color.red,2,Tween.TRANS_LINEAR)
$Tween.start()

This would animate the current color to red with a duration of 2 seconds.

If you need help, let me know.