HSVColor.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using UnityEngine;
  3. namespace wf
  4. {
  5. public struct HSVColor
  6. {
  7. public HSVColor(Color color)
  8. {
  9. int num = (int)Mathf.Floor(255f * color.r);
  10. int num2 = (int)Mathf.Floor(255f * color.g);
  11. int num3 = (int)Mathf.Floor(255f * color.b);
  12. int num4 = Math.Max(Math.Max(num, num2), num3);
  13. int num5 = Math.Min(Math.Min(num, num2), num3);
  14. this.h = 0f;
  15. if (num4 == num5)
  16. {
  17. this.h = 0f;
  18. }
  19. else if (num4 == num)
  20. {
  21. this.h = (60f * (float)(num2 - num3) / (float)(num4 - num5) + 360f) % 360f;
  22. }
  23. else if (num4 == num2)
  24. {
  25. this.h = (float)(60 * (num3 - num) / (num4 - num5) + 120);
  26. }
  27. else if (num4 == num3)
  28. {
  29. this.h = (float)(60 * (num - num2) / (num4 - num5) + 240);
  30. }
  31. this.s = ((num4 != 0) ? ((float)(num4 - num5) / (float)num4) : 0f);
  32. this.v = (float)num4 / 255f;
  33. }
  34. public Color ToColor()
  35. {
  36. float num = Mathf.Max(0f, this.h);
  37. float num2 = Mathf.Clamp(this.s, 0f, 1f);
  38. float num3 = Mathf.Clamp(this.v, 0f, 1f);
  39. if (num2 == 0f)
  40. {
  41. return new Color(num3, num3, num3);
  42. }
  43. float r = 0f;
  44. float g = 0f;
  45. float b = 0f;
  46. float num4 = num % 360f / 60f;
  47. int num5 = (int)Math.Floor((double)num4);
  48. float num6 = num4 - (float)num5;
  49. float num7 = num3 * (1f - num2);
  50. float num8;
  51. if (num5 % 2 == 0)
  52. {
  53. num8 = num3 * (1f - (1f - num6) * num2);
  54. }
  55. else
  56. {
  57. num8 = num3 * (1f - num6 * num2);
  58. }
  59. switch (num5)
  60. {
  61. case 0:
  62. r = num3;
  63. g = num8;
  64. b = num7;
  65. break;
  66. case 1:
  67. r = num8;
  68. g = num3;
  69. b = num7;
  70. break;
  71. case 2:
  72. r = num7;
  73. g = num3;
  74. b = num8;
  75. break;
  76. case 3:
  77. r = num7;
  78. g = num8;
  79. b = num3;
  80. break;
  81. case 4:
  82. r = num8;
  83. g = num7;
  84. b = num3;
  85. break;
  86. case 5:
  87. r = num3;
  88. g = num7;
  89. b = num8;
  90. break;
  91. }
  92. return new Color(r, g, b);
  93. }
  94. public float h;
  95. public float s;
  96. public float v;
  97. }
  98. }