agrobot_base/AgroBase/OperationControl/Converters/MultiplyConverter.cs

128 lines
5.2 KiB
C#

using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
using Color = System.Windows.Media.Color;
using ColorConverter = System.Windows.Media.ColorConverter;
using Brushes = System.Windows.Media.Brushes;
using Brush = System.Windows.Media.Brush;
using Binding = System.Windows.Data.Binding;
namespace OperationControl.Converters
{
public class MultiplyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return 0;
double v = System.Convert.ToDouble(value, CultureInfo.InvariantCulture);
double k = System.Convert.ToDouble(parameter ?? 1, CultureInfo.InvariantCulture);
return v * k;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
public class TempValueToWidthConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 4) return 0d;
double v = values[0] is double dv ? dv : 0;
double min = values[1] is double dmin ? dmin : 0;
double max = values[2] is double dmax ? dmax : 100;
double total = values[3] is double tw ? tw : 0;
if (total <= 0) return 0d;
v = Math.Max(min, Math.Min(max, v));
double t = (v - min) / Math.Max(1e-6, (max - min));
return Math.Max(0, t * total);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
=> throw new NotImplementedException();
}
/// <summary>
/// (value, warnStart, critStart) -> Brush
/// </summary>
public class TempRangeToBrushConverter : IMultiValueConverter
{
static readonly SolidColorBrush Ok = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2ECC71"));
static readonly SolidColorBrush Warn = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F1C40F"));
static readonly SolidColorBrush Crit = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E74C3C"));
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 3) return Ok;
double v = values[0] is double dv ? dv : 0;
double warn = values[1] is double dw ? dw : 50;
double crit = values[2] is double dc ? dc : 55;
if (v >= crit) return Crit;
if (v >= warn) return Warn;
return Ok;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
=> throw new NotImplementedException();
}
public class DifHeadingToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double d = value is double dd ? Math.Abs(dd) : 0;
if (d < 4) return Brushes.White; // alinhado
if (d < 10) return Brushes.Gold; // começando a desviar
return Brushes.Red; // desvio forte
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> value;
}
public class BooleanToBrushConverter : System.Windows.Data.IValueConverter
{
public Brush OnBrush { get; set; } = new SolidColorBrush(Color.FromRgb(0x2E, 0xCC, 0x71));
public Brush OffBrush { get; set; } = new SolidColorBrush(Color.FromRgb(0xE7, 0x4C, 0x3C));
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
=> (value is bool b && b) ? OnBrush : OffBrush;
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
=> Binding.DoNothing;
}
public class ArmedToTextConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
=> (value is bool b && b) ? "ARMADO" : "DESARMADO";
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
=> Binding.DoNothing;
}
public class LateralErrorToOffsetConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType,
object parameter, CultureInfo culture)
{
if (values.Length < 2) return 0;
double width = System.Convert.ToDouble(values[0], culture);
double errorN = System.Convert.ToDouble(values[1], culture);
// limite visual: 40% da meia largura
double maxFactor = 0.4;
return errorN * (width * 0.5 * maxFactor);
}
public object[] ConvertBack(object value, Type[] targetTypes,
object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
}