Color.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SuperTux Editor
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. namespace Drawing
  18. {
  19. public struct Color
  20. {
  21. /// <summary>red part of the color</summary>
  22. public float Red;
  23. /// <summary>green part of the color</summary>
  24. public float Green;
  25. /// <summary>blue part of the color</summary>
  26. public float Blue;
  27. /// <summary>alpha part of the color</summary>
  28. /// <remarks>1.0 = full opaque, 0.0 = invisible</remarks>
  29. public float Alpha;
  30. public Color(float Red, float Green, float Blue, float Alpha)
  31. {
  32. this.Red = Red;
  33. this.Green = Green;
  34. this.Blue = Blue;
  35. this.Alpha = Alpha;
  36. }
  37. public Color(float Red, float Green, float Blue)
  38. {
  39. this.Red = Red;
  40. this.Green = Green;
  41. this.Blue = Blue;
  42. this.Alpha = 1.0f;
  43. }
  44. public static bool operator ==(Color c1, Color c2) {
  45. return c1.Red == c2.Red && c1.Green == c2.Green && c1.Blue == c2.Blue && c1.Alpha == c2.Alpha;
  46. }
  47. public static bool operator !=(Color c1, Color c2) {
  48. return c1.Red != c2.Red || c1.Green != c2.Green || c1.Blue != c2.Blue || c1.Alpha != c2.Alpha;
  49. }
  50. public override bool Equals(object obj) {
  51. if (obj is Color) {
  52. Color color = (Color)obj;
  53. return this == color;
  54. } else if (obj is string) {
  55. // FIXME: This is a bit of an hack to
  56. // allow colors to be specified in
  57. // object attributes, as this doesn't work:
  58. //
  59. // [LispChild("color", Optional = true, Default = new Color(1,1,1))]
  60. //
  61. // instead use this:
  62. //
  63. // [LispChild("color", Optional = true, Default = "Color(1, 1, 1, 1)")]
  64. //
  65. // Note that this is a dumb string
  66. // compare, no parsing takes place, so
  67. // specify all four color components
  68. // and make sure the spaces are
  69. // exactly tha same or it will fail.
  70. string text = String.Format("Color({0}, {1}, {2}, {3})", Red, Green, Blue, Alpha);
  71. return text.Equals(obj);
  72. } else {
  73. return false;
  74. }
  75. }
  76. public override int GetHashCode() {
  77. return base.GetHashCode();
  78. }
  79. }
  80. }
  81. /* EOF */