diff --git a/src/client/spark/view/Converters/NumericSignBrushConverter.cs b/src/client/spark/view/Converters/NumericSignBrushConverter.cs index f3e434d75..fb37c307c 100644 --- a/src/client/spark/view/Converters/NumericSignBrushConverter.cs +++ b/src/client/spark/view/Converters/NumericSignBrushConverter.cs @@ -4,28 +4,48 @@ using System.Windows.Data; using System.Windows.Media; namespace mbts.ui.converters { - public class NumericSignBrushConverter : IValueConverter { - static readonly SolidColorBrush _error_brush = new SolidColorBrush(Colors.DimGray); + struct SignedDataStore { + public readonly T positive, neutral, negative, error; - SolidColorBrush _negative_brush = new SolidColorBrush(Colors.Red), - _neutral_brush = new SolidColorBrush(Colors.Black), - _positive_brush = new SolidColorBrush(Colors.Green); + public SignedDataStore(T positive, T neutral, T negative, T error) { + this.positive = positive; + this.neutral = neutral; + this.negative = negative; + this.error = error; + } + } - static NumericSignBrushConverter() => - _error_brush.Freeze(); + static class SignedDataStoreExtensions { + public static T get(this SignedDataStore store, double value) { + if (double.IsNaN(value)) return store.error; + else if (value > 0) return store.positive; + else if (value < 0) return store.negative; + else return store.neutral; + } - public Color PositiveColor { get => _positive_brush.Color; set => (_positive_brush = new SolidColorBrush(value) { Opacity = _opacity }).Freeze(); } - public Color NeutralColor { get => _neutral_brush.Color; set => (_neutral_brush = new SolidColorBrush(value) { Opacity = _opacity }).Freeze(); } - public Color NegativeColor { get => _negative_brush.Color; set => (_negative_brush = new SolidColorBrush(value) { Opacity = _opacity }).Freeze(); } + public static SignedDataStore with_positive(this SignedDataStore store, T positive) => new SignedDataStore(positive, store.neutral, store.negative, store.error); + public static SignedDataStore with_neutral(this SignedDataStore store, T neutral) => new SignedDataStore(store.positive, neutral, store.negative, store.error); + public static SignedDataStore with_negative(this SignedDataStore store, T negative) => new SignedDataStore(store.positive, store.neutral, negative, store.error); + } - private double _opacity = 1.0; - public Double Opacity { - get { return _opacity; } + class NumericSignBrushConverter : IValueConverter { + SignedDataStore _brushes = new SignedDataStore( + Brushes.Green, Brushes.Black, Brushes.Red, Brushes.DimGray); + + public Color PositiveColor { get => _brushes.positive.Color; set => _brushes = _brushes.with_positive(new SolidColorBrush(value) { Opacity = _opacity }.frozen()); } + public Color NeutralColor { get => _brushes.neutral.Color; set => _brushes = _brushes.with_neutral(new SolidColorBrush(value) { Opacity = _opacity }.frozen()); } + public Color NegativeColor { get => _brushes.negative.Color; set => _brushes = _brushes.with_negative(new SolidColorBrush(value) { Opacity = _opacity }.frozen()); } + + double _opacity = 1.0; + public double Opacity { + get => _opacity; set { _opacity = value; - (_positive_brush = new SolidColorBrush(_positive_brush.Color) { Opacity = _opacity }).Freeze(); - (_negative_brush = new SolidColorBrush(_negative_brush.Color) { Opacity = _opacity }).Freeze(); - (_neutral_brush = new SolidColorBrush(_neutral_brush.Color) { Opacity = _opacity }).Freeze(); + _brushes = new SignedDataStore( + _brushes.positive.with(value).frozen(), + _brushes.positive.with(value).frozen(), + _brushes.positive.with(value).frozen(), + _brushes.error.with(value).frozen()); } } public IComparable Zero { get; set; } = 0.0; @@ -33,9 +53,9 @@ namespace mbts.ui.converters { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var comparable = value as IComparable; if (comparable == null || comparable is double && double.IsNaN((double)value)) - return _error_brush; + return _brushes.error; int comp = comparable.CompareTo(Zero); - return comp == 0 ? _neutral_brush : comp > 0 ? _positive_brush : _negative_brush; + return comp == 0 ? _brushes.neutral : comp > 0 ? _brushes.positive : _brushes.negative; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => diff --git a/src/client/spark/view/Extensions/WPFExtensions.cs b/src/client/spark/view/Extensions/WPFExtensions.cs index 5aa34a548..0d49fce12 100644 --- a/src/client/spark/view/Extensions/WPFExtensions.cs +++ b/src/client/spark/view/Extensions/WPFExtensions.cs @@ -98,6 +98,11 @@ namespace mbts.ui { return brush; } + public static SolidColorBrush with(this SolidColorBrush brush, double opacity) { + brush.Opacity = opacity; + return brush; + } + public static Pen frozen(this Pen pen) { pen.Freeze(); return pen;