public static int DoFullCompare(int R1, int G1, int B1, int R2, int G2, int B2) { ColorFormulas oColor1 = new ColorFormulas(R1, G1, B1); ColorFormulas oColor2 = new ColorFormulas(R2, G2, B2); return(oColor1.CompareTo(oColor2)); }
/// /// The smaller the number returned by this, the closer the colors are /// /// /// public int CompareTo(ColorFormulas oComparisionColor) { // Based upon the Delta-E (1976) formula at easyrgb.com (http://www.easyrgb.com/index.php?X=DELT&H=03#text3) double DeltaE = Math.Sqrt(Math.Pow((CieL - oComparisionColor.CieL), 2) + Math.Pow((CieA - oComparisionColor.CieA), 2) + Math.Pow((CieB - oComparisionColor.CieB), 2)); return(Convert.ToInt16(Math.Round(DeltaE))); }
private static int CompareHexColors(string firstColor, string secondColor) { if (firstColor.IndexOf("#", StringComparison.Ordinal) != -1) { firstColor = firstColor.Replace("#", ""); } var r1 = int.Parse(firstColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); var g1 = int.Parse(firstColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); var b1 = int.Parse(firstColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); if (secondColor.IndexOf("#", StringComparison.Ordinal) != -1) { secondColor = secondColor.Replace("#", ""); } var r2 = int.Parse(secondColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); var g2 = int.Parse(secondColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); var b2 = int.Parse(secondColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); var result = ColorFormulas.DoFullCompare(r1, g1, b1, r2, g2, b2); return(result); }