2

I have colors like "FF7B2900" from a program. I need to be able to convert this kind of color code to color and back.

So for example to convert this code to color i'm doing like this:

            string colorcode = "FF7B2900";
            int argb = Int32.Parse(colorcode, NumberStyles.HexNumber);
            Color clr = Color.FromArgb(argb);

The problem is when i try to convert color to string because i need to pick a color with color picker and get (string) the same type of code together with alpha "FF7B2900" not the simple html code "#7B2900"

Thanks

adi sba
  • 621
  • 1
  • 12
  • 32
  • possible duplicate of [How to get Color from Hex color code using .NET?](http://stackoverflow.com/questions/2109756/how-to-get-color-from-hex-color-code-using-net) – LarsTech Jun 20 '12 at 16:46

2 Answers2

2

You can get the exact same hex string back by doing clr.ToArgb().ToString("x").ToUpper(). clr.ToArgb() gets your int back and calling ToString("x") tells it to ouput a hexadecimal string representation of the int.

JamieSee
  • 12,696
  • 2
  • 31
  • 47
0

the FF at the beginning refers to alpha, of 255. You can always remove the # at the beginning, and prefix with FF... that'll give you the same color.

  • ok my problem is that i have also colors like this "57087B00" what should i do in this case – adi sba Jun 20 '12 at 16:51
  • Add a # in front of them. :-) Web colors which are 8 digit - #AARRGGBB use ARGB, so it works nicely. When Alpha is FF, it can be omitted to give #RRGGBB. Hope this explanation helps. – Karthik Kumar Viswanathan Jun 20 '12 at 17:00
  • The 59 from "57087B00" represents the alpha and i needed and the problem is to get code from color with alpha i need also the alpha code i cannot put # in front – adi sba Jun 20 '12 at 18:30
  • in HTML, you are allowed to use #57087B00 ...it means a color of R=0x08, G=0x7B, B=0x00 and Alpha=0x57 – Karthik Kumar Viswanathan Jun 20 '12 at 18:36