Color.Operators.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #nullable enable
  2. using System.Diagnostics.Contracts;
  3. using System.Numerics;
  4. namespace Terminal.Gui;
  5. public readonly partial record struct Color {
  6. /// <inheritdoc />
  7. /// <returns>
  8. /// A <see cref="Color" /> <see langword="struct" /> with all values set to <see cref="byte.MaxValue" />, meaning white.
  9. /// </returns>
  10. public static Color MaxValue => new Color (uint.MaxValue);
  11. /// <inheritdoc />
  12. /// <returns>A <see cref="Color" /> <see langword="struct" /> with all values set to zero.</returns>
  13. /// <remarks>
  14. /// Though this returns a <see cref="Color" /> with <see cref="A" />, <see cref="R" />, <see cref="G" />, and <see cref="B" /> all set to
  15. /// zero, Terminal.Gui will treat it as black, because the alpha channel is not supported.
  16. /// </remarks>
  17. public static Color MinValue => new Color (uint.MinValue);
  18. /// <inheritdoc />
  19. [Pure]
  20. public override int GetHashCode () => Rgba.GetHashCode ();
  21. /// <summary>
  22. /// Implicit conversion from <see cref="Color" /> to <see cref="Vector3" /> via <see cref="Vector3(float,float,float)" /> where (
  23. /// <see cref="Vector3.X" />, <see cref="Vector3.Y" />, <see cref="Vector3.Z" />) is (R,G,B).
  24. /// </summary>
  25. /// <remarks>
  26. /// This cast is narrowing and drops the alpha channel.
  27. /// <para />
  28. /// Use <see cref="implicit operator Vector4(Color)" /> to maintain full value.
  29. /// </remarks>
  30. [Pure]
  31. public static explicit operator Vector3 (Color color) => new Vector3 (color.R, color.G, color.B);
  32. /// <summary>
  33. /// Implicit conversion from <see langword="int" /> to <see cref="Color" />, via the <see cref="Color(int)" /> costructor.
  34. /// </summary>
  35. [Pure]
  36. public static implicit operator Color (int rgba) => new Color (rgba);
  37. /// <summary>
  38. /// Implicit conversion from <see cref="Color" /> to <see langword="int" /> by returning the value of the <see cref="Rgba" /> field.
  39. /// </summary>
  40. [Pure]
  41. public static implicit operator int (Color color) => color.Rgba;
  42. /// <summary>
  43. /// Implicit conversion from <see langword="uint" /> to <see cref="Color" />, via the <see cref="Color(uint)" /> costructor.
  44. /// </summary>
  45. [Pure]
  46. public static implicit operator Color (uint u) => new Color (u);
  47. /// <summary>
  48. /// Implicit conversion from <see cref="Color" /> to <see langword="uint" /> by returning the value of the <see cref="Argb" /> field.
  49. /// </summary>
  50. [Pure]
  51. public static implicit operator uint (Color color) => color.Argb;
  52. /// <summary>
  53. /// Implicit conversion from <see cref="GetClosestNamedColor" /> to <see cref="Color" /> via lookup from
  54. /// <see cref="ColorExtensions.ColorNameToColorMap" />.
  55. /// </summary>
  56. [Pure]
  57. public static implicit operator Color (ColorName colorName) => ColorExtensions.ColorNameToColorMap [colorName];
  58. /// <summary>
  59. /// Implicit conversion from <see cref="Vector4" /> to <see cref="Color" />, where (<see cref="Vector4.X" />, <see cref="Vector4.Y" />,
  60. /// <see cref="Vector4.Z" />, <see cref="Vector4.W" />) is (<see cref="A" />,<see cref="R" />,<see cref="G" />,<see cref="B" />), via
  61. /// <see cref="Color(int,int,int,int)" />.
  62. /// </summary>
  63. [Pure]
  64. public static implicit operator Color (Vector4 v) => new Color ((byte)v.X, (byte)v.Y, (byte)v.Z, (byte)v.W);
  65. /// <summary>
  66. /// Implicit conversion to <see cref="Vector3" />, where <see cref="Vector3.X" /> = <see cref="R" />, <see cref="Vector3.Y" /> =
  67. /// <see cref="G" />, and <see cref="Vector3.Z" /> = <see cref="B" />.
  68. /// </summary>
  69. [Pure]
  70. public static implicit operator Vector4 (Color color) => new Vector4 (color.R, color.G, color.B, color.A);
  71. /// <summary>
  72. /// Implicit conversion from <see cref="Vector3" />, where <see cref="Vector3.X" /> = <see cref="R" />, <see cref="Vector3.Y" /> =
  73. /// <see cref="G" />, and <see cref="Vector3.Z" /> = <see cref="B" />.
  74. /// </summary>
  75. [Pure]
  76. public static implicit operator Color (Vector3 v) => new Color ((byte)v.X, (byte)v.Y, (byte)v.Z);
  77. }