Click here to Skip to main content
6,822,613 members and growing! (20,434 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Color Value Formats

By Ali Raza Shaikh

This Application will show the current selected color in three Formats i.e RGB Format, Hexadecimal Format and Interger Format
C#, Windows, .NET1.1VS.NET2003, Dev
Posted:30 Oct 2005
Views:25,765
Bookmarked:9 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 2.28 Rating: 2.28 out of 5
5 votes, 50.0%
1
1 vote, 10.0%
2

3
1 vote, 10.0%
4
3 votes, 30.0%
5

Sample Image - Color_Value_Formats.jpg

Introduction

This Application will show the Format of the current selected color in three Formats i.e RGB Format, Hexadecimal Format and Interger Format. You can change the value of any format, the other formats will adjust by themself.

System.Drawing.Color

This namespace almost provide all the colors used in the daily life Applications, if you want to make your own color, then this namespace provide the following fucntion.

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. Now the Following code will convert the Decimal Representation into Hexadeciaml 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 within different formats. Current Color is displayed with the help of Picture Box.
this.pictureBox1.BackColor=Color.FromArgb(Convert.ToInt32(this.RedValue.Text),Convert.ToInt32(this.GreenValue.Text),Convert.ToInt32(this.BlueValue.Text));

Decimal Conversion

When the change is made to the integer value of 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 of making this program comes when i get fustrated, this is the time when i was working on different Graphical Tools, the problem there is that some support the RDB Color Format, some support Hexadecimal Format and others....

So instead of calculating again & again, i have this nice program on my desktop now :).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Ali Raza Shaikh


Member
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
Occupation: Software Developer
Location: Pakistan Pakistan

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralWhy not??? Pinmemberlcdmendes29:57 6 Sep '07  
GeneralCool app Pinmemberreinux15:42 30 Oct '05  
GeneralRe: Cool app PinmemberAli Raza Shaikh15:47 30 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 30 Oct 2005
Editor:
Copyright 2005 by Ali Raza Shaikh
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project