Color.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /// <summary>
  34. /// Accesses color components by an index.
  35. /// </summary>
  36. /// <param name="index">Index ranging [0, 3]</param>
  37. /// <returns>Value of the component at the specified index.</returns>
  38. public float this[int index]
  39. {
  40. get
  41. {
  42. switch (index)
  43. {
  44. case 0:
  45. return r;
  46. case 1:
  47. return g;
  48. case 2:
  49. return b;
  50. case 3:
  51. return a;
  52. default:
  53. throw new IndexOutOfRangeException("Invalid Color index.");
  54. }
  55. }
  56. set
  57. {
  58. switch (index)
  59. {
  60. case 0:
  61. r = value;
  62. break;
  63. case 1:
  64. g = value;
  65. break;
  66. case 2:
  67. b = value;
  68. break;
  69. case 3:
  70. a = value;
  71. break;
  72. default:
  73. throw new IndexOutOfRangeException("Invalid Color index.");
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// Creates a new color value.
  79. /// </summary>
  80. /// <param name="r">Red component, in range [0, 1].</param>
  81. /// <param name="g">Green component, in range [0, 1].</param>
  82. /// <param name="b">Blue component, in range [0, 1].</param>
  83. /// <param name="a">Alpha component, in range [0, 1].</param>
  84. public Color(float r, float g, float b, float a)
  85. {
  86. this.r = r;
  87. this.g = g;
  88. this.b = b;
  89. this.a = a;
  90. }
  91. /// <summary>
  92. /// Creates a new color value. Alpha is assumed to be 1 (non-transparent).
  93. /// </summary>
  94. /// <param name="r">Red component, in range [0, 1].</param>
  95. /// <param name="g">Green component, in range [0, 1].</param>
  96. /// <param name="b">Blue component, in range [0, 1].</param>
  97. public Color(float r, float g, float b)
  98. {
  99. this.r = r;
  100. this.g = g;
  101. this.b = b;
  102. this.a = 1f;
  103. }
  104. public static Color operator+ (Color a, Color b)
  105. {
  106. return new Color(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a);
  107. }
  108. public static Color operator- (Color a, Color b)
  109. {
  110. return new Color(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a);
  111. }
  112. public static Color operator* (Color a, Color b)
  113. {
  114. return new Color(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a);
  115. }
  116. public static Color operator* (Color a, float b)
  117. {
  118. return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
  119. }
  120. public static Color operator* (float b, Color a)
  121. {
  122. return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
  123. }
  124. public static Color operator/ (Color a, float b)
  125. {
  126. return new Color(a.r / b, a.g / b, a.b / b, a.a / b);
  127. }
  128. public static bool operator ==(Color lhs, Color rhs)
  129. {
  130. return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b && lhs.a == rhs.a;
  131. }
  132. public static bool operator !=(Color lhs, Color rhs)
  133. {
  134. return !(lhs == rhs);
  135. }
  136. /// <summary>
  137. /// Converts the provided RGB color into HSV color space.
  138. /// </summary>
  139. /// <param name="input">Color in RGB color space.</param>
  140. /// <returns>Color in HSV color space.</returns>
  141. public static Color RGB2HSV(Color input)
  142. {
  143. Color output = input;
  144. float min = input.r < input.g ? input.r : input.g;
  145. min = min < input.b ? min : input.b;
  146. float max = input.r > input.g ? input.r : input.g;
  147. max = max > input.b ? max : input.b;
  148. output.b = max;
  149. if (max == 0.0f)
  150. {
  151. output.r = 0.0f;
  152. output.g = 0.0f;
  153. return output;
  154. }
  155. float delta = max - min;
  156. if (delta != 0.0f)
  157. {
  158. output.g = (delta / max);
  159. }
  160. else
  161. {
  162. output.g = 0.0f;
  163. delta = 1.0f;
  164. }
  165. if (input.r >= max)
  166. output.r = (input.g - input.b) / delta;
  167. else
  168. {
  169. if (input.g >= max)
  170. output.r = 2.0f + (input.b - input.r) / delta;
  171. else
  172. output.r = 4.0f + (input.r - input.g) / delta;
  173. }
  174. output.r /= 6.0f;
  175. if (output.r < 0.0f)
  176. output.r += 1.0f;
  177. return output;
  178. }
  179. /// <summary>
  180. /// Converts the provided HSV color into RGB color space.
  181. /// </summary>
  182. /// <param name="input">Color in HSV color space.</param>
  183. /// <returns>Color in RGB color space.</returns>
  184. public static Color HSV2RGB(Color input)
  185. {
  186. Color output = input;
  187. if (input.g <= 0.0)
  188. {
  189. output.r = input.b;
  190. output.g = input.b;
  191. output.b = input.b;
  192. return output;
  193. }
  194. float hh = input.r;
  195. if (hh >= 1.0f)
  196. hh = 0.0f;
  197. hh *= 6.0f;
  198. int i = (int)hh;
  199. float ff = hh - i;
  200. float p = input.b * (1.0f - input.g);
  201. float q = input.b * (1.0f - (input.g * ff));
  202. float t = input.b * (1.0f - (input.g * (1.0f - ff)));
  203. switch (i)
  204. {
  205. case 0:
  206. output.r = input.b;
  207. output.g = t;
  208. output.b = p;
  209. break;
  210. case 1:
  211. output.r = q;
  212. output.g = input.b;
  213. output.b = p;
  214. break;
  215. case 2:
  216. output.r = p;
  217. output.g = input.b;
  218. output.b = t;
  219. break;
  220. case 3:
  221. output.r = p;
  222. output.g = q;
  223. output.b = input.b;
  224. break;
  225. case 4:
  226. output.r = t;
  227. output.g = p;
  228. output.b = input.b;
  229. break;
  230. default:
  231. output.r = input.b;
  232. output.g = p;
  233. output.b = q;
  234. break;
  235. }
  236. return output;
  237. }
  238. /// <inheritdoc/>
  239. public override int GetHashCode()
  240. {
  241. return r.GetHashCode() ^ g.GetHashCode() << 2 ^ b.GetHashCode() >> 2 ^ a.GetHashCode() >> 1;
  242. }
  243. /// <inheritdoc/>
  244. public override bool Equals(object other)
  245. {
  246. if (!(other is Color))
  247. return false;
  248. Color color = (Color)other;
  249. if (r.Equals(color.r) && g.Equals(color.g) && b.Equals(color.b) && a.Equals(color.a))
  250. return true;
  251. return false;
  252. }
  253. }
  254. /** @} */
  255. }