ARTICLE

Getting All Colors in XNA

Posted by Ibrahim Ersoy Articles | XNA with C# July 08, 2012
I was playing with XNA today and was curious about if i can populate all the colors supported by XNA.For that i used Reflection method.
Reader Level:
Download Files:
 

To display all the colors,i've used combobox from windows controls

First of all we need to get the Type of Color:

Type structType = typeof(Color);

Then we use PropertyInfo to get all the controls:

PropertyInfo[] fields = structType.GetProperties();

The reason why we didnt use FieldInfo instead of PropertyInfo is Each color is actually Property and have fields like R,G,B,A.

So we iterated through PropertyInfo and populated our combobox object.
 

foreach (PropertyInfo field in fields)

{

    cbo.Items.Add(field.Name);

}

When we run it,we'll be getting extra useless items:

1.png

So what to do?

I've written a simple iteration to remove these useless items:
 

for (int a = 0; a <= 5; a++)

{

    cbo.Items.RemoveAt(0);

}

It removes the first 6 items from the list.

Simple but useful.As it seems.

2.png

So?

Our work is done here.We got our Colors populated.

Hope it helps.

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts