You have seen that XNA has a set of colors built in, including one with the strange name of Teal (it is actually a rather boring blue/green). However, you want to make your own colors and use these in your program.
1. Storing Color Values
A particular color is represented by a structure that holds the red, green, and blue intensity values. A structure
is used to hold a number of related data items in the same way that you
might write your name, address, and phone number on a piece of paper.
You want to create your own colors, and you need somewhere to store the color values you create. In programming terms, this is called declaring a variable. Figure 1 shows the anatomy of the statement that declares a variable to hold a value that represents a color.
The type of the variable is set as Color.
This determines what you can put in your variable. Having seen this
declaration, the C# compiler knows that you want to create a location
with the name backgroundColor in memory, which can hold color information. In programming terms, the name of a variable is called an identifier.backgroundColor
is an identifier that I’ve invented. When you create something to use
in a C# program, you have to think up an identifier for it. An
identifier is made up of numbers and letters and must start with a
letter. The identifier should describe what you are going to use the
thing for; in this program, you are storing the color that is going to
be used for the background, so it can be given the identifier backgroundColor. The word
Note:
The C# compiler uses
the type of a variable to make sure that a program never tries to do
something that would be stupid. The value Color.Red is recognized by the compiler as being of type Color, and can therefore be placed in a variable of type Color. If the programmer wrote some code that tried to put something else in the variable backgroundColor,
such as a player name, then the program would fail to compile. This is
rather like real life, where an attempt to put an elephant in a camera
case would be similarly unsuccessful.
Note:
Our Great Programmer says that there should be a special place in hell reserved for programmers who create identifiers like X24, or secretMagicCode, or clunk.
She says that these tell a reader of the program code nothing about
what the variable is being used for. She really likes identifiers like CarSpeed, backgroundColor, and accountBalance.
2. Setting a Color Value
You now have a variable
that can hold the color of your background. At the moment, it is not set
to anything useful. So next, you have to write a statement that causes
the game program to put a value into this variable. You start by
creating a new Color value that contains a particular amount of red, blue, and green. Figure 2 shows the anatomy of an assignment that makes a new Color value and then places it in the variable.
The thing that is going to be assigned is on the right side of the equals sign. In this case, you are making a new Color
value. Don’t get this confused with a double-equals that might be used
to compare two things. You should regard the equals sign in Figure 2-4
as being what I call a "gozzinta" operator. The value on the right of
the equals sign "goes into" the variable on the left. You can use it in the game program:
GraphicsDevice.Clear(backgroundColor);
The preceding statement calls the Clear method and feeds it the value of backgroundColor.
This causes the screen to be cleared to the new color you created. If
you put these statements together, you get a game program that contains a
backgroundColor variable that is used by the Draw method, which sets it to a value and then clears the screen using it:
protected override void Draw(GameTime gameTime)
{
Color backgroundColor;
backgroundColor = new Color(0,0,0);
GraphicsDevice.Clear(backgroundColor);
base.Draw(gameTime);
}
If you want to find out
what color you get if you make one with no red, no green, and no blue,
you can run a program that uses this Draw
method. But I don’t think I’m giving too much away when I tell you that
this would produce a black screen. The actual color values are given in
the order red, green, and blue, and each must be in the range 0 to 255
(you shall learn the reason for this later). By using different values
when you set the Color, you can
experiment with different displays. The color combinations obey all the
rules of color combinations (for light, rather than for paint) that you
would expect:
backgroundColor = new Color(255, 255, 0);
The preceding statement sets backgroundColor to a color value that has the red and green values at maximum, which would be displayed as yellow.