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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ OutputCache Duration="72000" VaryByParam="none"  Location="Any" %>
<script runat="server">
    protected void form1_Load(object sender, EventArgs e)
    {
        Response.Write("<a href='http://www.dotnetscraps.com'><img alt='.NET Scraps'" +
            "src='http://www.dotnetscraps.com/pics/dotnetscraps-logo.gif'  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("<TR>");
            Response.Write("<TD>" + myMemberInfo.Name + "</TD>");
            Response.Write("<TD bgcolor='" + myMemberInfo.Name + 
            "' borderColor='black' borderColorLight='black' borderColorDark='black'></TD>");
            if (Remainder == 1)
                Response.Write("</TR>");
            i += 1;
            if (myMemberInfo.Name == "YellowGreen")
                break;                
        }
        Response.Write("</Table>");
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Color Sample 1</title>
</head>
<body style ="font-family:verdana">
    <form id="form1" runat="server" onload="form1_Load">
    </form>
</body>
</html>

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

image

Hope that helps Wave
Rahul

Tags: , ,

.NET | Code Snippet | Color | Reflection

blog comments powered by Disqus

Comments


Who is Rahul?

Rahul

He works for Microsoft and likes Software & Gadgets!

He shares some interesting notes here while working with stuff that excites him.

Disclaimer

The opinions expressed herein are our own personal opinions and do not represent our employer's view in any way.
© Copyright 2014, Rahul Soni