Subby
Subby

Reputation: 5480

XNA Change background colour programmatically

Is there a way to change the colour/color of the background programmatically and constantly, for example, I want the beginning colour to be perfectly blue (for example), then every Update() call I want the background to slowly change to green, then yellow, then blue etc.... I want the colours to fade in to the next colour and not just suddenly switch.

Any ideas how I can achieve this? I know there's GraphicsDevice.Clear(Color.CornflowerBlue); but that's obviously not what I want.

Upvotes: 0

Views: 5435

Answers (2)

Lucius
Lucius

Reputation: 3745

GraphicsDevice.Clear is exactly what you want.

All you still need is a way of interpolating between colors:

var red = Color.Red;
var green = Color.Green;

// Gives you a color at half the distance between red and green
var color = Color.Lerp(red, green, 0.5f);

If you want to go through the whole color palette you could convert the color to HSL or HSV and animate the hue.

Upvotes: 1

Seth Battin
Seth Battin

Reputation: 2851

@BrianRasmussen is completely correct. You can vary the clear color every frame. Use one of the Color constructors that takes number values instead of the color enumeration values.

This one,

GraphicsDevice.Clear(new Color(byte r, byte g, byte b));

Or this one

GraphicsDevice.Clear(new Color(float r, float g, float b, float a));

And each frame, update your values of r, g, and b. Easy.

Upvotes: 1

Related Questions