Play with Colors - Part 1 - See all the colors using .NET reflection

by Rahul 6/10/2008 7:10:00 PM

When you work on designing a website, you may find it difficult to remember all the color names and visualize them. At least, I am personally not very good with color selection and find it really hard to recall the color names. So, in this scrap we will check out how to use .NET reflection to print out all the color names along with the colors...

There is a very small piece of C# code which I have used here to create a 2 column color table.

Check the sample

And here is the code that would do it for you...

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.Drawing" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<%@ OutputCache Duration="72000" VaryByParam="none"  Location="Any" %>
<script runat="server">
    protected void form1_Load(object sender, EventArgs e)
Response.Write("<a href=''><img alt='.NET Scraps'" +
"src=''  style='border:0' /></a>");
//Put user code to initialize the page here
        Response.Write("<Table width='100%' border=1 cellspacing=3 cellpadding=3>");
Response.Write("<TR bgcolor='LightCyan'><TD><B>Color Name</TD><TD width='30%'>" +
"<B>Color</TD><TD><B>Color Name</TD><TD width='30%'><B>Color</TD>");
//Get the assembly's information
        Color x =  new Color();
Type strType = x.GetType();
MemberInfo[] arrMemberInfo = strType.GetProperties();
//Lets Enumerate the colors now in three columns
        int i=0;
int Remainder = 0;
foreach (MemberInfo myMemberInfo in arrMemberInfo)
Remainder = i % 2;
if(Remainder == 0)
Response.Write("<TD>" + myMemberInfo.Name + "</TD>");
Response.Write("<TD bgcolor='" + myMemberInfo.Name + 
"' borderColor='black' borderColorLight='black' borderColorDark='black'></TD>");
if (Remainder == 1)
i += 1;
if (myMemberInfo.Name == "YellowGreen")
<html xmlns="">
<head runat="server">
    <title>Color Sample 1</title>
<body style ="font-family:verdana">
    <form id="form1" runat="server" onload="form1_Load">

Here is the output... (Check the live sample)


Hope that helps Wave

Tags: ,

.NET | Tips and Tricks

blog comments powered by Disqus