GradientFill.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 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. }
  21. /// <summary>
  22. /// Returns the color to use for the given <paramref name="point"/> or Black if it
  23. /// lies outside of the prepared gradient area (see constructor).
  24. /// </summary>
  25. /// <param name="point"></param>
  26. /// <returns></returns>
  27. public Color GetColor (Point point)
  28. {
  29. if (_map.TryGetValue (point, out var color))
  30. {
  31. return color;
  32. }
  33. return new Color (0, 0, 0); // Default to black if point not found
  34. }
  35. }