GradientFill.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Implementation of <see cref="IFill"/> that uses a color gradient (including
  4. /// radial, diagonal etc.).
  5. /// </summary>
  6. public class GradientFill : IFill
  7. {
  8. private readonly Dictionary<Point, Color> _map;
  9. /// <summary>
  10. /// Creates a new instance of the <see cref="GradientFill"/> class that can return
  11. /// color for any point in the given <paramref name="area"/> using the provided
  12. /// <paramref name="gradient"/> and <paramref name="direction"/>.
  13. /// </summary>
  14. /// <param name="area"></param>
  15. /// <param name="gradient"></param>
  16. /// <param name="direction"></param>
  17. public GradientFill (Rectangle area, Gradient gradient, GradientDirection direction)
  18. {
  19. _map = gradient.BuildCoordinateColorMapping (area.Height - 1, area.Width - 1, direction)
  20. .ToDictionary (
  21. kvp => new Point (kvp.Key.X + area.X, kvp.Key.Y + area.Y),
  22. kvp => kvp.Value);
  23. }
  24. /// <summary>
  25. /// Returns the color to use for the given <paramref name="point"/> or Black if it
  26. /// lies outside the prepared gradient area (see constructor).
  27. /// </summary>
  28. /// <param name="point"></param>
  29. /// <returns></returns>
  30. public Color GetColor (Point point)
  31. {
  32. if (_map.TryGetValue (point, out Color color))
  33. {
  34. return color;
  35. }
  36. return new (0, 0); // Default to black if point not found
  37. }
  38. }