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

Convert RGB Color to hex

By , 5 May 2011
 
private string HexFromRGB(int r, int g, int b)
        {
            return ColorTranslator.FromHtml(String.Format("#{0:X2}{1:X2}{2:X2}", r, g, b)).Name.Remove(0,2);
        }

License

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

About the Author

charles henington
United States United States
Member
I do not claim to be wrong! I just rarely ever write.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralYou are correct Chris, sorry was in the middle of two differ...membercharles henington14 May '11 - 6:27 
You are correct Chris, sorry was in the middle of two different projects when posted this tip but still no excuse., the string.Format() method would easily suffice for formatting the rgb to hex. EdMan apoligies for the lack of explanation on the tip/trick, thought the title gave it away, sometimes we forget to remember that the tip/tricks that we supply are also read by newbies to the language and should be adaquately explained. String.Format is a very important tool you will want to read up on this topic. A simple example would be MessageBox.Show(String.Format("Your Name is {0}!!!", textBox1.Text)); if the text in textBox1 is EdMan the output would be.... Your Name Is EdMan!!!
GeneralRe: That sound better :) And yes, I probably ought to read up on...memberEdMan19615 May '11 - 4:14 
That sound better Smile | :) And yes, I probably ought to read up on string formatting, but I have never really needed it Poke tongue | ;-P
GeneralWouldn't have an answer to what Chris Trelawny-Ross said but...memberEdMan1967 May '11 - 23:38 
Wouldn't have an answer to what Chris Trelawny-Ross said but it would have been nice for you to explain what the code actually does, given that I, along with many others I'm sure, have little or nor idea about and string formatting Poke tongue | ;-P
GeneralRe: check out my replymembercharles henington15 May '11 - 4:07 
check out my reply
GeneralUmm... the string.Format() method (with the format string yo...memberChris Trelawny-Ross6 May '11 - 12:04 
Umm... the string.Format() method (with the format string you're using) will quite nicely give you the hex value of the RGB values you pass in, without having to involve the ColorTranslator.
 
Am I missing something?

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 6 May 2011
Article Copyright 2011 by charles henington
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid