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

3
  • Two different types of Object. Commented Feb 25, 2013 at 10:33
  • Hi Ani. The question you suggest is a duplicate is nonsense - it doesn't even contain any words! Commented Feb 25, 2013 at 10:34
  • @ColonelPanic, using the suggested duplicate as a reference this would be pretty easy to solve. It seem like you have put any effort in yourself. Commented Feb 25, 2013 at 10:44

3 Answers 3

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; } 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.