2
0

Color.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace BansheeEngine
  4. {
  5. [StructLayout(LayoutKind.Sequential), SerializeObject]
  6. public struct Color
  7. {
  8. public float r;
  9. public float g;
  10. public float b;
  11. public float a;
  12. public static Color Red { get { return new Color(1.0f, 0.0f, 0.0f, 1.0f); } }
  13. public static Color Green { get { return new Color(0.0f, 1.0f, 0.0f, 1.0f); } }
  14. public static Color Blue { get { return new Color(0.0f, 0.0f, 1.0f, 1.0f); } }
  15. public static Color Yellow { get { return new Color(1.0f, 1.0f, 0.0f, 1.0f); } }
  16. public static Color Cyan { get { return new Color(0.0f, 1.0f, 1.0f, 1.0f); } }
  17. public static Color Magenta { get { return new Color(1.0f, 0.0f, 1.0f, 1.0f); } }
  18. public static Color White { get { return new Color(1.0f, 1.0f, 1.0f, 1.0f); } }
  19. public static Color Black { get { return new Color(0.0f, 0.0f, 0.0f, 1.0f); } }
  20. public static Color DarkCyan { get { return new Color(0.0f, (114.0f / 255.0f), (188.0f / 255.0f), 1.0f); } }
  21. public static Color BansheeOrange { get { return new Color(1.0f, (168.0f/255.0f), 0.0f, 1.0f); } }
  22. public float this[int index]
  23. {
  24. get
  25. {
  26. switch (index)
  27. {
  28. case 0:
  29. return r;
  30. case 1:
  31. return g;
  32. case 2:
  33. return b;
  34. case 3:
  35. return a;
  36. default:
  37. throw new IndexOutOfRangeException("Invalid Color index.");
  38. }
  39. }
  40. set
  41. {
  42. switch (index)
  43. {
  44. case 0:
  45. r = value;
  46. break;
  47. case 1:
  48. g = value;
  49. break;
  50. case 2:
  51. b = value;
  52. break;
  53. case 3:
  54. a = value;
  55. break;
  56. default:
  57. throw new IndexOutOfRangeException("Invalid Color index.");
  58. }
  59. }
  60. }
  61. public Color(float r, float g, float b, float a)
  62. {
  63. this.r = r;
  64. this.g = g;
  65. this.b = b;
  66. this.a = a;
  67. }
  68. public Color(float r, float g, float b)
  69. {
  70. this.r = r;
  71. this.g = g;
  72. this.b = b;
  73. this.a = 1f;
  74. }
  75. public static Color operator+ (Color a, Color b)
  76. {
  77. return new Color(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a);
  78. }
  79. public static Color operator- (Color a, Color b)
  80. {
  81. return new Color(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a);
  82. }
  83. public static Color operator* (Color a, Color b)
  84. {
  85. return new Color(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a);
  86. }
  87. public static Color operator* (Color a, float b)
  88. {
  89. return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
  90. }
  91. public static Color operator* (float b, Color a)
  92. {
  93. return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
  94. }
  95. public static Color operator/ (Color a, float b)
  96. {
  97. return new Color(a.r / b, a.g / b, a.b / b, a.a / b);
  98. }
  99. public static bool operator ==(Color lhs, Color rhs)
  100. {
  101. return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b && lhs.a == rhs.a;
  102. }
  103. public static bool operator !=(Color lhs, Color rhs)
  104. {
  105. return !(lhs == rhs);
  106. }
  107. public static Color RGB2HSV(Color input)
  108. {
  109. Color output = input;
  110. float min = input.r < input.g ? input.r : input.g;
  111. min = min < input.b ? min : input.b;
  112. float max = input.r > input.g ? input.r : input.g;
  113. max = max > input.b ? max : input.b;
  114. output.b = max;
  115. float delta = max - min;
  116. if (max > 0.0f)
  117. output.g = (delta / max);
  118. else
  119. {
  120. output.g = 0.0f;
  121. output.r = 0.0f;
  122. return output;
  123. }
  124. if (input.r >= max)
  125. output.r = (input.g - input.b) / delta;
  126. else
  127. {
  128. if (input.g >= max)
  129. output.r = 2.0f + (input.b - input.r) / delta;
  130. else
  131. output.r = 4.0f + (input.r - input.g) / delta;
  132. }
  133. output.r /= 6.0f;
  134. if (output.r < 0.0f)
  135. output.r += 1.0f;
  136. return output;
  137. }
  138. public static Color HSV2RGB(Color input)
  139. {
  140. Color output = input;
  141. if (input.g <= 0.0)
  142. {
  143. output.r = input.b;
  144. output.g = input.b;
  145. output.b = input.b;
  146. return output;
  147. }
  148. float hh = input.r;
  149. if (hh >= 1.0f)
  150. hh = 0.0f;
  151. hh *= 6.0f;
  152. int i = (int)hh;
  153. float ff = hh - i;
  154. float p = input.b * (1.0f - input.g);
  155. float q = input.b * (1.0f - (input.g * ff));
  156. float t = input.b * (1.0f - (input.g * (1.0f - ff)));
  157. switch (i)
  158. {
  159. case 0:
  160. output.r = input.b;
  161. output.g = t;
  162. output.b = p;
  163. break;
  164. case 1:
  165. output.r = q;
  166. output.g = input.b;
  167. output.b = p;
  168. break;
  169. case 2:
  170. output.r = p;
  171. output.g = input.b;
  172. output.b = t;
  173. break;
  174. case 3:
  175. output.r = p;
  176. output.g = q;
  177. output.b = input.b;
  178. break;
  179. case 4:
  180. output.r = t;
  181. output.g = p;
  182. output.b = input.b;
  183. break;
  184. default:
  185. output.r = input.b;
  186. output.g = p;
  187. output.b = q;
  188. break;
  189. }
  190. return output;
  191. }
  192. public override int GetHashCode()
  193. {
  194. return r.GetHashCode() ^ g.GetHashCode() << 2 ^ b.GetHashCode() >> 2 ^ a.GetHashCode() >> 1;
  195. }
  196. public override bool Equals(object other)
  197. {
  198. if (!(other is Color))
  199. return false;
  200. Color color = (Color)other;
  201. if (r.Equals(color.r) && g.Equals(color.g) && b.Equals(color.b) && a.Equals(color.a))
  202. return true;
  203. return false;
  204. }
  205. }
  206. }