Color.cs 8.8 KB

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