IntToColorConverter.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using Xamarin.Forms;
  5. namespace InABox.Mobile
  6. {
  7. public class IntToColorConverterThreshold
  8. {
  9. public int Lower { get; set; }
  10. public int Upper { get; set; }
  11. public Color Color { get; set; }
  12. }
  13. public class IntToColorConverter: AbstractConverter<int,Color>
  14. {
  15. public IList<IntToColorConverterThreshold> Thresholds { get; private set; }
  16. public Color DefaultColor { get; set; }
  17. public IntToColorConverter()
  18. {
  19. Thresholds = new ObservableCollection<IntToColorConverterThreshold>();
  20. }
  21. protected override Color Convert(int value, object? parameter = null)
  22. {
  23. foreach (var threshold in Thresholds.OrderBy(x=>x.Lower))
  24. {
  25. if (value >= threshold.Lower && value <= threshold.Upper)
  26. return threshold.Color;
  27. }
  28. return DefaultColor;
  29. }
  30. }
  31. }