ColorPicker.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Color Picker", "Color Picker.")]
  5. [ScenarioCategory ("Colors")]
  6. [ScenarioCategory ("Controls")]
  7. public class ColorPickers : Scenario
  8. {
  9. /// <summary>Background color Label.</summary>
  10. private Label _backgroundColorLabel;
  11. /// <summary>Demo label.</summary>
  12. private View _demoView;
  13. /// <summary>Foreground color label.</summary>
  14. private Label _foregroundColorLabel;
  15. /// <summary>Background ColorPicker.</summary>
  16. private ColorPicker backgroundColorPicker;
  17. /// <summary>Foreground ColorPicker.</summary>
  18. private ColorPicker foregroundColorPicker;
  19. /// <summary>Setup the scenario.</summary>
  20. public override void Main ()
  21. {
  22. Application.Init ();
  23. Window app = new ()
  24. {
  25. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
  26. };
  27. // Foreground ColorPicker.
  28. foregroundColorPicker = new ColorPicker { Title = "Foreground Color", BorderStyle = LineStyle.Single };
  29. foregroundColorPicker.ColorChanged += ForegroundColor_ColorChanged;
  30. app.Add (foregroundColorPicker);
  31. _foregroundColorLabel = new Label
  32. {
  33. X = Pos.Left (foregroundColorPicker), Y = Pos.Bottom (foregroundColorPicker) + 1
  34. };
  35. app.Add (_foregroundColorLabel);
  36. // Background ColorPicker.
  37. backgroundColorPicker = new ColorPicker
  38. {
  39. Title = "Background Color",
  40. X = Pos.AnchorEnd (),
  41. BoxHeight = 1,
  42. BoxWidth = 4,
  43. BorderStyle = LineStyle.Single,
  44. };
  45. backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
  46. app.Add (backgroundColorPicker);
  47. _backgroundColorLabel = new Label ()
  48. {
  49. X = Pos.AnchorEnd (),
  50. Y = Pos.Bottom (backgroundColorPicker) + 1
  51. };
  52. app.Add (_backgroundColorLabel);
  53. // Demo Label.
  54. _demoView = new View
  55. {
  56. Title = "Color Sample",
  57. Text = "Lorem Ipsum",
  58. TextAlignment = Alignment.Center,
  59. VerticalTextAlignment = Alignment.Center,
  60. BorderStyle = LineStyle.Heavy,
  61. X = Pos.Center (),
  62. Y = Pos.Center (),
  63. Height = 5,
  64. Width = 20
  65. };
  66. app.Add (_demoView);
  67. // Set default colors.
  68. foregroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Foreground.GetClosestNamedColor ();
  69. backgroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Background.GetClosestNamedColor ();
  70. app.Initialized += (s, e) => app.LayoutSubviews ();
  71. Application.Run (app);
  72. app.Dispose ();
  73. }
  74. /// <summary>Fired when background color is changed.</summary>
  75. private void BackgroundColor_ColorChanged (object sender, EventArgs e)
  76. {
  77. UpdateColorLabel (_backgroundColorLabel, backgroundColorPicker);
  78. UpdateDemoLabel ();
  79. }
  80. /// <summary>Fired when foreground color is changed.</summary>
  81. private void ForegroundColor_ColorChanged (object sender, EventArgs e)
  82. {
  83. UpdateColorLabel (_foregroundColorLabel, foregroundColorPicker);
  84. UpdateDemoLabel ();
  85. }
  86. /// <summary>Update a color label from his ColorPicker.</summary>
  87. private void UpdateColorLabel (Label label, ColorPicker colorPicker)
  88. {
  89. label.Clear ();
  90. var color = new Color (colorPicker.SelectedColor);
  91. label.Text =
  92. $"{colorPicker.SelectedColor} ({(int)colorPicker.SelectedColor}) #{color.R:X2}{color.G:X2}{color.B:X2}";
  93. }
  94. /// <summary>Update Demo Label.</summary>
  95. private void UpdateDemoLabel ()
  96. {
  97. _demoView.ColorScheme = new ColorScheme
  98. {
  99. Normal = new Attribute (
  100. foregroundColorPicker.SelectedColor,
  101. backgroundColorPicker.SelectedColor
  102. )
  103. };
  104. }
  105. }