InvertColors.cs 1.4 KB

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