Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / C#

Color Value Formats

Rate me:
Please Sign up or sign in to vote.
2.28/5 (10 votes)
30 Oct 2005CPOL1 min read 45.5K   687   12   3
This application will show the currently selected color in three formats: RGB, hexadecimal, and interger.

Sample Image - Color_Value_Formats.jpg

Introduction

This application will show the format of the currently selected color in three formats: RGB, hexadecimal, and integer. You can change the value in any format, and the other values will be adjusted accordingly.

System.Drawing.Color

This namespace provides all the colors used in the most common applications; if you want to make your own color, then this namespace provides the following function:

  • Color.FromArgb: Creates a Color structure from the four 8-bit ARGB component (alpha, red, green, and blue) values.

Hexadecimal Conversion

A integer value of the RBG color can be obtained from the Color.FromArgb(int argb) function. The following code will convert the decimal representation into the hexadecimal representation.

C#
private string DeciamlToHexadeciaml(int number)
{
    string[] hexvalues={"0","1","2","3","4","5","6","7",
                        "8","9","A","B","C","D","E","F"};
    string result=null, final=null;
    int rem=0,div=0;        

    while(true)
    {
        rem=(number%16);                                
        result+=hexvalues[rem].ToString();                

        if (number<16)
            break;

        number=(number/16);
    }                
    
    for (int i=(result.Length-1); i>=0; i--)
    {
        final+=result[i];
    }

    return final;
}

Current Color

The Current Color box will show the current color selected in different formats. The Current Color is displayed with the help of a PictureBox.

C#
this.pictureBox1.BackColor=Color.FromArgb(Convert.ToInt32(this.RedValue.Text), 
  Convert.ToInt32(this.GreenValue.Text),Convert.ToInt32(this.BlueValue.Text));

Decimal Conversion

When a change is made to the integer value of an RGB color, then this integer value is changed into the corresponding hexadecimal value.

C#
private int GetNumberFromNotation(char c)
{
    if (c=='A')
        return 10;
    else if (c=='B')
        return 11;
    else if (c=='C')
        return 12;
    else if (c=='D')
        return 13;
    else if (c=='E')
        return 14;
    else if (c=='F')
        return 15;

    return Convert.ToInt32(c.ToString());
}

private int HexadecimaltoDecimal(string hexadecimal)
{
    int result=0;

    for (int i=0; i<hexadecimal.Length; i++)
    {
        result+=Convert.ToInt32(this.GetNumberFromNotation(hexadecimal[i])*
                    Math.Pow(16,Convert.ToInt32(hexadecimal.Length)-(i+1)));
    }
    
    return Convert.ToInt32(result);
}

Motivation

The idea for making this program came when I got frustrated while I was working on different graphical tools, the problem there is that some support the RDB color format, some support the hexadecimal format, and others....

So instead of having to calculate them myself again and again, I now have this nice program on my desktop :).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
Ali Raza Shaikh did his BS (CS) from FAST-NUCES, Karachi Campus. He is also a Microsoft Certified Application Developer. He have a great passion for programming and all the programming related stuff. He can be reached at alirazashaikh.blogspot.com

Comments and Discussions

 
QuestionWhy not??? Pin
lcdmendes26-Sep-07 8:57
lcdmendes26-Sep-07 8:57 
GeneralCool app Pin
Rei Miyasaka30-Oct-05 14:42
Rei Miyasaka30-Oct-05 14:42 
GeneralRe: Cool app Pin
Ali Raza Shaikh30-Oct-05 14:47
Ali Raza Shaikh30-Oct-05 14:47 
Thanx, for increasing my knowledge Wink | ;)

Final Year Student
BS(Computer Science)
FAST (Karachi Campus)
Microsoft Certified Application Developer.net

alirazashaikh.blogspot.com
www.programmersparadise.cjb.net

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.