ColorScheme.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #nullable enable
  2. using System.Numerics;
  3. using System.Text.Json.Serialization;
  4. namespace Terminal.Gui;
  5. /// <summary>Defines a standard set of <see cref="Attribute"/>s for common visible elements in a <see cref="View"/>.</summary>
  6. /// <remarks>
  7. /// <para>
  8. /// ColorScheme objects are immutable. Once constructed, the properties cannot be changed. To change a
  9. /// ColorScheme, create a new one with the desired values, using the <see cref="ColorScheme(ColorScheme)"/>
  10. /// constructor.
  11. /// </para>
  12. /// </remarks>
  13. [JsonConverter (typeof (ColorSchemeJsonConverter))]
  14. public record ColorScheme : IEqualityOperators<ColorScheme, ColorScheme, bool>
  15. {
  16. private readonly Attribute _disabled;
  17. private readonly Attribute _focus;
  18. private readonly Attribute _hotFocus;
  19. private readonly Attribute _hotNormal;
  20. private readonly Attribute _normal;
  21. /// <summary>Creates a new instance set to the default colors (see <see cref="Attribute.Default"/>).</summary>
  22. public ColorScheme () : this (Attribute.Default) { }
  23. /// <summary>Creates a new instance, initialized with the values from <paramref name="scheme"/>.</summary>
  24. /// <param name="scheme">The scheme to initialize the new instance with.</param>
  25. public ColorScheme (ColorScheme? scheme)
  26. {
  27. ArgumentNullException.ThrowIfNull (scheme);
  28. _normal = scheme.Normal;
  29. _focus = scheme.Focus;
  30. _hotNormal = scheme.HotNormal;
  31. _disabled = scheme.Disabled;
  32. _hotFocus = scheme.HotFocus;
  33. }
  34. /// <summary>Creates a new instance, initialized with the values from <paramref name="attribute"/>.</summary>
  35. /// <param name="attribute">The attribute to initialize the new instance with.</param>
  36. public ColorScheme (Attribute attribute)
  37. {
  38. _normal = attribute;
  39. _focus = attribute;
  40. _hotNormal = attribute;
  41. _disabled = attribute;
  42. _hotFocus = attribute;
  43. }
  44. /// <summary>The default foreground and background color for text when the view is disabled.</summary>
  45. public Attribute Disabled
  46. {
  47. get => _disabled;
  48. init => _disabled = value;
  49. }
  50. /// <summary>The foreground and background color for text when the view has the focus.</summary>
  51. public Attribute Focus
  52. {
  53. get => _focus;
  54. init => _focus = value;
  55. }
  56. /// <summary>The foreground and background color for for text in a focused view that indicates a <see cref="View.HotKey"/>.</summary>
  57. public Attribute HotFocus
  58. {
  59. get => _hotFocus;
  60. init => _hotFocus = value;
  61. }
  62. /// <summary>The foreground and background color for text in a non-focused view that indicates a <see cref="View.HotKey"/>.</summary>
  63. public Attribute HotNormal
  64. {
  65. get => _hotNormal;
  66. init => _hotNormal = value;
  67. }
  68. /// <summary>The foreground and background color for text when the view is not focused, hot, or disabled.</summary>
  69. public Attribute Normal
  70. {
  71. get => _normal;
  72. init => _normal = value;
  73. }
  74. /// <summary>Compares two <see cref="ColorScheme"/> objects for equality.</summary>
  75. /// <param name="other"></param>
  76. /// <returns>true if the two objects are equal</returns>
  77. public virtual bool Equals (ColorScheme? other)
  78. {
  79. return other is { }
  80. && EqualityComparer<Attribute>.Default.Equals (_normal, other._normal)
  81. && EqualityComparer<Attribute>.Default.Equals (_focus, other._focus)
  82. && EqualityComparer<Attribute>.Default.Equals (_hotNormal, other._hotNormal)
  83. && EqualityComparer<Attribute>.Default.Equals (_hotFocus, other._hotFocus)
  84. && EqualityComparer<Attribute>.Default.Equals (_disabled, other._disabled);
  85. }
  86. /// <summary>Returns a hashcode for this instance.</summary>
  87. /// <returns>hashcode for this instance</returns>
  88. public override int GetHashCode ()
  89. {
  90. return HashCode.Combine (_normal, _focus, _hotNormal, _hotFocus, _disabled);
  91. }
  92. /// <inheritdoc/>
  93. public override string ToString () { return $"Normal: {Normal}; Focus: {Focus}; HotNormal: {HotNormal}; HotFocus: {HotFocus}; Disabled: {Disabled}"; }
  94. }