# Monogame - how to make a smooth color transition with Color.Lerp()?

I would like to know how to dynamically change a color. My code:

Color myColor;
Color startColor = new Color(255, 255, 255);
Color finalColor = new Color(231, 214, 90);
myColor = Color.Lerp(startColor, finalColor, 0.5f);

spriteBatch.Draw(myTexture, Vector2.Zero, myColor);


But the color does not change, in fact I get something similar to the finalColor, that's perhaps because I need to decrement the RGB values of startColor and check if they match finalColor? Help would be much appreciated.

If you look at the Color.Lerp documentation you'll notice that the third parameter is the amount.

public static Color Lerp(Color value1, Color value2, float amount)


amount - A value between 0 and 1.0 indicating the weight of value2.

What this means is that to get the color to change you'll need to change the amount over time between values of 0.0 and 1.0. To do this, create a member variable in your class and change it in the Update method.

 private float _colorAmount = 0.0f;

protected override void Update(GameTime gameTime)
{
var deltaSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

_colorAmount += deltaTime;

if(_colorAmount > 1.0f)
_colorAmount = 0.0f;

// rest of update method...
}

protected override void Draw(GameTime gameTime)
{
// start of draw method..

var startColor = new Color(255, 255, 255);
var finalColor = new Color(231, 214, 90);
var myColor = Color.Lerp(startColor, finalColor, _colorAmount);

spriteBatch.Draw(myTexture, Vector2.Zero, myColor);

// rest of draw method...
}


Obviously the actual logic you use will differ from the example here, but this should give you enough to get started.

• I can't see why this isn't directly useable but I'd wrap _colorAmount, rather than truncate it. Consider adding, as an example, running _colorAmount through a trig function to get a nice repeating back and forth between -1:1, scaled to 0:1. – Jon Mar 15 '16 at 1:13
• Maybe show how actually uncomplicated it is too: Color Lerp(Color c0, Color c1, float t) { return (1-t)*c0 + t*c1; } – Jon Mar 15 '16 at 1:15
• @Jon I realize my example won't produce the nicest animation but I think it's better to keep the example as simple as possible without complicating it with extra information. There's loads of different ways to interpolate between 0 and 1 once you understand the basics. – craftworkgames Mar 15 '16 at 1:28
• @craftworkgames Oh now I see. I read those docs but I hadn't drank my coffee yet so I couldn't figure it out. Thanks for the help! – PowerUser Mar 15 '16 at 12:22
• You're welcome. Animation code is fun to write. – craftworkgames Mar 15 '16 at 12:39