From fd2e9a004968f622d8219d3768c631ca008ef2e9 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sat, 5 Sep 2020 23:47:05 +0200 Subject: [PATCH] Added color-extension to calculate the distance between two colors --- RGB.NET.Core/Extensions/ColorExtensions.cs | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 RGB.NET.Core/Extensions/ColorExtensions.cs 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 + } +}