ColorScheme.cs 4.9 KB

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