ColorPicker.cs 3.5 KB

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