ColorDataPoint.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Drawing;
  3. namespace shjxCamera
  4. {
  5. public class ColorDataPoint
  6. {
  7. #region protected
  8. /// <summary>
  9. /// 单点颜色
  10. /// </summary>
  11. protected Color PointColor { get; set; }
  12. protected void UpdatePoint()
  13. {
  14. PointColor = Color.FromArgb(Convert.ToInt32(RTV / Area), Convert.ToInt32(GTV / Area), Convert.ToInt32(BTV / Area));
  15. }
  16. #endregion
  17. #region properties
  18. /// <summary>
  19. /// 序号
  20. /// </summary>
  21. public int Id { get; set; }
  22. /// <summary>
  23. /// 区域R总量
  24. /// </summary>
  25. public double RTV { get; set; }
  26. /// <summary>
  27. /// 区域G总量
  28. /// </summary>
  29. public double GTV { get; set; }
  30. /// <summary>
  31. /// 区域B总量
  32. /// </summary>
  33. public double BTV { get; set; }
  34. /// <summary>
  35. /// 区域面积
  36. /// </summary>
  37. public double Area { get; set; }
  38. /// <summary>
  39. /// 小数点后位数
  40. /// </summary>
  41. public int FloatCount { get; set; }
  42. #endregion
  43. #region methods
  44. public ColorDataPoint(int id = 1, double rtv = 1, double gtv = 1, double btv = 1, double area = 1, int floatcount = 2)
  45. {
  46. Id = id;
  47. RTV = rtv;
  48. GTV = gtv;
  49. BTV = btv;
  50. Area = area;
  51. FloatCount = floatcount;
  52. UpdatePoint();
  53. }
  54. /// <summary>
  55. /// 返回单点R平均值
  56. /// </summary>
  57. /// <returns></returns>
  58. public double GetRSV()
  59. {
  60. double ret = Math.Round(RTV / Area, FloatCount);
  61. return ret;
  62. }
  63. /// <summary>
  64. /// 返回单点G平均值
  65. /// </summary>
  66. /// <returns></returns>
  67. public double GetGSV()
  68. {
  69. double ret = Math.Round(GTV / Area, FloatCount);
  70. return ret;
  71. }
  72. /// <summary>
  73. /// 返回单点B平均值
  74. /// </summary>
  75. /// <returns></returns>
  76. public double GetBSV()
  77. {
  78. double ret = Math.Round(BTV / Area, FloatCount);
  79. return ret;
  80. }
  81. /// <summary>
  82. /// 返回HSB色调值
  83. /// </summary>
  84. /// <returns></returns>
  85. public double GetHue()
  86. {
  87. UpdatePoint();
  88. return Math.Round(PointColor.GetHue(), FloatCount);
  89. }
  90. /// <summary>
  91. /// 返回HSB饱和度值
  92. /// </summary>
  93. /// <returns></returns>
  94. public double GetSaturation()
  95. {
  96. return 1.0;
  97. #if false
  98. #region MyRegion
  99. #if true
  100. UpdatePoint();
  101. return Math.Round(this.PointColor.GetSaturation(), FloatCount);
  102. #endif
  103. #region MyRegion
  104. this.UpdatePoint();
  105. var vmax = Math.Max(this.GTV, Math.Max(this.RTV, this.BTV)) / this.Area;
  106. var vmin = Math.Min(this.GTV, Math.Min(this.RTV, this.BTV)) / this.Area;
  107. var sat = Math.Max(1.0, Math.Round((vmax - vmin) / vmax * 100.0, 2));
  108. return sat;
  109. #endregion
  110. #endregion
  111. #endif
  112. }
  113. /// <summary>
  114. /// 返回HSB亮度值
  115. /// </summary>
  116. /// <returns></returns>
  117. public double GetBrightness()
  118. {
  119. UpdatePoint();
  120. return Math.Round(PointColor.GetBrightness(), FloatCount);
  121. }
  122. #endregion
  123. }
  124. }