XNA Tutorials/Color Text
From BluWiki
Contents |
Coloring Text Output in C#
Written by Weston Castleberg
Part of a series of XNA Tutorials.
Displaying Text on the Screen
This article will only explain how to color text. How to display text on the screen in C# is beyond the scope of this article. For more information on displaying text, please see Displaying Text.
Coloring Text Output to the Screen
Below is a fully functional code segment. The statement required to change the color of the text is in bold.
Microsoft.XNA.Framework.Graphics.SpriteBatch display = new Microsoft.XNA.Framework.Graphics.SpriteBatch(); display.Begin(); display.DrawString(myFont, "Hello World!", new Vector2(0.0f,0.0f), new Color()); display.End();
In the above code, the default constructor for the Color object is called. The default constructor will initialize a Color object to be white; that is, by default, the red, green, and blue values of a Color object are all 0.
Initializing a Color Object
If one wishes to initialize a color object to something besides white, then that person has two options
Using a Static Instance of Color
The Color class in C# already supports a number of predefined (or static) instances. These static instances include all of the commonly known colors, such as Red, Orange, Yellow, and Purple. They also include some less commonly known hues such as SeaShell, Peru, and BurlyWood. To use one of these static instances, one must simply use the Color object's name. It is unnecessary and incorrect to instantiate a new Color object. For example:
Microsoft.XNA.Framework.Graphics.SpriteBatch display = new Microsoft.XNA.Framework.Graphics.SpriteBatch(); display.Begin(); display.DrawString(myFont, "Hello World!", new Vector2(0.0f,0.0f), Color.BurlyWood); display.End();
Creating a New Color
As many obscure colors as are represented by the static objects in the Color class, they don't come close to representing every possible color which is capable of being rendered by the Color class (some 16777216 different hues). So, it is possible to create a new Color object and initialize it with custom values for Red, Green, and Blue. For example:
Microsoft.XNA.Framework.Graphics.SpriteBatch display = new Microsoft.XNA.Framework.Graphics.SpriteBatch(); display.Begin(); //the below text will be Aquamarine in color display.DrawString(myFont, "Hello World!", new Vector2(0.0f,0.0f), new Color(127.0f, 255.0f, 212.0f)); display.End();
It is necessary to note that two different constructors may be called here. The first constructor takes three bytes, the second constructor takes three floats. Therefore, one must be sure to cast variables accordingly, or the ambiguous call will be ignored and the Color object will be assigned its default value. An example of casting preexisting variables:
int red = 127; int blue = 255; int green = 212; Microsoft.XNA.Framework.Graphics.SpriteBatch display = new Microsoft.XNA.Framework.Graphics.SpriteBatch(); display.Begin(); display.DrawString(myFont, "Hello World!", new Vector2(0.0f,0.0f), new Color((byte)red, (byte)blue, (byte)green)); display.End();
Generating Random Colors
With the knowledge above, it should be relatively easy to introduce a random color generator. Random number generation itself is beyond the scope of this article; for more information please see Generating Random Numbers.
It is possible to generate a random color by calling the random number generator for each of the red, green, and blue values to be sent to the constructor. For example:
Random gen = new Random(); Microsoft.XNA.Framework.Graphics.SpriteBatch display = new Microsoft.XNA.Framework.Graphics.SpriteBatch(); display.Begin(); display.DrawString(myFont, "Hello World!", new Vector2(0.0f,0.0f), new Color((byte)gen(255), (byte)gen(255), (byte)gen(255))); display.End();



