ColorPicker.cs 8.2 KB

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