How To Convert Color To HexaDecimal String in ASP.NET

      private string ColorToHexDecimal(Color color)

      {

            int spaceRed, spaceGreen, spaceBlue;

            //Hexa Decimal Symbols 0-9 and A-F represents 10-15

            string[] hexaDecimalSymbols = new string[] { "0", "1", "2", "3", "4", "5", "6",

"7", "8", "9", "A", "B", "C", "D", "E", "F" };

            int base16 = 16;

            //RGB color space

            spaceRed = color.R;

            spaceGreen = color.G;

            spaceBlue = color.B;

 

            string hex = hexaDecimalSymbols[spaceRed / base16] +

                                     hexaDecimalSymbols[spaceRed % base16]

                        + hexaDecimalSymbols[spaceGreen / base16] +

                           hexaDecimalSymbols[spaceGreen % base16]

                        + hexaDecimalSymbols[spaceBlue / base16] +

                                          hexaDecimalSymbols[spaceBlue % base16];

            return "#" + hex;

     }

To test the conversion method, please use the following

string hexDacimal = ColorToHexDecimal(Color.Red);

 



Alternate Titles: Color TO hexa,RGB to HTML HEXA, C# Color To Hexa Decimal Symbol , VB.NET Color To Hexa Decimal Digits