ColorScheme.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /// <summary>Creates a new instance set to the default colors (see <see cref="Attribute.Default"/>).</summary>
  17. public ColorScheme () : this (Attribute.Default) { }
  18. /// <summary>Creates a new instance, initialized with the values from <paramref name="scheme"/>.</summary>
  19. /// <param name="scheme">The scheme to initialize the new instance with.</param>
  20. public ColorScheme (ColorScheme? scheme)
  21. {
  22. ArgumentNullException.ThrowIfNull (scheme);
  23. Normal = scheme.Normal;
  24. Focus = scheme.Focus;
  25. HotNormal = scheme.HotNormal;
  26. Disabled = scheme.Disabled;
  27. HotFocus = scheme.HotFocus;
  28. }
  29. /// <summary>Creates a new instance, initialized with the values from <paramref name="attribute"/>.</summary>
  30. /// <param name="attribute">The attribute to initialize the new instance with.</param>
  31. public ColorScheme (Attribute attribute)
  32. {
  33. Normal = attribute;
  34. Focus = attribute;
  35. HotNormal = attribute;
  36. Disabled = attribute;
  37. HotFocus = attribute;
  38. }
  39. /// <summary>Creates a new instance, initialized with the values provided.</summary>
  40. public ColorScheme (
  41. Attribute normal,
  42. Attribute focus,
  43. Attribute hotNormal,
  44. Attribute disabled,
  45. Attribute hotFocus
  46. )
  47. {
  48. Normal = normal;
  49. Focus = focus;
  50. HotNormal = hotNormal;
  51. Disabled = disabled;
  52. HotFocus = hotFocus;
  53. }
  54. /// <summary>The default foreground and background color for text when the view is disabled.</summary>
  55. public Attribute Disabled { get; init; }
  56. /// <summary>The foreground and background color for text when the view has the focus.</summary>
  57. public Attribute Focus { get; init; }
  58. /// <summary>The foreground and background color for text in a focused view that indicates a <see cref="View.HotKey"/>.</summary>
  59. public Attribute HotFocus { get; init; }
  60. /// <summary>The foreground and background color for text in a non-focused view that indicates a <see cref="View.HotKey"/>.</summary>
  61. public Attribute HotNormal { get; init; }
  62. /// <summary>The foreground and background color for text when the view is not focused, hot, or disabled.</summary>
  63. public Attribute Normal { get; init; }
  64. /// <summary>
  65. /// Gets a new <see cref="ColorScheme"/> with the same values as this instance, but with the foreground and background
  66. /// colors adjusted to be more visible.
  67. /// </summary>
  68. /// <returns></returns>
  69. public ColorScheme GetHighlightColorScheme ()
  70. {
  71. return this with
  72. {
  73. Normal = new (Normal.Foreground.GetHighlightColor (), Normal.Background),
  74. HotNormal = new (HotNormal.Foreground.GetHighlightColor (), HotNormal.Background),
  75. Focus = new (Focus.Foreground.GetHighlightColor (), Focus.Background),
  76. HotFocus = new (HotFocus.Foreground.GetHighlightColor (), HotFocus.Background)
  77. };
  78. }
  79. /// <summary>Compares two <see cref="ColorScheme"/> objects for equality.</summary>
  80. /// <param name="other"></param>
  81. /// <returns>true if the two objects are equal</returns>
  82. public virtual bool Equals (ColorScheme? other)
  83. {
  84. return other is { }
  85. && EqualityComparer<Attribute>.Default.Equals (Normal, other.Normal)
  86. && EqualityComparer<Attribute>.Default.Equals (Focus, other.Focus)
  87. && EqualityComparer<Attribute>.Default.Equals (HotNormal, other.HotNormal)
  88. && EqualityComparer<Attribute>.Default.Equals (HotFocus, other.HotFocus)
  89. && EqualityComparer<Attribute>.Default.Equals (Disabled, other.Disabled);
  90. }
  91. /// <summary>Returns a hashcode for this instance.</summary>
  92. /// <returns>hashcode for this instance</returns>
  93. public override int GetHashCode () { return HashCode.Combine (Normal, Focus, HotNormal, HotFocus, Disabled); }
  94. /// <inheritdoc/>
  95. public override string ToString () { return $"Normal: {Normal}; Focus: {Focus}; HotNormal: {HotNormal}; HotFocus: {HotFocus}; Disabled: {Disabled}"; }
  96. }