-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinearInterpolationConverter.cs
61 lines (59 loc) · 2.74 KB
/
LinearInterpolationConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Globalization;
using System.Windows.Data;
namespace Sharp.Utils.Wpf
{
/// <summary>
/// Calculates a <see cref="System.Double"/> value by linearly interpolating between two numeric values.
/// This value converter requires 3 input values. The first two inputs are the starting and ending values,
/// and the third input is the scale (0-1). A scale of less than 0 or greater than 1 results in a value
/// outside of the first two input values.
/// </summary>
public class LinearInterpolationConverter : OneWayMultiValueConverter
{
/// <summary>
/// Converts source values to a value for the binding target. The data binding
/// engine calls this method when it propagates the values from source bindings
/// to the binding target.
/// </summary>
/// <param name="values">
/// The values produced by the binding source. The first two inputs are the starting and ending values,
/// and the third input is the scale (0-1). A scale of less than 0 or greater than 1 results in a value
/// outside of the first two input values.
/// </param>
/// <param name="targetType">The type of the binding target property.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>The converted value.</returns>
public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length != 3)
throw new ArgumentException("LinearInterpolationConverter must have 3 input values.", "values");
var value1 = GetNumericValue(values[0]);
var value2 = GetNumericValue(values[1]);
var scale = GetNumericValue(values[2]);
var output = value1 + (value2 - value1) * scale;
return output;
}
private static double GetNumericValue(object obj)
{
if (obj is double)
return (double)obj;
if (obj is int)
return (double)(int)obj;
if (obj is long)
return (double)(long)obj;
if (obj is short)
return (double)(short)obj;
if (obj is float)
return (double)(float)obj;
if (obj is byte)
return (double)(byte)obj;
if (obj is string)
return double.Parse((string)obj);
if (obj == null)
throw new ArgumentNullException("obj");
throw new NotSupportedException(string.Concat("The type '", obj.GetType(), "' is not a supported numeric type."));
}
}
}