diff --git a/RGB.NET.Core/Extensions/ColorExtensions.cs b/RGB.NET.Core/Extensions/ColorExtensions.cs new file mode 100644 index 0000000..15d0c80 --- /dev/null +++ b/RGB.NET.Core/Extensions/ColorExtensions.cs @@ -0,0 +1,30 @@ +using System; + +namespace RGB.NET.Core +{ + public static class ColorExtensions + { + #region Methods + + /// + /// Calculates the distance between the two given colors using the redmean algorithm. + /// For more infos check https://www.compuphase.com/cmetric.htm + /// + /// The start color of the distance calculation. + /// The end color fot the distance calculation. + /// + public static double DistanceTo(this Color color1, Color color2) + { + (_, byte r1, byte g1, byte b1) = color1.GetRGBBytes(); + (_, byte r2, byte g2, byte b2) = color2.GetRGBBytes(); + + long rmean = (r1 + r2) / 2; + long r = r1 - r2; + long g = g1 - g2; + long b = b1 - b2; + return Math.Sqrt((((512 + rmean) * r * r) >> 8) + (4 * g * g) + (((767 - rmean) * b * b) >> 8)); + } + + #endregion + } +}