ColorPicker.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 Setup ()
  21. {
  22. // Scenario Window's.
  23. Win.Title = GetName ();
  24. // Foreground ColorPicker.
  25. foregroundColorPicker = new ColorPicker { Title = "Foreground Color", BorderStyle = LineStyle.Single };
  26. foregroundColorPicker.ColorChanged += ForegroundColor_ColorChanged;
  27. Win.Add (foregroundColorPicker);
  28. _foregroundColorLabel = new Label
  29. {
  30. X = Pos.Left (foregroundColorPicker), Y = Pos.Bottom (foregroundColorPicker) + 1
  31. };
  32. Win.Add (_foregroundColorLabel);
  33. // Background ColorPicker.
  34. backgroundColorPicker = new ColorPicker
  35. {
  36. Title = "Background Color",
  37. // TODO: Replace with Pos.AnchorEnd () when #2900 is done
  38. X = Pos.AnchorEnd ((8 * 4) + 2), // 8 box * 4 width + 2 for border
  39. BoxHeight = 1,
  40. BoxWidth = 4,
  41. BorderStyle = LineStyle.Single
  42. };
  43. //backgroundColorPicker.X = Pos.AnchorEnd () - (Pos.Right (backgroundColorPicker) - Pos.Left (backgroundColorPicker));
  44. backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
  45. Win.Add (backgroundColorPicker);
  46. _backgroundColorLabel = new Label ();
  47. _backgroundColorLabel.X =
  48. Pos.AnchorEnd () - (Pos.Right (_backgroundColorLabel) - Pos.Left (_backgroundColorLabel));
  49. _backgroundColorLabel.Y = Pos.Bottom (backgroundColorPicker) + 1;
  50. Win.Add (_backgroundColorLabel);
  51. // Demo Label.
  52. _demoView = new View
  53. {
  54. Title = "Color Sample",
  55. Text = "Lorem Ipsum",
  56. TextAlignment = TextAlignment.Centered,
  57. VerticalTextAlignment = VerticalTextAlignment.Middle,
  58. BorderStyle = LineStyle.Heavy,
  59. X = Pos.Center (),
  60. Y = Pos.Center (),
  61. Height = 5,
  62. Width = 20
  63. };
  64. Win.Add (_demoView);
  65. // Set default colors.
  66. foregroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Foreground.GetClosestNamedColor ();
  67. backgroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Background.GetClosestNamedColor ();
  68. Win.Initialized += (s, e) => Win.LayoutSubviews ();
  69. }
  70. /// <summary>Fired when background color is changed.</summary>
  71. private void BackgroundColor_ColorChanged (object sender, EventArgs e)
  72. {
  73. UpdateColorLabel (_backgroundColorLabel, backgroundColorPicker);
  74. UpdateDemoLabel ();
  75. }
  76. /// <summary>Fired when foreground color is changed.</summary>
  77. private void ForegroundColor_ColorChanged (object sender, EventArgs e)
  78. {
  79. UpdateColorLabel (_foregroundColorLabel, foregroundColorPicker);
  80. UpdateDemoLabel ();
  81. }
  82. /// <summary>Update a color label from his ColorPicker.</summary>
  83. private void UpdateColorLabel (Label label, ColorPicker colorPicker)
  84. {
  85. label.Clear ();
  86. var color = new Color (colorPicker.SelectedColor);
  87. label.Text =
  88. $"{colorPicker.SelectedColor} ({(int)colorPicker.SelectedColor}) #{color.R:X2}{color.G:X2}{color.B:X2}";
  89. }
  90. /// <summary>Update Demo Label.</summary>
  91. private void UpdateDemoLabel ()
  92. {
  93. _demoView.ColorScheme = new ColorScheme
  94. {
  95. Normal = new Attribute (
  96. foregroundColorPicker.SelectedColor,
  97. backgroundColorPicker.SelectedColor
  98. )
  99. };
  100. }
  101. }