locked
How to convert RGBA value into its equivalent hex code? RRS feed

  • Question

  • I want to convert my RGBA Value into its hex code.

    Any code sample?


    Pradeep AJ

    Tuesday, June 25, 2013 4:05 PM

Answers

  • You can use this:


    public static class ExtensionMethods
    {
        public static Color ToColor(this uint argb)
        {
            return Color.FromArgb((byte)((argb & -16777216) >> 0x18),
                                    (byte)((argb & 0xff0000) >> 0x10),
                                    (byte)((argb & 0xff00) >> 8),
                                    (byte)(argb & 0xff));
        }
    
        public static string ToHex(this Color color)
        {
            return String.Format("#{0}{1}{2}{3}"
                , color.A.ToString("X").Length == 1 ? String.Format("0{0}", color.A.ToString("X")) : color.A.ToString("X")
                , color.R.ToString("X").Length == 1 ? String.Format("0{0}", color.R.ToString("X")) : color.R.ToString("X")
                , color.G.ToString("X").Length == 1 ? String.Format("0{0}", color.G.ToString("X")) : color.G.ToString("X")
                , color.B.ToString("X").Length == 1 ? String.Format("0{0}", color.B.ToString("X")) : color.B.ToString("X"));
        }
    }


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you. Happy Coding!!!


    • Edited by Vishal Kaushik Tuesday, June 25, 2013 5:00 PM formatting issue
    • Marked as answer by Pradeep AJ Wednesday, June 26, 2013 2:50 AM
    Tuesday, June 25, 2013 4:58 PM