ColorScheme.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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>Creates a new instance, initialized with the values provided.</summary>
  45. public ColorScheme (
  46. Attribute normal,
  47. Attribute focus,
  48. Attribute hotNormal,
  49. Attribute disabled,
  50. Attribute hotFocus)
  51. {
  52. _normal = normal;
  53. _focus = focus;
  54. _hotNormal = hotNormal;
  55. _disabled = disabled;
  56. _hotFocus = hotFocus;
  57. }
  58. /// <summary>The default foreground and background color for text when the view is disabled.</summary>
  59. public Attribute Disabled
  60. {
  61. get => _disabled;
  62. init => _disabled = value;
  63. }
  64. /// <summary>The foreground and background color for text when the view has the focus.</summary>
  65. public Attribute Focus
  66. {
  67. get => _focus;
  68. init => _focus = value;
  69. }
  70. /// <summary>The foreground and background color for text in a focused view that indicates a <see cref="View.HotKey"/>.</summary>
  71. public Attribute HotFocus
  72. {
  73. get => _hotFocus;
  74. init => _hotFocus = value;
  75. }
  76. /// <summary>The foreground and background color for text in a non-focused view that indicates a <see cref="View.HotKey"/>.</summary>
  77. public Attribute HotNormal
  78. {
  79. get => _hotNormal;
  80. init => _hotNormal = value;
  81. }
  82. /// <summary>The foreground and background color for text when the view is not focused, hot, or disabled.</summary>
  83. public Attribute Normal
  84. {
  85. get => _normal;
  86. init => _normal = value;
  87. }
  88. /// <summary>Compares two <see cref="ColorScheme"/> objects for equality.</summary>
  89. /// <param name="other"></param>
  90. /// <returns>true if the two objects are equal</returns>
  91. public virtual bool Equals (ColorScheme? other)
  92. {
  93. return other is { }
  94. && EqualityComparer<Attribute>.Default.Equals (_normal, other._normal)
  95. && EqualityComparer<Attribute>.Default.Equals (_focus, other._focus)
  96. && EqualityComparer<Attribute>.Default.Equals (_hotNormal, other._hotNormal)
  97. && EqualityComparer<Attribute>.Default.Equals (_hotFocus, other._hotFocus)
  98. && EqualityComparer<Attribute>.Default.Equals (_disabled, other._disabled);
  99. }
  100. /// <summary>Returns a hashcode for this instance.</summary>
  101. /// <returns>hashcode for this instance</returns>
  102. public override int GetHashCode ()
  103. {
  104. return HashCode.Combine (_normal, _focus, _hotNormal, _hotFocus, _disabled);
  105. }
  106. /// <inheritdoc/>
  107. public override string ToString () { return $"Normal: {Normal}; Focus: {Focus}; HotNormal: {HotNormal}; HotFocus: {HotFocus}; Disabled: {Disabled}"; }
  108. }