EuclideanColorDistance.cs 965 B

1234567891011121314151617181920212223242526272829
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// <para>
  4. /// Calculates the distance between two colors using Euclidean distance in 3D RGB space.
  5. /// This measures the straight-line distance between the two points representing the colors.
  6. ///</para>
  7. /// <para>
  8. /// Euclidean distance in RGB space is calculated as:
  9. /// </para>
  10. /// <code>
  11. /// √((R2 - R1)² + (G2 - G1)² + (B2 - B1)²)
  12. /// </code>
  13. /// <remarks>Values vary from 0 to ~441.67 linearly</remarks>
  14. ///
  15. /// <remarks>This distance metric is commonly used for comparing colors in RGB space, though
  16. /// it doesn't account for perceptual differences in color.</remarks>
  17. /// </summary>
  18. public class EuclideanColorDistance : IColorDistance
  19. {
  20. /// <inheritdoc/>
  21. public double CalculateDistance (Color c1, Color c2)
  22. {
  23. int rDiff = c1.R - c2.R;
  24. int gDiff = c1.G - c2.G;
  25. int bDiff = c1.B - c2.B;
  26. return Math.Sqrt (rDiff * rDiff + gDiff * gDiff + bDiff * bDiff);
  27. }
  28. }