qamp/Qrakhen.TilingFrames/Converters/AlphaRatioConverter.cs

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="BetaRatioConverter"/>.
/// </summary>
public class AlphaRatioConverter : 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 <= double.Epsilon)
{
// If a ratio near zero, alpha is essentially hidden.
return new GridLength(0, GridUnitType.Star);
}
if (ratio <= .5)
{
// With a ratio less than or equal to .5, alpha will be calculated as the minor component
return new GridLength(1, GridUnitType.Star);
}
if (ratio >= 1)
{
// With a ratio near 1, alpha will be 1.
return new GridLength(1, GridUnitType.Star);
}
// Returns the parts-per-ratio that alpha 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();
}