65.9K
CodeProject is changing. Read more.
Home

Color Value Formats

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.28/5 (10 votes)

Oct 30, 2005

CPOL

1 min read

viewsIcon

45910

downloadIcon

697

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.

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.

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.

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 :).