ColorPicker.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Terminal.Gui;
  2. using System;
  3. namespace UICatalog.Scenarios {
  4. [ScenarioMetadata (Name: "Color Picker", Description: "Color Picker.")]
  5. [ScenarioCategory ("Colors")]
  6. [ScenarioCategory ("Controls")]
  7. public class ColorPickers : Scenario {
  8. /// <summary>
  9. /// Foreground ColorPicker.
  10. /// </summary>
  11. private ColorPicker foregroundColorPicker;
  12. /// <summary>
  13. /// Background ColorPicker.
  14. /// </summary>
  15. private ColorPicker backgroundColorPicker;
  16. /// <summary>
  17. /// Foreground color label.
  18. /// </summary>
  19. private Label foregroundColorLabel;
  20. /// <summary>
  21. /// Background color Label.
  22. /// </summary>
  23. private Label backgroundColorLabel;
  24. /// <summary>
  25. /// Demo label.
  26. /// </summary>
  27. private Label demoLabel;
  28. /// <summary>
  29. /// Setup the scenario.
  30. /// </summary>
  31. public override void Setup ()
  32. {
  33. // Scenario Window's.
  34. Win.Title = this.GetName ();
  35. // Foreground ColorPicker.
  36. foregroundColorPicker = new ColorPicker ("Foreground Color");
  37. foregroundColorPicker.ColorChanged += ForegroundColor_ColorChanged;
  38. Win.Add (foregroundColorPicker);
  39. foregroundColorLabel = new Label {
  40. X = Pos.Left (foregroundColorPicker),
  41. Y = Pos.Bottom (foregroundColorPicker) + 1
  42. };
  43. Win.Add (foregroundColorLabel);
  44. // Background ColorPicker.
  45. backgroundColorPicker = new ColorPicker ("Background Color");
  46. backgroundColorPicker.X = Pos.AnchorEnd () - (Pos.Right (backgroundColorPicker) - Pos.Left (backgroundColorPicker));
  47. backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
  48. Win.Add (backgroundColorPicker);
  49. backgroundColorLabel = new Label ();
  50. backgroundColorLabel.X = Pos.AnchorEnd () - (Pos.Right (backgroundColorLabel) - Pos.Left (backgroundColorLabel));
  51. backgroundColorLabel.Y = Pos.Bottom (backgroundColorPicker) + 1;
  52. Win.Add (backgroundColorLabel);
  53. // Demo Label.
  54. demoLabel = new Label ("Lorem Ipsum");
  55. demoLabel.X = Pos.Center ();
  56. demoLabel.Y = 1;
  57. Win.Add (demoLabel);
  58. // Set default colors.
  59. backgroundColorPicker.SelectedColor = demoLabel.SuperView.ColorScheme.Normal.Background;
  60. foregroundColorPicker.SelectedColor = demoLabel.SuperView.ColorScheme.Normal.Foreground;
  61. }
  62. /// <summary>
  63. /// Fired when foreground color is changed.
  64. /// </summary>
  65. private void ForegroundColor_ColorChanged (object sender, EventArgs e)
  66. {
  67. UpdateColorLabel (foregroundColorLabel, foregroundColorPicker);
  68. UpdateDemoLabel ();
  69. }
  70. /// <summary>
  71. /// Fired when background color is changed.
  72. /// </summary>
  73. private void BackgroundColor_ColorChanged (object sender, EventArgs e)
  74. {
  75. UpdateColorLabel (backgroundColorLabel, backgroundColorPicker);
  76. UpdateDemoLabel ();
  77. }
  78. /// <summary>
  79. /// Update a color label from his ColorPicker.
  80. /// </summary>
  81. private void UpdateColorLabel (Label label, ColorPicker colorPicker)
  82. {
  83. label.Clear ();
  84. label.Text = $"{colorPicker.SelectedColor} - {(int)colorPicker.SelectedColor}";
  85. }
  86. /// <summary>
  87. /// Update Demo Label.
  88. /// </summary>
  89. private void UpdateDemoLabel () => demoLabel.ColorScheme = new ColorScheme () {
  90. Normal = new Terminal.Gui.Attribute (foregroundColorPicker.SelectedColor, backgroundColorPicker.SelectedColor)
  91. };
  92. }
  93. }