ColorPicker.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // Forground ColorPicker.
  36. foregroundColorPicker = new ColorPicker ("Foreground Color");
  37. foregroundColorPicker.X = 0;
  38. foregroundColorPicker.Y = 0;
  39. foregroundColorPicker.ColorChanged += ForegroundColor_ColorChanged;
  40. Win.Add (foregroundColorPicker);
  41. foregroundColorLabel = new Label ();
  42. foregroundColorLabel.X = Pos.Left(foregroundColorPicker);
  43. foregroundColorLabel.Y = Pos.Bottom (foregroundColorPicker) + 1;
  44. Win.Add (foregroundColorLabel);
  45. // Background ColorPicker.
  46. backgroundColorPicker = new ColorPicker ();
  47. backgroundColorPicker.Text = "Background Color";
  48. backgroundColorPicker.X = Pos.AnchorEnd () - (Pos.Right (backgroundColorPicker) - Pos.Left (backgroundColorPicker));
  49. backgroundColorPicker.Y = 0;
  50. backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
  51. Win.Add (backgroundColorPicker);
  52. backgroundColorLabel = new Label ();
  53. backgroundColorLabel.X = Pos.AnchorEnd () - (Pos.Right (backgroundColorLabel) - Pos.Left (backgroundColorLabel));
  54. backgroundColorLabel.Y = Pos.Bottom (backgroundColorPicker) + 1;
  55. Win.Add (backgroundColorLabel);
  56. // Demo Label.
  57. demoLabel = new Label ("Lorem Ipsum");
  58. demoLabel.X = Pos.Center ();
  59. demoLabel.Y = 1;
  60. Win.Add (demoLabel);
  61. // Set default colors.
  62. backgroundColorPicker.SelectedColor = demoLabel.SuperView.ColorScheme.Normal.Background;
  63. foregroundColorPicker.SelectedColor = demoLabel.SuperView.ColorScheme.Normal.Foreground;
  64. }
  65. /// <summary>
  66. /// Fired when foreground color is changed.
  67. /// </summary>
  68. private void ForegroundColor_ColorChanged ()
  69. {
  70. UpdateColorLabel (foregroundColorLabel, foregroundColorPicker);
  71. UpdateDemoLabel ();
  72. }
  73. /// <summary>
  74. /// Fired when background color is changed.
  75. /// </summary>
  76. private void BackgroundColor_ColorChanged ()
  77. {
  78. UpdateColorLabel (backgroundColorLabel, backgroundColorPicker);
  79. UpdateDemoLabel ();
  80. }
  81. /// <summary>
  82. /// Update a color label from his ColorPicker.
  83. /// </summary>
  84. private void UpdateColorLabel (Label label, ColorPicker colorPicker)
  85. {
  86. label.Clear ();
  87. label.Text = $"{colorPicker.SelectedColor} - {(int)colorPicker.SelectedColor}";
  88. }
  89. /// <summary>
  90. /// Update Demo Label.
  91. /// </summary>
  92. private void UpdateDemoLabel () => demoLabel.ColorScheme = new ColorScheme () {
  93. Normal = new Terminal.Gui.Attribute (foregroundColorPicker.SelectedColor, backgroundColorPicker.SelectedColor)
  94. };
  95. }
  96. }