Color.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Runtime.InteropServices;
  2. namespace AtomicEngine
  3. {
  4. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  5. public struct Color
  6. {
  7. public Color(float r, float g, float b, float a = 1.0f)
  8. {
  9. R = r;
  10. G = g;
  11. B = b;
  12. A = a;
  13. }
  14. public uint ToUInt()
  15. {
  16. uint r = (uint)(R * 255.0f);
  17. uint g = (uint)(G * 255.0f);
  18. uint b = (uint)(B * 255.0f);
  19. uint a = (uint)(A * 255.0f);
  20. return (a << 24) | (b << 16) | (g << 8) | r;
  21. }
  22. public static readonly Color White = new Color(1, 1, 1);
  23. public static readonly Color Gray = new Color(0.5f, 0.5f, 0.5f);
  24. public static readonly Color Black = new Color(0.0f, 0.0f, 0.0f);
  25. public static readonly Color Red = new Color(1.0f, 0.0f, 0.0f);
  26. public static readonly Color Green = new Color(0.0f, 1.0f, 0.0f);
  27. public static readonly Color Blue = new Color(0.0f, 0.0f, 1.0f);
  28. public static readonly Color Cyan = new Color(0.0f, 1.0f, 1.0f);
  29. public static readonly Color Magenta = new Color(1.0f, 0.0f, 1.0f);
  30. public static readonly Color Yellow = new Color(1.0f, 1.0f, 0.0f);
  31. public static readonly Color LightBlue = new Color(0.50f, 0.88f, 0.81f);
  32. public static readonly Color Transparent = new Color(0.0f, 0.0f, 0.0f, 0.0f);
  33. public static Color operator *(Color value, float scale)
  34. {
  35. return new Color((float)(value.R * scale), (float)(value.G * scale), (float)(value.B * scale), (float)(value.A * scale));
  36. }
  37. public static Color Lerp(Color value1, Color value2, float amount)
  38. {
  39. amount = MathHelper.Clamp(amount, 0, 1);
  40. return new Color(
  41. MathHelper.Lerp(value1.R, value2.R, amount),
  42. MathHelper.Lerp(value1.G, value2.G, amount),
  43. MathHelper.Lerp(value1.B, value2.B, amount),
  44. MathHelper.Lerp(value1.A, value2.A, amount));
  45. }
  46. public float R;
  47. public float G;
  48. public float B;
  49. public float A;
  50. }
  51. }