Color.cs 9.3 KB

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