restructure NumericSignBrushConverter

This commit is contained in:
John Colvin committed 2018-10-05 13:36:42 -04:00
1 parent dc8c0df0a4
commit f5d7ea9a47
2 files changed
+43 -18

No files matched your search

@@ -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<T> {
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<T>(this SignedDataStore<T> 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<T> with_positive<T>(this SignedDataStore<T> store, T positive) => new SignedDataStore<T>(positive, store.neutral, store.negative, store.error);
public static SignedDataStore<T> with_neutral<T>(this SignedDataStore<T> store, T neutral) => new SignedDataStore<T>(store.positive, neutral, store.negative, store.error);
public static SignedDataStore<T> with_negative<T>(this SignedDataStore<T> store, T negative) => new SignedDataStore<T>(store.positive, store.neutral, negative, store.error);
}
private double _opacity = 1.0;
public Double Opacity {
get { return _opacity; }
class NumericSignBrushConverter : IValueConverter {
SignedDataStore<SolidColorBrush> _brushes = new SignedDataStore<SolidColorBrush>(
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<SolidColorBrush>(
_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) =>
@@ -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;