using System.Globalization; using System.Windows; using System.Windows.Data; using System.Windows.Markup; namespace Qrakhen.TilingFrames.Converters { 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) return new GridLength(0, GridUnitType.Star); if (ratio <= .5) return new GridLength(1, GridUnitType.Star); else if (ratio >= 1) return new GridLength(1, GridUnitType.Star); 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(); } }