0

How to test whether a System.Drawing.Color and a System.Windows.Media.Color describe the same colour?

I tried

colour1 == colour2

but I get an error

Operator '==' cannot be applied to operands of type 'System.Drawing.Color' and 'System.Windows.Media.Color

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465

3 Answers3

2

You have 2 options:

  1. Convert from one type to the other, which is covered here, and then use the '==' operator.

  2. Compare the individual components. Since both of them have the R, G, B, A properties as bytes, you can simply do:

    bool ColorsEqual (System.Drawing.Color c1, System.Windows.Media.Color c2)
    {
        return c1.R == c2.R && c1.G == c2.G 
            && c1.B == c2.B && c1.A == c2.A;
    }
    
Community
  • 1
  • 1
Tibi
  • 4,015
  • 8
  • 40
  • 64
0

As there is no operator== overloaded for these two types, you could acquire the string-values of the color or the ARGB-values.

System.Drawing.Color c1 = System.Drawing.Color.FromArgb(255,0,0,0);
System.Windows.Media.Color c2 = System.Windows.Media.Color(255,0,0,0);
if(c1.A == c2.A && c1.R == c2.R && ...

Look here and here.

bash.d
  • 13,029
  • 3
  • 29
  • 42
0

You can make an extension method to the System.Drawing.Color that converts to a System.Windows.Media.Color and then compare on the System.Windows.Media.Color type:

public static System.Windows.Media.Color ToMediaColor(this System.Drawing.Color color)
{
    return System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
}
Catalin M.
  • 652
  • 5
  • 13