Color.cs 9.5 KB

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