Introduction
Code to compare two brushes using IsEquals
in WPF.
Using the code
public static bool IsEqual(Brush aBrush1, Brush aBrush2)
{
if (aBrush1.GetType() != aBrush2.GetType())
return false;
else
{
if (aBrush1 is SolidColorBrush)
{
return (aBrush1 as SolidColorBrush).Color ==
(aBrush2 as SolidColorBrush).Color &&
(aBrush1 as SolidColorBrush).Opacity == (aBrush2 as SolidColorBrush).Opacity;
}
else if (aBrush1 is LinearGradientBrush)
{
bool result = true;
result = (aBrush1 as LinearGradientBrush).ColorInterpolationMode ==
(aBrush2 as LinearGradientBrush).ColorInterpolationMode && result;
result = (aBrush1 as LinearGradientBrush).EndPoint ==
(aBrush2 as LinearGradientBrush).EndPoint && result;
result = (aBrush1 as LinearGradientBrush).MappingMode ==
(aBrush2 as LinearGradientBrush).MappingMode && result;
result = (aBrush1 as LinearGradientBrush).Opacity ==
(aBrush2 as LinearGradientBrush).Opacity && result;
result = (aBrush1 as LinearGradientBrush).StartPoint ==
(aBrush2 as LinearGradientBrush).StartPoint && result;
result = (aBrush1 as LinearGradientBrush).SpreadMethod ==
(aBrush2 as LinearGradientBrush).SpreadMethod && result;
result = (aBrush1 as LinearGradientBrush).GradientStops.Count ==
(aBrush2 as LinearGradientBrush).GradientStops.Count && result;
if (result && (aBrush1 as LinearGradientBrush).GradientStops.Count ==
(aBrush2 as LinearGradientBrush).GradientStops.Count)
{
for (int i = 0; i < (aBrush1 as LinearGradientBrush).GradientStops.Count; i++)
{
result = (aBrush1 as LinearGradientBrush).GradientStops[i].Color ==
(aBrush2 as LinearGradientBrush).GradientStops[i].Color && result;
result = (aBrush1 as LinearGradientBrush).GradientStops[i].Offset ==
(aBrush2 as LinearGradientBrush).GradientStops[i].Offset && result;
if (!result)
return result;
}
}
return result;
}
else if (aBrush1 is RadialGradientBrush)
{
bool result = true;
result = (aBrush1 as RadialGradientBrush).ColorInterpolationMode ==
(aBrush2 as RadialGradientBrush).ColorInterpolationMode && result;
result = (aBrush1 as RadialGradientBrush).GradientOrigin ==
(aBrush2 as RadialGradientBrush).GradientOrigin && result;
result = (aBrush1 as RadialGradientBrush).MappingMode == (aBrush2 as RadialGradientBrush).MappingMode && result;
result = (aBrush1 as RadialGradientBrush).Opacity == (aBrush2 as RadialGradientBrush).Opacity && result;
result = (aBrush1 as RadialGradientBrush).RadiusX == (aBrush2 as RadialGradientBrush).RadiusX && result;
result = (aBrush1 as RadialGradientBrush).RadiusY == (aBrush2 as RadialGradientBrush).RadiusY && result;
result = (aBrush1 as RadialGradientBrush).SpreadMethod == (aBrush2 as RadialGradientBrush).SpreadMethod && result;
result = (aBrush1 as RadialGradientBrush).GradientStops.Count == (aBrush2 as RadialGradientBrush).GradientStops.Count && result;
if (result && (aBrush1 as RadialGradientBrush).GradientStops.Count == (aBrush2 as RadialGradientBrush).GradientStops.Count)
{
for (int i = 0; i < (aBrush1 as RadialGradientBrush).GradientStops.Count; i++)
{
result = (aBrush1 as RadialGradientBrush).GradientStops[i].Color ==
(aBrush2 as RadialGradientBrush).GradientStops[i].Color && result;
result = (aBrush1 as RadialGradientBrush).GradientStops[i].Offset ==
(aBrush2 as RadialGradientBrush).GradientStops[i].Offset && result;
if (!result)
return result;
}
}
return result;
}
else if (aBrush1 is ImageBrush)
{
bool result = true;
result = (aBrush1 as ImageBrush).AlignmentX == (aBrush2 as ImageBrush).AlignmentX && result;
result = (aBrush1 as ImageBrush).AlignmentY == (aBrush2 as ImageBrush).AlignmentY && result;
result = (aBrush1 as ImageBrush).Opacity == (aBrush2 as ImageBrush).Opacity && result;
result = (aBrush1 as ImageBrush).Stretch == (aBrush2 as ImageBrush).Stretch && result;
result = (aBrush1 as ImageBrush).TileMode == (aBrush2 as ImageBrush).TileMode && result;
result = (aBrush1 as ImageBrush).Viewbox == (aBrush2 as ImageBrush).Viewbox && result;
result = (aBrush1 as ImageBrush).ViewboxUnits == (aBrush2 as ImageBrush).ViewboxUnits && result;
result = (aBrush1 as ImageBrush).Viewport == (aBrush2 as ImageBrush).Viewport && result;
result = (aBrush1 as ImageBrush).ViewportUnits == (aBrush2 as ImageBrush).ViewportUnits && result;
result = (aBrush1 as ImageBrush).ImageSource == (aBrush2 as ImageBrush).ImageSource && result;
return result;
}
}
return false;
}