TrueColors.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("True Colors", "Demonstration of true color support.")]
  5. [ScenarioCategory ("Colors")]
  6. public class TrueColors : Scenario
  7. {
  8. public override void Main ()
  9. {
  10. Application.Init ();
  11. Window app = new ()
  12. {
  13. Title = GetQuitKeyAndName ()
  14. };
  15. var x = 2;
  16. var y = 1;
  17. bool canTrueColor = Application.Driver?.SupportsTrueColor ?? false;
  18. var lblDriverName = new Label
  19. {
  20. X = x, Y = y++, Text = $"Current driver is {Application.Driver?.GetType ().Name}"
  21. };
  22. app.Add (lblDriverName);
  23. y++;
  24. var cbSupportsTrueColor = new CheckBox
  25. {
  26. X = x,
  27. Y = y++,
  28. State = canTrueColor ? CheckState.Checked : CheckState.UnChecked,
  29. CanFocus = false,
  30. Enabled = false,
  31. Text = "Driver supports true color "
  32. };
  33. app.Add (cbSupportsTrueColor);
  34. var cbUseTrueColor = new CheckBox
  35. {
  36. X = x,
  37. Y = y++,
  38. State = Application.Force16Colors ? CheckState.Checked : CheckState.UnChecked,
  39. Enabled = canTrueColor,
  40. Text = "Force 16 colors"
  41. };
  42. cbUseTrueColor.Toggle += (_, evt) => { Application.Force16Colors = evt.NewValue == CheckState.Checked; };
  43. app.Add (cbUseTrueColor);
  44. y += 2;
  45. SetupGradient ("Red gradient", x, ref y, i => new (i, 0));
  46. SetupGradient ("Green gradient", x, ref y, i => new (0, i));
  47. SetupGradient ("Blue gradient", x, ref y, i => new (0, 0, i));
  48. SetupGradient ("Yellow gradient", x, ref y, i => new (i, i));
  49. SetupGradient ("Magenta gradient", x, ref y, i => new (i, 0, i));
  50. SetupGradient ("Cyan gradient", x, ref y, i => new (0, i, i));
  51. SetupGradient ("Gray gradient", x, ref y, i => new (i, i, i));
  52. app.Add (
  53. new Label { X = Pos.AnchorEnd (44), Y = 2, Text = "Mouse over to get the gradient view color:" }
  54. );
  55. app.Add (
  56. new Label { X = Pos.AnchorEnd (44), Y = 4, Text = "Red:" }
  57. );
  58. app.Add (
  59. new Label { X = Pos.AnchorEnd (44), Y = 5, Text = "Green:" }
  60. );
  61. app.Add (
  62. new Label { X = Pos.AnchorEnd (44), Y = 6, Text = "Blue:" }
  63. );
  64. app.Add (
  65. new Label { X = Pos.AnchorEnd (44), Y = 8, Text = "Darker:" }
  66. );
  67. app.Add (
  68. new Label { X = Pos.AnchorEnd (44), Y = 9, Text = "Lighter:" }
  69. );
  70. var lblRed = new Label { X = Pos.AnchorEnd (32), Y = 4, Text = "na" };
  71. app.Add (lblRed);
  72. var lblGreen = new Label { X = Pos.AnchorEnd (32), Y = 5, Text = "na" };
  73. app.Add (lblGreen);
  74. var lblBlue = new Label { X = Pos.AnchorEnd (32), Y = 6, Text = "na" };
  75. app.Add (lblBlue);
  76. var lblDarker = new Label { X = Pos.AnchorEnd (32), Y = 8, Text = " " };
  77. app.Add (lblDarker);
  78. var lblLighter = new Label { X = Pos.AnchorEnd (32), Y = 9, Text = " " };
  79. app.Add (lblLighter);
  80. Application.MouseEvent += (s, e) =>
  81. {
  82. if (e.View == null)
  83. {
  84. return;
  85. }
  86. if (e.Flags == MouseFlags.Button1Clicked)
  87. {
  88. Attribute normal = e.View.GetNormalColor ();
  89. lblLighter.ColorScheme = new (e.View.ColorScheme)
  90. {
  91. Normal = new (
  92. normal.Foreground,
  93. normal.Background.GetHighlightColor ()
  94. )
  95. };
  96. }
  97. else
  98. {
  99. Attribute normal = e.View.GetNormalColor ();
  100. lblRed.Text = normal.Foreground.R.ToString ();
  101. lblGreen.Text = normal.Foreground.G.ToString ();
  102. lblBlue.Text = normal.Foreground.B.ToString ();
  103. }
  104. };
  105. Application.Run (app);
  106. app.Dispose ();
  107. Application.Shutdown ();
  108. return;
  109. void SetupGradient (string name, int x, ref int y, Func<int, Color> colorFunc)
  110. {
  111. var gradient = new Label { X = x, Y = y++, Text = name };
  112. app.Add (gradient);
  113. for (int dx = x, i = 0; i <= 256; i += 4)
  114. {
  115. var l = new Label
  116. {
  117. X = dx++,
  118. Y = y,
  119. ColorScheme = new()
  120. {
  121. Normal = new (
  122. colorFunc (Math.Clamp (i, 0, 255)),
  123. colorFunc (Math.Clamp (i, 0, 255))
  124. )
  125. },
  126. Text = " "
  127. };
  128. app.Add (l);
  129. }
  130. y += 2;
  131. }
  132. }
  133. }