Color.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Utility
  8. * @{
  9. */
  10. /// <summary>
  11. /// Contains a three-component color with an alpha component.
  12. /// </summary>
  13. [StructLayout(LayoutKind.Sequential), SerializeObject]
  14. public struct Color // Note: Must match C++ class Color
  15. {
  16. public float r;
  17. public float g;
  18. public float b;
  19. public float a;
  20. public static Color Red { get { return new Color(1.0f, 0.0f, 0.0f, 1.0f); } }
  21. public static Color Green { get { return new Color(0.0f, 1.0f, 0.0f, 1.0f); } }
  22. public static Color Blue { get { return new Color(0.0f, 0.0f, 1.0f, 1.0f); } }
  23. public static Color Yellow { get { return new Color(1.0f, 1.0f, 0.0f, 1.0f); } }
  24. public static Color Cyan { get { return new Color(0.0f, 1.0f, 1.0f, 1.0f); } }
  25. public static Color Magenta { get { return new Color(1.0f, 0.0f, 1.0f, 1.0f); } }
  26. public static Color White { get { return new Color(1.0f, 1.0f, 1.0f, 1.0f); } }
  27. public static Color Black { get { return new Color(0.0f, 0.0f, 0.0f, 1.0f); } }
  28. public static Color DarkCyan { get { return new Color(0.0f, (114.0f / 255.0f), (188.0f / 255.0f), 1.0f); } }
  29. public static Color VeryDarkGray { get { return new Color(23.0f / 255.0f, 23.0f / 255.0f, 23.0f / 255.0f, 1.0f); } }
  30. public static Color DarkGray { get { return new Color(63.0f / 255.0f, 63.0f / 255.0f, 63.0f / 255.0f, 1.0f); } }
  31. public static Color LightGray { get { return new Color(200.0f / 255.0f, 200.0f / 255.0f, 200.0f / 255.0f, 1.0f); } }
  32. public static Color BansheeOrange { get { return new Color(1.0f, (168.0f/255.0f), 0.0f, 1.0f); } }
  33. public static Color Transparent { get { return new Color(0.0f, 0.0f, 0.0f, 0.0f); } }
  34. /// <summary>
  35. /// Accesses color components by an index.
  36. /// </summary>
  37. /// <param name="index">Index ranging [0, 3]</param>
  38. /// <returns>Value of the component at the specified index.</returns>
  39. public float this[int index]
  40. {
  41. get
  42. {
  43. switch (index)
  44. {
  45. case 0:
  46. return r;
  47. case 1:
  48. return g;
  49. case 2:
  50. return b;
  51. case 3:
  52. return a;
  53. default:
  54. throw new IndexOutOfRangeException("Invalid Color index.");
  55. }
  56. }
  57. set
  58. {
  59. switch (index)
  60. {
  61. case 0:
  62. r = value;
  63. break;
  64. case 1:
  65. g = value;
  66. break;
  67. case 2:
  68. b = value;
  69. break;
  70. case 3:
  71. a = value;
  72. break;
  73. default:
  74. throw new IndexOutOfRangeException("Invalid Color index.");
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// Creates a new color value.
  80. /// </summary>
  81. /// <param name="r">Red component, in range [0, 1].</param>
  82. /// <param name="g">Green component, in range [0, 1].</param>
  83. /// <param name="b">Blue component, in range [0, 1].</param>
  84. /// <param name="a">Alpha component, in range [0, 1].</param>
  85. public Color(float r, float g, float b, float a)
  86. {
  87. this.r = r;
  88. this.g = g;
  89. this.b = b;
  90. this.a = a;
  91. }
  92. /// <summary>
  93. /// Creates a new color value. Alpha is assumed to be 1 (non-transparent).
  94. /// </summary>
  95. /// <param name="r">Red component, in range [0, 1].</param>
  96. /// <param name="g">Green component, in range [0, 1].</param>
  97. /// <param name="b">Blue component, in range [0, 1].</param>
  98. public Color(float r, float g, float b)
  99. {
  100. this.r = r;
  101. this.g = g;
  102. this.b = b;
  103. this.a = 1f;
  104. }
  105. public static Color operator+ (Color a, Color b)
  106. {
  107. return new Color(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a);
  108. }
  109. public static Color operator- (Color a, Color b)
  110. {
  111. return new Color(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a);
  112. }
  113. public static Color operator* (Color a, Color b)
  114. {
  115. return new Color(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a);
  116. }
  117. public static Color operator* (Color a, float b)
  118. {
  119. return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
  120. }
  121. public static Color operator* (float b, Color a)
  122. {
  123. return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
  124. }
  125. public static Color operator/ (Color a, float b)
  126. {
  127. return new Color(a.r / b, a.g / b, a.b / b, a.a / b);
  128. }
  129. public static bool operator ==(Color lhs, Color rhs)
  130. {
  131. return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b && lhs.a == rhs.a;
  132. }
  133. public static bool operator !=(Color lhs, Color rhs)
  134. {
  135. return !(lhs == rhs);
  136. }
  137. /// <summary>
  138. /// Converts the provided RGB color into HSV color space.
  139. /// </summary>
  140. /// <param name="input">Color in RGB color space.</param>
  141. /// <returns>Color in HSV color space.</returns>
  142. public static Color RGB2HSV(Color input)
  143. {
  144. Color output = input;
  145. float min = input.r < input.g ? input.r : input.g;
  146. min = min < input.b ? min : input.b;
  147. float max = input.r > input.g ? input.r : input.g;
  148. max = max > input.b ? max : input.b;
  149. output.b = max;
  150. if (max == 0.0f)
  151. {
  152. output.r = 0.0f;
  153. output.g = 0.0f;
  154. return output;
  155. }
  156. float delta = max - min;
  157. if (delta != 0.0f)
  158. {
  159. output.g = (delta / max);
  160. }
  161. else
  162. {
  163. output.g = 0.0f;
  164. delta = 1.0f;
  165. }
  166. if (input.r >= max)
  167. output.r = (input.g - input.b) / delta;
  168. else
  169. {
  170. if (input.g >= max)
  171. output.r = 2.0f + (input.b - input.r) / delta;
  172. else
  173. output.r = 4.0f + (input.r - input.g) / delta;
  174. }
  175. output.r /= 6.0f;
  176. if (output.r < 0.0f)
  177. output.r += 1.0f;
  178. return output;
  179. }
  180. /// <summary>
  181. /// Converts the provided HSV color into RGB color space.
  182. /// </summary>
  183. /// <param name="input">Color in HSV color space.</param>
  184. /// <returns>Color in RGB color space.</returns>
  185. public static Color HSV2RGB(Color input)
  186. {
  187. Color output = input;
  188. if (input.g <= 0.0)
  189. {
  190. output.r = input.b;
  191. output.g = input.b;
  192. output.b = input.b;
  193. return output;
  194. }
  195. float hh = input.r;
  196. if (hh >= 1.0f)
  197. hh = 0.0f;
  198. hh *= 6.0f;
  199. int i = (int)hh;
  200. float ff = hh - i;
  201. float p = input.b * (1.0f - input.g);
  202. float q = input.b * (1.0f - (input.g * ff));
  203. float t = input.b * (1.0f - (input.g * (1.0f - ff)));
  204. switch (i)
  205. {
  206. case 0:
  207. output.r = input.b;
  208. output.g = t;
  209. output.b = p;
  210. break;
  211. case 1:
  212. output.r = q;
  213. output.g = input.b;
  214. output.b = p;
  215. break;
  216. case 2:
  217. output.r = p;
  218. output.g = input.b;
  219. output.b = t;
  220. break;
  221. case 3:
  222. output.r = p;
  223. output.g = q;
  224. output.b = input.b;
  225. break;
  226. case 4:
  227. output.r = t;
  228. output.g = p;
  229. output.b = input.b;
  230. break;
  231. default:
  232. output.r = input.b;
  233. output.g = p;
  234. output.b = q;
  235. break;
  236. }
  237. return output;
  238. }
  239. /// <inheritdoc/>
  240. public override int GetHashCode()
  241. {
  242. return r.GetHashCode() ^ g.GetHashCode() << 2 ^ b.GetHashCode() >> 2 ^ a.GetHashCode() >> 1;
  243. }
  244. /// <inheritdoc/>
  245. public override bool Equals(object other)
  246. {
  247. if (!(other is Color))
  248. return false;
  249. Color color = (Color)other;
  250. if (r.Equals(color.r) && g.Equals(color.g) && b.Equals(color.b) && a.Equals(color.a))
  251. return true;
  252. return false;
  253. }
  254. }
  255. /** @} */
  256. }