ColorPicker.cs 3.7 KB

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