Color.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 { get { return new Color(1.0f, 0.0f, 0.0f, 1.0f); } }
  13. public static Color Green { get { return new Color(0.0f, 1.0f, 0.0f, 1.0f); } }
  14. public static Color Blue { get { return new Color(0.0f, 0.0f, 1.0f, 1.0f); } }
  15. public static Color Yellow { get { return new Color(1.0f, 1.0f, 0.0f, 1.0f); } }
  16. public static Color Cyan { get { return new Color(0.0f, 1.0f, 1.0f, 1.0f); } }
  17. public static Color Magenta { get { return new Color(1.0f, 0.0f, 1.0f, 1.0f); } }
  18. public static Color White { get { return new Color(1.0f, 1.0f, 1.0f, 1.0f); } }
  19. public static Color Black { get { return new Color(0.0f, 0.0f, 0.0f, 1.0f); } }
  20. public static Color BansheeOrange { get { return new Color(1.0f, (168.0f/255.0f), 0.0f, 1.0f); } }
  21. public float this[int index]
  22. {
  23. get
  24. {
  25. switch (index)
  26. {
  27. case 0:
  28. return r;
  29. case 1:
  30. return g;
  31. case 2:
  32. return b;
  33. case 3:
  34. return a;
  35. default:
  36. throw new IndexOutOfRangeException("Invalid Color index.");
  37. }
  38. }
  39. set
  40. {
  41. switch (index)
  42. {
  43. case 0:
  44. r = value;
  45. break;
  46. case 1:
  47. g = value;
  48. break;
  49. case 2:
  50. b = value;
  51. break;
  52. case 3:
  53. a = value;
  54. break;
  55. default:
  56. throw new IndexOutOfRangeException("Invalid Color index.");
  57. }
  58. }
  59. }
  60. public Color(float r, float g, float b, float a)
  61. {
  62. this.r = r;
  63. this.g = g;
  64. this.b = b;
  65. this.a = a;
  66. }
  67. public Color(float r, float g, float b)
  68. {
  69. this.r = r;
  70. this.g = g;
  71. this.b = b;
  72. this.a = 1f;
  73. }
  74. public static Color operator+ (Color a, Color b)
  75. {
  76. return new Color(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a);
  77. }
  78. public static Color operator- (Color a, Color b)
  79. {
  80. return new Color(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a);
  81. }
  82. public static Color operator* (Color a, Color b)
  83. {
  84. return new Color(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a);
  85. }
  86. public static Color operator* (Color a, float b)
  87. {
  88. return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
  89. }
  90. public static Color operator* (float b, Color a)
  91. {
  92. return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
  93. }
  94. public static Color operator/ (Color a, float b)
  95. {
  96. return new Color(a.r / b, a.g / b, a.b / b, a.a / b);
  97. }
  98. public static bool operator ==(Color lhs, Color rhs)
  99. {
  100. return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b && lhs.a == rhs.a;
  101. }
  102. public static bool operator !=(Color lhs, Color rhs)
  103. {
  104. return !(lhs == rhs);
  105. }
  106. public override int GetHashCode()
  107. {
  108. return r.GetHashCode() ^ g.GetHashCode() << 2 ^ b.GetHashCode() >> 2 ^ a.GetHashCode() >> 1;
  109. }
  110. public override bool Equals(object other)
  111. {
  112. if (!(other is Color))
  113. return false;
  114. Color color = (Color)other;
  115. if (r.Equals(color.r) && g.Equals(color.g) && b.Equals(color.b) && a.Equals(color.a))
  116. return true;
  117. return false;
  118. }
  119. }
  120. }