using System;
using System.Globalization;
using Avalonia.Data.Converters;
using Avalonia.Media;
namespace Avalonia.PropertyGrid.Controls;
///
/// Converts an Avalonia.Media.Color to a SolidColorBrush
///
public sealed class ColorToBrushConverter : IValueConverter
{
///
/// Converts a Color to a SolidColorBrush.
///
/// The Color value to convert.
/// The target type.
/// Optional parameter.
/// Culture information.
/// A SolidColorBrush with the specified color.
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is Color color)
{
return new SolidColorBrush(color);
}
// Return transparent brush if not a color
return new SolidColorBrush(Colors.Transparent);
}
///
/// Converts a SolidColorBrush back to a Color.
///
/// The SolidColorBrush value to convert back.
/// The target type.
/// Optional parameter.
/// Culture information.
/// The Color from the brush.
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is SolidColorBrush brush)
{
return brush.Color;
}
return Colors.Transparent;
}
}