Color.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace BansheeEngine
  4. {
  5. [StructLayout(LayoutKind.Sequential), SerializeObject]
  6. public struct Color
  7. {
  8. public float r;
  9. public float g;
  10. public float b;
  11. public float a;
  12. public static Color red
  13. {
  14. get
  15. {
  16. return new Color(1.0f, 0.0f, 0.0f, 1.0f);
  17. }
  18. }
  19. public static Color green
  20. {
  21. get
  22. {
  23. return new Color(0.0f, 1.0f, 0.0f, 1.0f);
  24. }
  25. }
  26. public static Color blue
  27. {
  28. get
  29. {
  30. return new Color(0.0f, 0.0f, 1.0f, 1.0f);
  31. }
  32. }
  33. public static Color white
  34. {
  35. get
  36. {
  37. return new Color(1.0f, 1.0f, 1.0f, 1.0f);
  38. }
  39. }
  40. public static Color black
  41. {
  42. get
  43. {
  44. return new Color(0.0f, 0.0f, 0.0f, 1.0f);
  45. }
  46. }
  47. public float this[int index]
  48. {
  49. get
  50. {
  51. switch (index)
  52. {
  53. case 0:
  54. return r;
  55. case 1:
  56. return g;
  57. case 2:
  58. return b;
  59. case 3:
  60. return a;
  61. default:
  62. throw new IndexOutOfRangeException("Invalid Color index.");
  63. }
  64. }
  65. set
  66. {
  67. switch (index)
  68. {
  69. case 0:
  70. r = value;
  71. break;
  72. case 1:
  73. g = value;
  74. break;
  75. case 2:
  76. b = value;
  77. break;
  78. case 3:
  79. a = value;
  80. break;
  81. default:
  82. throw new IndexOutOfRangeException("Invalid Color index.");
  83. }
  84. }
  85. }
  86. public Color(float r, float g, float b, float a)
  87. {
  88. this.r = r;
  89. this.g = g;
  90. this.b = b;
  91. this.a = a;
  92. }
  93. public Color(float r, float g, float b)
  94. {
  95. this.r = r;
  96. this.g = g;
  97. this.b = b;
  98. this.a = 1f;
  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, 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, float b)
  113. {
  114. return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
  115. }
  116. public static Color operator* (float b, Color a)
  117. {
  118. return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
  119. }
  120. public static Color operator/ (Color a, float b)
  121. {
  122. return new Color(a.r / b, a.g / b, a.b / b, a.a / b);
  123. }
  124. public static bool operator ==(Color lhs, Color rhs)
  125. {
  126. return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b && lhs.a == rhs.a;
  127. }
  128. public static bool operator !=(Color lhs, Color rhs)
  129. {
  130. return !(lhs == rhs);
  131. }
  132. public override int GetHashCode()
  133. {
  134. return r.GetHashCode() ^ g.GetHashCode() << 2 ^ b.GetHashCode() >> 2 ^ a.GetHashCode() >> 1;
  135. }
  136. public override bool Equals(object other)
  137. {
  138. if (!(other is Color))
  139. return false;
  140. Color color = (Color)other;
  141. if (r.Equals(color.r) && g.Equals(color.g) && b.Equals(color.b) && a.Equals(color.a))
  142. return true;
  143. return false;
  144. }
  145. }
  146. }