ColorPicker.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #nullable enable
  2. using System;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// True color picker using HSL
  6. /// </summary>
  7. public partial 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. tfValue.Accepting += (s, _)=>UpdateSingleBarValueFromTextField(s);
  56. _textFields.Add (bar, tfValue);
  57. }
  58. y++;
  59. bar.ValueChanged += RebuildColorFromBar;
  60. _bars.Add (bar);
  61. Add (bar);
  62. if (tfValue is { })
  63. {
  64. Add (tfValue);
  65. }
  66. }
  67. if (Style.ShowColorName)
  68. {
  69. CreateNameField ();
  70. }
  71. CreateTextField ();
  72. SelectedColor = oldValue;
  73. SetLayoutNeeded ();
  74. }
  75. /// <summary>
  76. /// Fired when color is changed.
  77. /// </summary>
  78. public event EventHandler<ColorEventArgs>? ColorChanged;
  79. /// <inheritdoc/>
  80. public override void OnDrawContent (Rectangle viewport)
  81. {
  82. base.OnDrawContent (viewport);
  83. Attribute normal = GetNormalColor ();
  84. Driver?.SetAttribute (new (SelectedColor, normal.Background));
  85. int y = _bars.Count + (Style.ShowColorName ? 1 : 0);
  86. AddRune (13, y, (Rune)'■');
  87. }
  88. /// <summary>
  89. /// The color selected in the picker
  90. /// </summary>
  91. public Color SelectedColor
  92. {
  93. get => _selectedColor;
  94. set => SetSelectedColor (value, true);
  95. }
  96. /// <summary>
  97. /// Style settings for the color picker. After making changes ensure you call
  98. /// <see cref="ApplyStyleChanges"/>.
  99. /// </summary>
  100. public ColorPickerStyle Style { get; set; } = new ();
  101. private void CreateNameField ()
  102. {
  103. _lbName = new ()
  104. {
  105. Text = "Name:",
  106. X = 0,
  107. Y = 3
  108. };
  109. _tfName = new ()
  110. {
  111. Y = 3,
  112. X = 6,
  113. Width = 20 // width of "LightGoldenRodYellow" - the longest w3c color name
  114. };
  115. Add (_lbName);
  116. Add (_tfName);
  117. var auto = new AppendAutocomplete (_tfName);
  118. auto.SuggestionGenerator = new SingleWordSuggestionGenerator
  119. {
  120. AllSuggestions = _colorNameResolver.GetColorNames ().ToList ()
  121. };
  122. _tfName.Autocomplete = auto;
  123. _tfName.HasFocusChanged += UpdateValueFromName;
  124. _tfName.Accepting += (s, _) => UpdateValueFromName ();
  125. }
  126. private void CreateTextField ()
  127. {
  128. int y = _bars.Count;
  129. if (Style.ShowColorName)
  130. {
  131. y++;
  132. }
  133. _lbHex = new ()
  134. {
  135. Text = "Hex:",
  136. X = 0,
  137. Y = y
  138. };
  139. _tfHex = new ()
  140. {
  141. Y = y,
  142. X = 4,
  143. Width = 8,
  144. };
  145. Add (_lbHex);
  146. Add (_tfHex);
  147. _tfHex.HasFocusChanged += UpdateValueFromTextField;
  148. _tfHex.Accepting += (_,_)=> UpdateValueFromTextField();
  149. }
  150. private void DisposeOldViews ()
  151. {
  152. foreach (ColorBar bar in _bars.Cast<ColorBar> ())
  153. {
  154. bar.ValueChanged -= RebuildColorFromBar;
  155. if (_textFields.TryGetValue (bar, out TextField? tf))
  156. {
  157. Remove (tf);
  158. tf.Dispose ();
  159. }
  160. Remove (bar);
  161. bar.Dispose ();
  162. }
  163. _bars = new ();
  164. _textFields.Clear ();
  165. if (_lbHex != null)
  166. {
  167. Remove (_lbHex);
  168. _lbHex.Dispose ();
  169. _lbHex = null;
  170. }
  171. if (_tfHex != null)
  172. {
  173. Remove (_tfHex);
  174. _tfHex.Dispose ();
  175. _tfHex = null;
  176. }
  177. if (_lbName != null)
  178. {
  179. Remove (_lbName);
  180. _lbName.Dispose ();
  181. _lbName = null;
  182. }
  183. if (_tfName != null)
  184. {
  185. Remove (_tfName);
  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. SetLayoutNeeded ();
  223. }
  224. private void UpdateSingleBarValueFromTextField (object? sender, HasFocusEventArgs e)
  225. {
  226. // if the new value of Focused is true then it is an enter event so ignore
  227. if (e.NewValue)
  228. {
  229. return;
  230. }
  231. // it is a leave event so update
  232. UpdateSingleBarValueFromTextField (sender);
  233. }
  234. private void UpdateSingleBarValueFromTextField (object? sender)
  235. {
  236. foreach (KeyValuePair<IColorBar, TextField> kvp in _textFields)
  237. {
  238. if (kvp.Value == sender)
  239. {
  240. if (int.TryParse (kvp.Value.Text, out int v))
  241. {
  242. kvp.Key.Value = v;
  243. }
  244. }
  245. }
  246. }
  247. private void UpdateValueFromName (object? sender, HasFocusEventArgs e)
  248. {
  249. // if the new value of Focused is true then it is an enter event so ignore
  250. if (e.NewValue)
  251. {
  252. return;
  253. }
  254. // it is a leave event so update
  255. UpdateValueFromName();
  256. }
  257. private void UpdateValueFromName ()
  258. {
  259. if (_tfName == null)
  260. {
  261. return;
  262. }
  263. if (_colorNameResolver.TryParseColor (_tfName.Text, out Color newColor))
  264. {
  265. SelectedColor = newColor;
  266. }
  267. else
  268. {
  269. // value is invalid, revert the value in the text field back to current state
  270. SyncSubViewValues (false);
  271. }
  272. }
  273. private void UpdateValueFromTextField (object? sender, HasFocusEventArgs e)
  274. {
  275. // if the new value of Focused is true then it is an enter event so ignore
  276. if (e.NewValue)
  277. {
  278. return;
  279. }
  280. // it is a leave event so update
  281. UpdateValueFromTextField ();
  282. }
  283. private void UpdateValueFromTextField ()
  284. {
  285. if (_tfHex == null)
  286. {
  287. return;
  288. }
  289. if (Color.TryParse (_tfHex.Text, out Color? newColor))
  290. {
  291. SelectedColor = newColor.Value;
  292. }
  293. else
  294. {
  295. // value is invalid, revert the value in the text field back to current state
  296. SyncSubViewValues (false);
  297. }
  298. }
  299. /// <inheritdoc />
  300. protected override void Dispose (bool disposing)
  301. {
  302. DisposeOldViews ();
  303. base.Dispose (disposing);
  304. }
  305. }