33 lines
1.1 KiB
C#
33 lines
1.1 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Windows.Data;
|
|
using System.Windows.Media;
|
|
|
|
namespace HC_APTBS.Converters
|
|
{
|
|
/// <summary>
|
|
/// Converts a CSS-style hex colour string (e.g. "#26C200") to a
|
|
/// <see cref="SolidColorBrush"/> for use in WPF bindings.
|
|
/// Returns <see cref="Brushes.Transparent"/> for null or invalid input.
|
|
/// </summary>
|
|
[ValueConversion(typeof(string), typeof(SolidColorBrush))]
|
|
public sealed class HexColorToBrushConverter : IValueConverter
|
|
{
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value is string hex && !string.IsNullOrWhiteSpace(hex))
|
|
{
|
|
try
|
|
{
|
|
return new SolidColorBrush((Color)ColorConverter.ConvertFromString(hex));
|
|
}
|
|
catch { /* fall through */ }
|
|
}
|
|
return Brushes.Transparent;
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
=> throw new NotSupportedException();
|
|
}
|
|
}
|