Tuesday, November 13, 2007

Question: How do I tint the color of a Texture2D using the SpriteBatch?

Answer: To tint the color of a Texture2D when using the Draw call of a SpriteBatch, you need to change the Color parameter. For example, to tint the background of a sprite to the color orange, you would make this call in the Draw method:

spriteBatch.Draw(myTexture, new Vector2(0, 0), Color.Orange);

The Color.Orange parameter is the important parameter. This tells the SpriteBatch to draw the texture with an overall orange-ish background. An example of the same texture drawn twice is shown below:



Figure 1: On the left there is the texture drawn with a white background. On the left, the same texture is drawn with an orange background.

The same texture was drawn once with Color.White. This value specifies that you want no changes to the background of the texture (i.e. if the original background was blue and you specified Color.White, the background would stay blue). Then the texture was drawn with Color.Orange. This drew the sprite with an orange-ish color. Note that if the original background was blue, the background would not turn orange, but instead the blue background would be mixed with the orange color to create a new, odd color.

No comments: