Saturday, October 30, 2010

Comparing Colors in C Sharp

Sometimes there could be problems in comparing two colors in C Sharp because we cant direct compare two colors using (==) operator. One handy solution is that write a function that compares each component of the color as:


public bool comp(Color A, Color B)
{
if (A.A.Equals(B.A) && A.R.Equals(B.R) && A.G.Equals(B.G) && A.B.Equals(B.B))
{
return true;
}
else
{
return false;
}
}


And now you can check two colors like

if(comp(Color.Black,Color.White)==true)
{
...
}

No comments:

Post a Comment