ColorPicker.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 View _demoView;
  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 () {
  37. Title = "Foreground Color",
  38. X = 0,
  39. Y = 0,
  40. BoxHeight = 3,
  41. BoxWidth = 6,
  42. BorderStyle = LineStyle.Single
  43. };
  44. foregroundColorPicker.ColorChanged += ForegroundColor_ColorChanged;
  45. Win.Add (foregroundColorPicker);
  46. _foregroundColorLabel = new Label {
  47. X = Pos.Left (foregroundColorPicker),
  48. Y = Pos.Bottom (foregroundColorPicker) + 1
  49. };
  50. Win.Add (_foregroundColorLabel);
  51. // Background ColorPicker.
  52. backgroundColorPicker = new ColorPicker () {
  53. Title = "Background Color",
  54. Y = 0,
  55. BoxHeight = 1,
  56. BoxWidth = 4,
  57. BorderStyle = LineStyle.Single
  58. };
  59. backgroundColorPicker.X = Pos.AnchorEnd () - (Pos.Right (backgroundColorPicker) - Pos.Left (backgroundColorPicker));
  60. backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
  61. Win.Add (backgroundColorPicker);
  62. _backgroundColorLabel = new Label ();
  63. _backgroundColorLabel.X = Pos.AnchorEnd () - (Pos.Right (_backgroundColorLabel) - Pos.Left (_backgroundColorLabel));
  64. _backgroundColorLabel.Y = Pos.Bottom (backgroundColorPicker) + 1;
  65. Win.Add (_backgroundColorLabel);
  66. // Demo Label.
  67. _demoView = new View () {
  68. Title = "Color Sample",
  69. Text = "Lorem Ipsum",
  70. TextAlignment = TextAlignment.Centered,
  71. VerticalTextAlignment = VerticalTextAlignment.Middle,
  72. BorderStyle = LineStyle.Heavy,
  73. X = Pos.Center (),
  74. Y = Pos.Center (),
  75. Height = 5,
  76. Width = 20
  77. };
  78. Win.Add (_demoView);
  79. // Set default colors.
  80. backgroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Background;
  81. foregroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Foreground;
  82. }
  83. /// <summary>
  84. /// Fired when foreground color is changed.
  85. /// </summary>
  86. private void ForegroundColor_ColorChanged (object sender, EventArgs e)
  87. {
  88. UpdateColorLabel (_foregroundColorLabel, foregroundColorPicker);
  89. UpdateDemoLabel ();
  90. }
  91. /// <summary>
  92. /// Fired when background color is changed.
  93. /// </summary>
  94. private void BackgroundColor_ColorChanged (object sender, EventArgs e)
  95. {
  96. UpdateColorLabel (_backgroundColorLabel, backgroundColorPicker);
  97. UpdateDemoLabel ();
  98. }
  99. /// <summary>
  100. /// Update a color label from his ColorPicker.
  101. /// </summary>
  102. private void UpdateColorLabel (Label label, ColorPicker colorPicker)
  103. {
  104. label.Clear ();
  105. label.Text = $"{colorPicker.SelectedColor} - {(int)colorPicker.SelectedColor}";
  106. }
  107. /// <summary>
  108. /// Update Demo Label.
  109. /// </summary>
  110. private void UpdateDemoLabel () => _demoView.ColorScheme = new ColorScheme () {
  111. Normal = new Terminal.Gui.Attribute (foregroundColorPicker.SelectedColor, backgroundColorPicker.SelectedColor)
  112. };
  113. }
  114. }