InvertColors.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.ColorSchemes ["TopLevel"];
  13. List<Label> labels = new List<Label> ();
  14. var foreColors = Enum.GetValues (typeof (ColorName)).Cast<ColorName> ().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 = new ColorScheme (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 = new ColorScheme (label.ColorScheme) {
  36. Normal = color
  37. };
  38. label.Text = $"{color.Foreground} on {color.Background}";
  39. label.SetNeedsDisplay ();
  40. }
  41. };
  42. Win.Add (button);
  43. }
  44. }
  45. }