ColorPicker.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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>Background ColorPicker.</summary>
  20. private ColorPicker16 backgroundColorPicker16;
  21. /// <summary>Foreground ColorPicker.</summary>
  22. private ColorPicker16 foregroundColorPicker16;
  23. /// <summary>Setup the scenario.</summary>
  24. public override void Main ()
  25. {
  26. Application.Init ();
  27. Window app = new ()
  28. {
  29. Title = GetQuitKeyAndName (),
  30. };
  31. ///////////////////////////////////////
  32. // True Color Pickers
  33. ///////////////////////////////////////
  34. // Foreground ColorPicker.
  35. foregroundColorPicker = new ColorPicker {
  36. Title = "_Foreground Color",
  37. BorderStyle = LineStyle.Single,
  38. Width = Dim.Percent (50)
  39. };
  40. foregroundColorPicker.ColorChanged += ForegroundColor_ColorChanged;
  41. app.Add (foregroundColorPicker);
  42. _foregroundColorLabel = new Label
  43. {
  44. X = Pos.Left (foregroundColorPicker), Y = Pos.Bottom (foregroundColorPicker) + 1
  45. };
  46. app.Add (_foregroundColorLabel);
  47. // Background ColorPicker.
  48. backgroundColorPicker = new ColorPicker
  49. {
  50. Title = "_Background Color",
  51. X = Pos.AnchorEnd (),
  52. Width = Dim.Percent (50),
  53. BorderStyle = LineStyle.Single
  54. };
  55. backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
  56. app.Add (backgroundColorPicker);
  57. _backgroundColorLabel = new Label ()
  58. {
  59. X = Pos.AnchorEnd (),
  60. Y = Pos.Bottom (backgroundColorPicker) + 1
  61. };
  62. app.Add (_backgroundColorLabel);
  63. ///////////////////////////////////////
  64. // 16 Color Pickers
  65. ///////////////////////////////////////
  66. // Foreground ColorPicker 16.
  67. foregroundColorPicker16 = new ColorPicker16
  68. {
  69. Title = "_Foreground Color",
  70. BorderStyle = LineStyle.Single,
  71. Width = Dim.Percent (50),
  72. Visible = false // We default to HSV so hide old one
  73. };
  74. foregroundColorPicker16.ColorChanged += ForegroundColor_ColorChanged;
  75. app.Add (foregroundColorPicker16);
  76. // Background ColorPicker 16.
  77. backgroundColorPicker16 = new ColorPicker16
  78. {
  79. Title = "_Background Color",
  80. X = Pos.AnchorEnd (),
  81. Width = Dim.Percent (50),
  82. BorderStyle = LineStyle.Single,
  83. Visible = false // We default to HSV so hide old one
  84. };
  85. backgroundColorPicker16.ColorChanged += BackgroundColor_ColorChanged;
  86. app.Add (backgroundColorPicker16);
  87. // Demo Label.
  88. _demoView = new View
  89. {
  90. Title = "Color Sample",
  91. Text = "Lorem Ipsum",
  92. TextAlignment = Alignment.Center,
  93. VerticalTextAlignment = Alignment.Center,
  94. BorderStyle = LineStyle.Heavy,
  95. X = Pos.Center (),
  96. Y = Pos.Center (),
  97. Height = 5,
  98. Width = 20
  99. };
  100. app.Add (_demoView);
  101. // Radio for switching color models
  102. var rgColorModel = new RadioGroup ()
  103. {
  104. Y = Pos.Bottom (_demoView),
  105. Width = Dim.Auto (),
  106. Height = Dim.Auto (),
  107. RadioLabels = new []
  108. {
  109. "_RGB",
  110. "_HSV",
  111. "H_SL",
  112. "_16 Colors"
  113. },
  114. SelectedItem = (int)foregroundColorPicker.Style.ColorModel,
  115. };
  116. rgColorModel.SelectedItemChanged += (_, e) =>
  117. {
  118. // 16 colors
  119. if (e.SelectedItem == 3)
  120. {
  121. foregroundColorPicker16.Visible = true;
  122. foregroundColorPicker.Visible = false;
  123. backgroundColorPicker16.Visible = true;
  124. backgroundColorPicker.Visible = false;
  125. // Switching to 16 colors
  126. ForegroundColor_ColorChanged (null,null);
  127. BackgroundColor_ColorChanged (null, null);
  128. }
  129. else
  130. {
  131. foregroundColorPicker16.Visible = false;
  132. foregroundColorPicker.Visible = true;
  133. foregroundColorPicker.Style.ColorModel = (ColorModel)e.SelectedItem;
  134. foregroundColorPicker.ApplyStyleChanges ();
  135. backgroundColorPicker16.Visible = false;
  136. backgroundColorPicker.Visible = true;
  137. backgroundColorPicker.Style.ColorModel = (ColorModel)e.SelectedItem;
  138. backgroundColorPicker.ApplyStyleChanges ();
  139. // Switching to true colors
  140. foregroundColorPicker.SelectedColor = foregroundColorPicker16.SelectedColor;
  141. backgroundColorPicker.SelectedColor = backgroundColorPicker16.SelectedColor;
  142. }
  143. };
  144. app.Add (rgColorModel);
  145. // Checkbox for switching show text fields on and off
  146. var cbShowTextFields = new CheckBox ()
  147. {
  148. Text = "Show _Text Fields",
  149. Y = Pos.Bottom (rgColorModel)+1,
  150. Width = Dim.Auto (),
  151. Height = Dim.Auto (),
  152. CheckedState = foregroundColorPicker.Style.ShowTextFields ? CheckState.Checked: CheckState.UnChecked,
  153. };
  154. cbShowTextFields.CheckedStateChanging += (_, e) =>
  155. {
  156. foregroundColorPicker.Style.ShowTextFields = e.NewValue == CheckState.Checked;
  157. foregroundColorPicker.ApplyStyleChanges ();
  158. backgroundColorPicker.Style.ShowTextFields = e.NewValue == CheckState.Checked;
  159. backgroundColorPicker.ApplyStyleChanges ();
  160. };
  161. app.Add (cbShowTextFields);
  162. // Checkbox for switching show text fields on and off
  163. var cbShowName = new CheckBox ()
  164. {
  165. Text = "Show Color _Name",
  166. Y = Pos.Bottom (cbShowTextFields) + 1,
  167. Width = Dim.Auto (),
  168. Height = Dim.Auto (),
  169. CheckedState = foregroundColorPicker.Style.ShowColorName ? CheckState.Checked : CheckState.UnChecked,
  170. };
  171. cbShowName.CheckedStateChanging += (_, e) =>
  172. {
  173. foregroundColorPicker.Style.ShowColorName = e.NewValue == CheckState.Checked;
  174. foregroundColorPicker.ApplyStyleChanges ();
  175. backgroundColorPicker.Style.ShowColorName = e.NewValue == CheckState.Checked;
  176. backgroundColorPicker.ApplyStyleChanges ();
  177. };
  178. app.Add (cbShowName);
  179. // Set default colors.
  180. foregroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Foreground.GetClosestNamedColor16 ();
  181. backgroundColorPicker.SelectedColor = _demoView.SuperView.ColorScheme.Normal.Background.GetClosestNamedColor16 ();
  182. Application.Run (app);
  183. app.Dispose ();
  184. Application.Shutdown ();
  185. }
  186. /// <summary>Fired when background color is changed.</summary>
  187. private void BackgroundColor_ColorChanged (object sender, EventArgs e)
  188. {
  189. UpdateColorLabel (_backgroundColorLabel,
  190. backgroundColorPicker.Visible ?
  191. backgroundColorPicker.SelectedColor :
  192. backgroundColorPicker16.SelectedColor
  193. );
  194. UpdateDemoLabel ();
  195. }
  196. /// <summary>Fired when foreground color is changed.</summary>
  197. private void ForegroundColor_ColorChanged (object sender, EventArgs e)
  198. {
  199. UpdateColorLabel (_foregroundColorLabel,
  200. foregroundColorPicker.Visible ?
  201. foregroundColorPicker.SelectedColor :
  202. foregroundColorPicker16.SelectedColor
  203. );
  204. UpdateDemoLabel ();
  205. }
  206. /// <summary>Update a color label from his ColorPicker.</summary>
  207. private void UpdateColorLabel (Label label, Color color)
  208. {
  209. label.ClearViewport ();
  210. label.Text =
  211. $"{color} ({(int)color}) #{color.R:X2}{color.G:X2}{color.B:X2}";
  212. }
  213. /// <summary>Update Demo Label.</summary>
  214. private void UpdateDemoLabel ()
  215. {
  216. _demoView.ColorScheme = new ColorScheme
  217. {
  218. Normal = new Attribute (
  219. foregroundColorPicker.Visible ?
  220. foregroundColorPicker.SelectedColor :
  221. foregroundColorPicker16.SelectedColor,
  222. backgroundColorPicker.Visible ?
  223. backgroundColorPicker.SelectedColor :
  224. backgroundColorPicker16.SelectedColor
  225. )
  226. };
  227. }
  228. }