/Datatypes/Color.cs

https://github.com/freesquare/DeltaEngine · C# · 101 lines · 84 code · 14 blank · 3 comment · 2 complexity · 0e41f683f9390edd39f11a149ac0ed1a MD5 · raw file

  1. using System;
  2. using System.Diagnostics;
  3. using DeltaEngine.Core;
  4. namespace DeltaEngine.Datatypes
  5. {
  6. /// <summary>
  7. /// Color with a byte or float per component (red, green, blue, alpha)
  8. /// </summary>
  9. [DebuggerDisplay("Color(R={R}, G={G}, B={B}, A={A})")]
  10. public struct Color : IEquatable<Color>
  11. {
  12. public Color(byte r, byte g, byte b, byte a = 255)
  13. : this()
  14. {
  15. R = r;
  16. G = g;
  17. B = b;
  18. A = a;
  19. }
  20. public byte R { get; set; }
  21. public byte G { get; set; }
  22. public byte B { get; set; }
  23. public byte A { get; set; }
  24. public Color(float r, float g, float b, float a = 1.0f)
  25. : this()
  26. {
  27. R = (byte)(r * 255);
  28. G = (byte)(g * 255);
  29. B = (byte)(b * 255);
  30. A = (byte)(a * 255);
  31. }
  32. public static readonly Color Black = new Color(0, 0, 0);
  33. public static readonly Color Blue = new Color(0, 0, 255);
  34. public static readonly Color CornflowerBlue = new Color(100, 149, 237);
  35. public static readonly Color Cyan = new Color(0, 255, 255);
  36. public static readonly Color Gray = new Color(128, 128, 128);
  37. public static readonly Color Green = new Color(0, 255, 0);
  38. public static readonly Color PaleGreen = new Color(152, 251, 152);
  39. public static readonly Color Pink = new Color(255, 192, 203);
  40. public static readonly Color Purple = new Color(255, 0, 255);
  41. public static readonly Color Red = new Color(255, 0, 0);
  42. public static readonly Color White = new Color(255, 255, 255);
  43. public static readonly Color Yellow = new Color(255, 255, 0);
  44. public int PackedArgb
  45. {
  46. get
  47. {
  48. return (A << 24) + (R << 16) + (G << 8) + B;
  49. }
  50. }
  51. public static bool operator !=(Color c1, Color c2)
  52. {
  53. return c1.Equals(c2) == false;
  54. }
  55. public static bool operator ==(Color c1, Color c2)
  56. {
  57. return c1.Equals(c2);
  58. }
  59. public bool Equals(Color other)
  60. {
  61. return PackedArgb == other.PackedArgb;
  62. }
  63. public override bool Equals(object other)
  64. {
  65. return other is Color ? Equals((Color)other) : base.Equals(other);
  66. }
  67. public override int GetHashCode()
  68. {
  69. return PackedArgb;
  70. }
  71. public static Color Lerp(Color color1, Color color2, float percentage)
  72. {
  73. var r = (byte)(MathExtensions.Lerp(color1.R, color2.R, percentage));
  74. var g = (byte)(MathExtensions.Lerp(color1.G, color2.G, percentage));
  75. var b = (byte)(MathExtensions.Lerp(color1.B, color2.B, percentage));
  76. var a = (byte)(MathExtensions.Lerp(color1.A, color2.A, percentage));
  77. return new Color(r, g, b, a);
  78. }
  79. public static Color GetRandomBrightColor()
  80. {
  81. var r = (byte)PseudoRandom.Get(128, 256);
  82. var g = (byte)PseudoRandom.Get(128, 256);
  83. var b = (byte)PseudoRandom.Get(128, 256);
  84. return new Color(r, g, b);
  85. }
  86. }
  87. }