Calculates the difference between two Colors - CSharp System.Drawing

CSharp examples for System.Drawing:Color Calculation

Description

Calculates the difference between two Colors

Demo Code


using System.Windows.Media;
using System.Globalization;
using System;//from   w w w  .ja  v  a2s  .  co  m

public class Main{
        /// <summary>
        /// Calculates the difference between two <see cref="Color"/>s
        /// </summary>
        /// <param name="c1">
        /// </param>
        /// <param name="c2">
        /// </param>
        /// <returns>
        /// L2-norm in RGBA space
        /// </returns>
        public static double ColorDifference(Color c1, Color c2)
        {
            // http://en.wikipedia.org/wiki/Color_difference
            // http://mathworld.wolfram.com/L2-Norm.html
            double dr = (c1.R - c2.R) / 255.0;
            double dg = (c1.G - c2.G) / 255.0;
            double db = (c1.B - c2.B) / 255.0;
            double da = (c1.A - c2.A) / 255.0;
            double e = dr * dr + dg * dg + db * db + da * da;
            return Math.Sqrt(e);
        }
}

Related Tutorials