18

I need to serialize a color used in a WPF application to a database. I'd like to use the sRGB values, because they're more familiar to those of us that have spent the last few years doing web development.

How can a get an ARGB string (like #FFFFFFFF) from a System.Windows.Media.Color object?

UPDATE: I was misled by the documentation on MSDN. As @Kris noted below, the documentation for the ToString() method is incorrect. Although it says that ToString() "creates a string representation of the color using the ScRGB channels", it will actually return a string in ARGB hex format if the color was created using the FromARGB() method. It's an undocumented feature, I suppose.

See http://msdn.microsoft.com/en-us/library/ms606572.aspx

dthrasher
  • 40,656
  • 34
  • 113
  • 139

7 Answers7

24

If you create your colors using either Color.FromRgb or Color.FromArgb instead of FromScRgb you should get a hex string result from ToString.

If you want to do it manually

string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", c.A, c.R, c.G, c.B);

You can use int.Parse(,NumberStyles.HexNumber) to go the other way.

Note sRGB and scRGB refer to different color spaces, make sure your using the one you want.

Kris
  • 7,110
  • 2
  • 24
  • 26
  • You mean that if I created the Color object using FromArgb, ToString will return the ARGB hex string instead of the ScRgb value? That contradicts what it says in the MSDN documentation: http://msdn.microsoft.com/en-us/library/ms606572.aspx. – dthrasher Jul 23 '10 at 14:34
  • I hadn't noticed that in the documentation but it does generate a hex format when using those methods. Looking in reflector a flag is set which is used in the ToString implementation. – Kris Jul 23 '10 at 17:16
  • Yup. ToString() gives me the format I want. Looks like the MSDN documentation is incomplete. – dthrasher Jul 23 '10 at 18:11
  • ToString works if created as mentioned. https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Media/Color.cs,7df649cf464cd38e,references – user487779 Nov 16 '20 at 22:44
10

You can also do it this way:

string myHex = new ColorConverter().ConvertToString(myColor);
Anthony Bobenrieth
  • 2,758
  • 1
  • 26
  • 38
5

I created a struct to handle conversion and serialisation. It solves two problems for me: it is serialisable and it corrects the spelling ;)

[Serializable]
public struct Colour
    {
    public byte A;
    public byte R;
    public byte G;
    public byte B;

    public Colour(byte a, byte r, byte g, byte b)
        {
        A = a;
        R = r;
        G = g;
        B = b;
        }

    public Colour(Color color)
        : this(color.A, color.R, color.G, color.B)
        {
        }

    public static implicit operator Colour(Color color)
        {
        return new Colour(color);
        }

    public static implicit operator Color(Colour colour)
        {
        return Color.FromArgb(colour.A, colour.R, colour.G, colour.B);
        }
    }

Just use Colour where you would otherwise use System.Windows.Media.Color

Phil J Pearson
  • 547
  • 6
  • 11
2

If your purpose is to serialize to a file and deserialize back to the color object, I think you are better of to convert color to an Int32 and vice versa. It is no brainier to serialize/deserialize Int32. If this is your purpose, here is the code: Color To Int32:

        byte[] color = new byte[4];
        color[0] = Color.B;
        color[1] = Color.G;
        color[2] = Color.R;
        color[3] = Color.A;
        Int32 intColor = System.BitConverter.ToInt32(color, 0);

Int32 To Color:

byte[] bytes = System.BitConverter.GetBytes(intColor);
Color =new System.Windows.Media.Color(){B= bytes[0], G=bytes[1], R=bytes[2], A=bytes[3]};
Wayne Lo
  • 3,689
  • 2
  • 28
  • 28
1

This answer is for GDI colors, and not WPF, so might not be much help.

You can get the HTML color string (and back) like this

System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#F5F7F8");
String strHtmlColor = System.Drawing.ColorTranslator.ToHtml(c);

here is the MSDN documentation.

LJNielsenDk
  • 1,414
  • 1
  • 16
  • 32
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
1

There is inbuild implementation for System.Windows.Media.Color toString() method which gives hexcode of the color.

Snippet from the class interface it self

        //
        // Summary:
        //     Creates a string representation of the color using the sRGB channels.
        //
        // Returns:
        //     The string representation of the color. The default implementation represents
        //     the System.Byte values in hex form, prefixes with the # character, and starts
        //     with the alpha channel. For example, the System.Windows.Media.Color.ToString()
        //     value for System.Windows.Media.Colors.AliceBlue is #FFF0F8FF.
        public override string ToString();
Vivek Dhiman
  • 319
  • 1
  • 7
0

You can get the A, R, G and B values from a Color instance as bytes, so you just need to convert the bytes to hex and concatenate the hex values as strings.

byte[] to hex string

Community
  • 1
  • 1
AndrewS
  • 3,450
  • 1
  • 21
  • 26