Click here to Skip to main content
Click here to Skip to main content

Color Value Formats

, 30 Oct 2005
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) .

License

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

About the Author

Ali Raza Shaikh
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??? Pinmemberlcdmendes26-Sep-07 8:57 
GeneralCool app Pinmemberreinux30-Oct-05 14:42 
GeneralRe: Cool app PinmemberAli Raza Shaikh30-Oct-05 14:47 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 30 Oct 2005
Article Copyright 2005 by Ali Raza Shaikh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid