ColorPicker.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #nullable enable
  2. using System;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// True color picker using HSL
  6. /// </summary>
  7. public class ColorPicker : View
  8. {
  9. /// <summary>
  10. /// Creates a new instance of <see cref="ColorPicker"/>. Use
  11. /// <see cref="Style"/> to change color model. Use <see cref="SelectedColor"/>
  12. /// to change initial <see cref="Color"/>.
  13. /// </summary>
  14. public ColorPicker ()
  15. {
  16. CanFocus = true;
  17. TabStop = TabBehavior.TabStop;
  18. Height = Dim.Auto ();
  19. Width = Dim.Auto ();
  20. ApplyStyleChanges ();
  21. }
  22. private readonly Dictionary<IColorBar, TextField> _textFields = new ();
  23. private readonly ColorModelStrategy _strategy = new ();
  24. private TextField? _tfHex;
  25. private Label? _lbHex;
  26. private TextField? _tfName;
  27. private Label? _lbName;
  28. private Color _selectedColor = Color.Black;
  29. // TODO: Add interface
  30. private readonly IColorNameResolver _colorNameResolver = new W3CColors ();
  31. private List<IColorBar> _bars = new ();
  32. /// <summary>
  33. /// Rebuild the user interface to reflect the new state of <see cref="Style"/>.
  34. /// </summary>
  35. public void ApplyStyleChanges ()
  36. {
  37. Color oldValue = _selectedColor;
  38. DisposeOldViews ();
  39. var y = 0;
  40. const int textFieldWidth = 4;
  41. foreach (ColorBar bar in _strategy.CreateBars (Style.ColorModel))
  42. {
  43. bar.Y = y;
  44. bar.Width = Dim.Fill (Style.ShowTextFields ? textFieldWidth : 0);
  45. TextField? tfValue = null;
  46. if (Style.ShowTextFields)
  47. {
  48. tfValue = new TextField
  49. {
  50. X = Pos.AnchorEnd (textFieldWidth),
  51. Y = y,
  52. Width = textFieldWidth
  53. };
  54. tfValue.HasFocusChanged += UpdateSingleBarValueFromTextField;
  55. _textFields.Add (bar, tfValue);
  56. }
  57. y++;
  58. bar.ValueChanged += RebuildColorFromBar;
  59. _bars.Add (bar);
  60. Add (bar);
  61. if (tfValue is { })
  62. {
  63. Add (tfValue);
  64. }
  65. }
  66. if (Style.ShowColorName)
  67. {
  68. CreateNameField ();
  69. }
  70. CreateTextField ();
  71. SelectedColor = oldValue;
  72. LayoutSubviews ();
  73. }
  74. /// <summary>
  75. /// Fired when color is changed.
  76. /// </summary>
  77. public event EventHandler<ColorEventArgs>? ColorChanged;
  78. /// <inheritdoc/>
  79. public override void OnDrawContent (Rectangle viewport)
  80. {
  81. base.OnDrawContent (viewport);
  82. Attribute normal = GetNormalColor ();
  83. Driver.SetAttribute (new (SelectedColor, normal.Background));
  84. int y = _bars.Count + (Style.ShowColorName ? 1 : 0);
  85. AddRune (13, y, (Rune)'■');
  86. }
  87. /// <summary>
  88. /// The color selected in the picker
  89. /// </summary>
  90. public Color SelectedColor
  91. {
  92. get => _selectedColor;
  93. set => SetSelectedColor (value, true);
  94. }
  95. /// <summary>
  96. /// Style settings for the color picker. After making changes ensure you call
  97. /// <see cref="ApplyStyleChanges"/>.
  98. /// </summary>
  99. public ColorPickerStyle Style { get; set; } = new ();
  100. private void CreateNameField ()
  101. {
  102. _lbName = new ()
  103. {
  104. Text = "Name:",
  105. X = 0,
  106. Y = 3
  107. };
  108. _tfName = new ()
  109. {
  110. Y = 3,
  111. X = 6,
  112. Width = 20 // width of "LightGoldenRodYellow" - the longest w3c color name
  113. };
  114. Add (_lbName);
  115. Add (_tfName);
  116. var auto = new AppendAutocomplete (_tfName);
  117. auto.SuggestionGenerator = new SingleWordSuggestionGenerator
  118. {
  119. AllSuggestions = _colorNameResolver.GetColorNames ().ToList ()
  120. };
  121. _tfName.Autocomplete = auto;
  122. _tfName.HasFocusChanged += UpdateValueFromName;
  123. }
  124. private void CreateTextField ()
  125. {
  126. int y = _bars.Count;
  127. if (Style.ShowColorName)
  128. {
  129. y++;
  130. }
  131. _lbHex = new ()
  132. {
  133. Text = "Hex:",
  134. X = 0,
  135. Y = y
  136. };
  137. _tfHex = new ()
  138. {
  139. Y = y,
  140. X = 4,
  141. Width = 8,
  142. };
  143. Add (_lbHex);
  144. Add (_tfHex);
  145. _tfHex.HasFocusChanged += UpdateValueFromTextField;
  146. }
  147. private void DisposeOldViews ()
  148. {
  149. foreach (ColorBar bar in _bars.Cast<ColorBar> ())
  150. {
  151. bar.ValueChanged -= RebuildColorFromBar;
  152. if (_textFields.TryGetValue (bar, out TextField? tf))
  153. {
  154. tf.HasFocusChanged -= UpdateSingleBarValueFromTextField;
  155. Remove (tf);
  156. tf.Dispose ();
  157. }
  158. Remove (bar);
  159. bar.Dispose ();
  160. }
  161. _bars = new ();
  162. _textFields.Clear ();
  163. if (_lbHex != null)
  164. {
  165. Remove (_lbHex);
  166. _lbHex.Dispose ();
  167. _lbHex = null;
  168. }
  169. if (_tfHex != null)
  170. {
  171. Remove (_tfHex);
  172. _tfHex.HasFocusChanged -= UpdateValueFromTextField;
  173. _tfHex.Dispose ();
  174. _tfHex = null;
  175. }
  176. if (_lbName != null)
  177. {
  178. Remove (_lbName);
  179. _lbName.Dispose ();
  180. _lbName = null;
  181. }
  182. if (_tfName != null)
  183. {
  184. Remove (_tfName);
  185. _tfName.HasFocusChanged -= UpdateValueFromName;
  186. _tfName.Dispose ();
  187. _tfName = null;
  188. }
  189. }
  190. private void RebuildColorFromBar (object? sender, EventArgs<int> e) { SetSelectedColor (_strategy.GetColorFromBars (_bars, Style.ColorModel), false); }
  191. private void SetSelectedColor (Color value, bool syncBars)
  192. {
  193. if (_selectedColor != value)
  194. {
  195. Color old = _selectedColor;
  196. _selectedColor = value;
  197. ColorChanged?.Invoke (
  198. this,
  199. new (value));
  200. }
  201. SyncSubViewValues (syncBars);
  202. }
  203. private void SyncSubViewValues (bool syncBars)
  204. {
  205. if (syncBars)
  206. {
  207. _strategy.SetBarsToColor (_bars, _selectedColor, Style.ColorModel);
  208. }
  209. foreach (KeyValuePair<IColorBar, TextField> kvp in _textFields)
  210. {
  211. kvp.Value.Text = kvp.Key.Value.ToString ();
  212. }
  213. var colorHex = _selectedColor.ToString ($"#{SelectedColor.R:X2}{SelectedColor.G:X2}{SelectedColor.B:X2}");
  214. if (_tfName != null)
  215. {
  216. _tfName.Text = _colorNameResolver.TryNameColor (_selectedColor, out string name) ? name : string.Empty;
  217. }
  218. if (_tfHex != null)
  219. {
  220. _tfHex.Text = colorHex;
  221. }
  222. }
  223. private void UpdateSingleBarValueFromTextField (object? sender, HasFocusEventArgs e)
  224. {
  225. if (e.NewValue)
  226. {
  227. return;
  228. }
  229. foreach (KeyValuePair<IColorBar, TextField> kvp in _textFields)
  230. {
  231. if (kvp.Value == sender)
  232. {
  233. if (int.TryParse (kvp.Value.Text, out int v))
  234. {
  235. kvp.Key.Value = v;
  236. }
  237. }
  238. }
  239. }
  240. private void UpdateValueFromName (object? sender, HasFocusEventArgs e)
  241. {
  242. if (e.NewValue)
  243. {
  244. return;
  245. }
  246. if (_tfName == null)
  247. {
  248. return;
  249. }
  250. if (_colorNameResolver.TryParseColor (_tfName.Text, out Color newColor))
  251. {
  252. SelectedColor = newColor;
  253. }
  254. else
  255. {
  256. // value is invalid, revert the value in the text field back to current state
  257. SyncSubViewValues (false);
  258. }
  259. }
  260. private void UpdateValueFromTextField (object? sender, HasFocusEventArgs e)
  261. {
  262. if (e.NewValue)
  263. {
  264. return;
  265. }
  266. if (_tfHex == null)
  267. {
  268. return;
  269. }
  270. if (Color.TryParse (_tfHex.Text, out Color? newColor))
  271. {
  272. SelectedColor = newColor.Value;
  273. }
  274. else
  275. {
  276. // value is invalid, revert the value in the text field back to current state
  277. SyncSubViewValues (false);
  278. }
  279. }
  280. protected override void Dispose (bool disposing)
  281. {
  282. DisposeOldViews ();
  283. base.Dispose (disposing);
  284. }
  285. }