GradientFill.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Terminal.Gui.TextEffects;
  8. /// <summary>
  9. /// Implementation of <see cref="IFill"/> that uses a color gradient (including
  10. /// radial, diagonal etc).
  11. /// </summary>
  12. public class GradientFill : IFill
  13. {
  14. private Dictionary<Point, Terminal.Gui.Color> _map;
  15. public GradientFill (Rectangle area, Gradient gradient, Gradient.Direction direction)
  16. {
  17. _map =
  18. gradient.BuildCoordinateColorMapping (area.Height, area.Width, direction)
  19. .ToDictionary (
  20. (k) => new Point (k.Key.Column, k.Key.Row),
  21. (v) => new Terminal.Gui.Color (v.Value.R, v.Value.G, v.Value.B));
  22. }
  23. public Terminal.Gui.Color GetColor (Point point)
  24. {
  25. if (_map.TryGetValue (point, out var color))
  26. {
  27. return color;
  28. }
  29. return new Terminal.Gui.Color (0, 0, 0); // Default to black if point not found
  30. }
  31. }