ColorPicker.cs 7.8 KB

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