InvertColors.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("Invert Colors", "Invert the foreground and the background colors.")]
  7. [ScenarioCategory ("Colors")]
  8. [ScenarioCategory ("Text and Formatting")]
  9. public class InvertColors : Scenario
  10. {
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. var win = new Window
  15. {
  16. Title = GetQuitKeyAndName (),
  17. ColorScheme = Colors.ColorSchemes ["TopLevel"]
  18. };
  19. List<Label> labels = new ();
  20. ColorName16 [] foreColors = Enum.GetValues (typeof (ColorName16)).Cast<ColorName16> ().ToArray ();
  21. for (var y = 0; y < foreColors.Length; y++)
  22. {
  23. ColorName16 fore = foreColors [y];
  24. ColorName16 back = foreColors [(y + 1) % foreColors.Length];
  25. var color = new Attribute (fore, back);
  26. var label = new Label { ColorScheme = new ColorScheme (), Y = y, Text = $"{fore} on {back}" };
  27. label.ColorScheme = new ColorScheme (label.ColorScheme) { Normal = color };
  28. win.Add (label);
  29. labels.Add (label);
  30. }
  31. var button = new Button { X = Pos.Center (), Y = foreColors.Length + 1, Text = "Invert color!" };
  32. button.Accepting += (s, e) =>
  33. {
  34. foreach (Label label in labels)
  35. {
  36. Attribute color = label.ColorScheme.Normal;
  37. color = new Attribute (color.Background, color.Foreground);
  38. label.ColorScheme = new ColorScheme (label.ColorScheme) { Normal = color };
  39. label.Text = $"{color.Foreground} on {color.Background}";
  40. label.SetNeedsDisplay ();
  41. }
  42. };
  43. win.Add (button);
  44. Application.Run (win);
  45. win.Dispose ();
  46. Application.Shutdown ();
  47. }
  48. }