Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WPF: Compare two brushes using IsEquals

0.00/5 (No votes)
17 Dec 2012 1  
WPF Compare two brushes IsEquals

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here