Files
ShrlAlgoToolkit/WPFluent/Converters/HsbToColorConverter.cs
2025-04-24 20:56:44 +08:00

42 lines
1.2 KiB
C#

using System;
using System.Windows.Data;
using WPFluent.Controls;
using WPFluent.Extensions;
namespace WPFluent.Converters;
/// <summary>
/// Converts HSB color values to SolidColorBrush and vice versa. Supports single and multi-value conversions.
/// </summary>
internal class HsbToColorConverter : IValueConverter, IMultiValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Hsb hsb)
return new SolidColorBrush(hsb.ToColor());
return Binding.DoNothing;
}
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var h = (double)values[0];
var s = (double)values[1];
var b = (double)values[2];
return new SolidColorBrush(new Hsb(h, s, b).ToColor());
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is SolidColorBrush brush)
return brush.Color.ToHsb();
return Binding.DoNothing;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{ throw new NotImplementedException(); }
}