48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using System.Globalization;
|
|
using System.Windows;
|
|
using System.Windows.Data;
|
|
using System.Windows.Markup;
|
|
|
|
namespace CopaData.FileInspector.GUI.TilingPanels.Converters;
|
|
|
|
/// <summary>
|
|
/// Converts a double between 0 and 1 to a star-length representation,
|
|
/// to be used in conjunction with <see cref="AlphaRatioConverter"/>.
|
|
/// </summary>
|
|
public class BetaRatioConverter : MarkupExtension, IValueConverter
|
|
{
|
|
public override object ProvideValue(IServiceProvider serviceProvider) => this;
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value is double ratio)
|
|
{
|
|
if (ratio >= 1)
|
|
{
|
|
// If near 1, beta is essentially hidden.
|
|
return new GridLength(0, GridUnitType.Star);
|
|
}
|
|
|
|
if (ratio >= .5)
|
|
{
|
|
// With a ratio larger than or equal to 0.5, beta is the lesser component of the two.
|
|
return new GridLength(1, GridUnitType.Star);
|
|
}
|
|
|
|
if (ratio <= double.Epsilon)
|
|
{
|
|
// If near zero, beta covers the entire panel.
|
|
return new GridLength(1, GridUnitType.Star);
|
|
}
|
|
|
|
// Return the parts-per-ratio that beta represents.
|
|
return new GridLength((1 - ratio) / ratio, GridUnitType.Star);
|
|
}
|
|
|
|
throw new ArgumentException($"Expected value to be ratio (double), god {value} instead.");
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
=> throw new NotImplementedException();
|
|
}
|