2
0

Color.cs 9.2 KB

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