Color.Operators.cs 4.2 KB

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