ColorPoint.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Drawing;
  3. namespace shjxCamera
  4. {
  5. public class ColorPoint : IComparable<ColorPoint>
  6. {
  7. public string Tag { get; set; }
  8. public int R { get; set; }
  9. public int G { get; set; }
  10. public int B { get; set; }
  11. public int Gray { get; set; }
  12. public int Hue { get; set; }
  13. public float Saturation { get; set; }
  14. public float Brightness { get; set; }
  15. // 计算色相差
  16. public static int HueDiff(ColorPoint cp1, ColorPoint cp2)
  17. {
  18. int a = cp1.Hue;
  19. int b = cp2.Hue;
  20. if (a >= b)
  21. {
  22. return Math.Min(a - b, b - a + 360);
  23. }
  24. else
  25. {
  26. return Math.Min(b - a, a - b + 360);
  27. }
  28. }
  29. public ColorPoint(int r, int g, int b, string tag)
  30. {
  31. Tag = tag;
  32. R = r;
  33. G = g;
  34. B = b;
  35. Gray = Math.Min(255, (int)(R * 0.299 + G * 0.587 + B * 0.114));
  36. Color color = Color.FromArgb(R, G, B);
  37. Hue = (int)color.GetHue();
  38. Saturation = color.GetSaturation();
  39. Brightness = color.GetBrightness();
  40. }
  41. public int CompareTo(ColorPoint obj)
  42. {
  43. return Hue - obj.Hue;
  44. }
  45. }
  46. }