RgbColor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SmartCoalApplication.Core
  8. {
  9. /// <summary>
  10. /// Adapted from:
  11. /// "A Primer on Building a Color Picker User Control with GDI+ in Visual Basic .NET or C#"
  12. /// http://www.msdnaa.net/Resources/display.aspx?ResID=2460
  13. ///
  14. /// This class is only used by the ColorsForm and ColorWheel. Nothing else in this program
  15. /// should be using it!
  16. /// </summary>
  17. [Serializable]
  18. public struct RgbColor
  19. {
  20. // All values are between 0 and 255.
  21. public int Red;
  22. public int Green;
  23. public int Blue;
  24. public RgbColor(int R, int G, int B)
  25. {
  26. #if DEBUG
  27. if (R < 0 || R > 255)
  28. {
  29. throw new ArgumentOutOfRangeException("R", R, "R must corrospond to a byte value");
  30. }
  31. if (G < 0 || G > 255)
  32. {
  33. throw new ArgumentOutOfRangeException("G", G, "G must corrospond to a byte value");
  34. }
  35. if (B < 0 || B > 255)
  36. {
  37. throw new ArgumentOutOfRangeException("B", B, "B must corrospond to a byte value");
  38. }
  39. #endif
  40. Red = R;
  41. Green = G;
  42. Blue = B;
  43. }
  44. public static RgbColor FromHsv(HsvColor hsv)
  45. {
  46. return hsv.ToRgb();
  47. }
  48. public Color ToColor()
  49. {
  50. return Color.FromArgb(Red, Green, Blue);
  51. }
  52. public HsvColor ToHsv()
  53. {
  54. // In this function, R, G, and B values must be scaled
  55. // to be between 0 and 1.
  56. // HsvColor.Hue will be a value between 0 and 360, and
  57. // HsvColor.Saturation and value are between 0 and 1.
  58. double min;
  59. double max;
  60. double delta;
  61. double r = (double)Red / 255;
  62. double g = (double)Green / 255;
  63. double b = (double)Blue / 255;
  64. double h;
  65. double s;
  66. double v;
  67. min = Math.Min(Math.Min(r, g), b);
  68. max = Math.Max(Math.Max(r, g), b);
  69. v = max;
  70. delta = max - min;
  71. if (max == 0 || delta == 0)
  72. {
  73. // R, G, and B must be 0, or all the same.
  74. // In this case, S is 0, and H is undefined.
  75. // Using H = 0 is as good as any...
  76. s = 0;
  77. h = 0;
  78. }
  79. else
  80. {
  81. s = delta / max;
  82. if (r == max)
  83. {
  84. // Between Yellow and Magenta
  85. h = (g - b) / delta;
  86. }
  87. else if (g == max)
  88. {
  89. // Between Cyan and Yellow
  90. h = 2 + (b - r) / delta;
  91. }
  92. else
  93. {
  94. // Between Magenta and Cyan
  95. h = 4 + (r - g) / delta;
  96. }
  97. }
  98. // Scale h to be between 0 and 360.
  99. // This may require adding 360, if the value
  100. // is negative.
  101. h *= 60;
  102. if (h < 0)
  103. {
  104. h += 360;
  105. }
  106. // Scale to the requirements of this
  107. // application. All values are between 0 and 255.
  108. return new HsvColor((int)h, (int)(s * 100), (int)(v * 100));
  109. }
  110. public override string ToString()
  111. {
  112. return String.Format("({0}, {1}, {2})", Red, Green, Blue);
  113. }
  114. }
  115. }