WaveProgressGradientOffsetConverter.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Globalization;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Data.Converters;
  6. using Avalonia.Media;
  7. using Avalonia.Styling;
  8. namespace SukiUI.Converters.WaveProgress;
  9. internal class WaveProgressGradientOffsetConverter : IValueConverter
  10. {
  11. public static readonly WaveProgressGradientOffsetConverter Instance = new();
  12. public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  13. {
  14. if (value is not double v)
  15. return Brushes.Blue;
  16. var primaryColor = (Color)(Application.Current!.FindResource("SukiPrimaryColor") ?? Colors.DodgerBlue);
  17. var accentColor = (Color)(Application.Current!.FindResource("SukiAccentColor") ?? Colors.Transparent);
  18. v /= 100;
  19. v += Application.Current!.RequestedThemeVariant == ThemeVariant.Light ? 0.2 : 0.4;
  20. if (v > 1)
  21. v = 1;
  22. return new LinearGradientBrush()
  23. {
  24. EndPoint = new RelativePoint(0.5, 1, RelativeUnit.Relative),
  25. StartPoint = new RelativePoint(0.5, 0, RelativeUnit.Relative),
  26. GradientStops = new GradientStops()
  27. {
  28. new() { Color = primaryColor, Offset = 0 },
  29. new() { Color = Application.Current.RequestedThemeVariant == ThemeVariant.Light ? Colors.Transparent: accentColor, Offset = v }
  30. }
  31. };
  32. }
  33. public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  34. {
  35. throw new NotSupportedException();
  36. }
  37. }