MULTIMEDIA

Microsoft XNA Game Studio 3.0 : Working with Colors

2/16/2011 11:30:56 AM
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.

Figure 1. Declaring a Color variable called backgroundColor


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.

Figure 2. Assigning a new Color value to backgroundColor


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.


Other  
  •  iPhone 3D Programming : Textures and Image Capture - Creating Textures with the Camera
  •  iPhone 3D Programming : Textures and Image Capture - Dealing with Size Constraints
  •  Programming with DirectX : Game Math - Vectors
  •  iPhone 3D Programming : Textures and Image Capture - Generating and Transforming OpenGL Textures with Quartz
  •  iPhone 3D Programming : Textures and Image Capture - The PowerVR SDK and Low-Precision Textures
  •  Building LOB Applications : Using Visual Studio 2010 WCF Data Services Tooling
  •  Building LOB Applications : Accessing RESTful Data using OData
  •  Programming with DirectX : Additional Texture Mapping - Image Filters
  •  Microsoft XNA Game Studio 3.0 : Making a Game Program
  •  iPhone 3D Programming : Textures and Image Capture - Texture Compression with PVRTC
  •  iPhone 3D Programming : Textures and Image Capture - Texture Formats and Types
  •  iPhone 3D Programming : Textures and Image Capture - Fight Aliasing with Filtering
  •  iPhone 3D Programming : Textures and Image Capture - Texture Coordinates Revisited
  •  Programming with DirectX : Additional Texture Mapping - Sprites
  •  Programming with DirectX : Additional Texture Mapping - Alpha Mapping
  •  Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 2) - Running the Same XNA Game on Different Devices
  •  Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 1)
  •  Programming with DirectX : Shading and Surfaces - Additional Texturing Topics
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 4) - Enabling Textures with ES2::RenderingEngine
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 3) - Enabling Textures with ES1::RenderingEngine
  •  
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Bamboo Splash - Powerful Specs And Friendly Interface
    Powered By Windows (Part 2) - Toshiba Satellite U840 Series, Philips E248C3 MODA Lightframe Monitor & HP Envy Spectre 14
    MSI X79A-GD65 8D - Power without the Cost
    Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    Windows Server 2003 : Building an Active Directory Structure (part 1) - The First Domain
    Personalize Your iPhone Case
    Speed ​​up browsing with a faster DNS
    Using and Configuring Public Folder Sharing
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 1)
    Google, privacy & you (Part 1)
    iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
    Microsoft Surface With Windows RT - Truly A Unique Tablet
    Network Configuration & Troubleshooting (Part 1)
    Panasonic Lumix GH3 – The Fastest Touchscreen-Camera (Part 2)
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005
    Exchange Server 2010 : Track Exchange Performance (part 2) - Test the Performance Limitations in a Lab
    Extra Network Hardware Round-Up (Part 2) - NAS Drives, Media Center Extenders & Games Consoles
    Windows Server 2003 : Planning a Host Name Resolution Strategy - Understanding Name Resolution Requirements
    Google’s Data Liberation Front (Part 2)
    Datacolor SpyderLensCal (Part 1)