// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) 2014 OxyPlot contributors
//
//
// Converts between and .
//
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.Avalonia.Converters
{
using global::Avalonia.Data.Converters;
using global::Avalonia.Media;
using System;
using System.Globalization;
///
/// Converts between and .
///
public class OxyColorConverter : IValueConverter
{
///
/// Converts a value.
///
/// The value produced by the binding source.
/// The type of the binding target property.
/// The converter parameter to use.
/// The culture to use in the converter.
/// A converted value. If the method returns null, the valid null value is used.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is OxyColor color)
{
if (targetType == typeof(Color))
{
return color.ToColor();
}
if (targetType == typeof(IBrush))
{
return color.ToBrush();
}
}
return null;
}
///
/// Converts a value.
///
/// The value that is produced by the binding target.
/// The type to convert to.
/// The converter parameter to use.
/// The culture to use in the converter.
/// A converted value. If the method returns null, the valid null value is used.
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType == typeof(OxyColor))
{
if (value is Color color)
{
return OxyColor.FromArgb(color.A, color.R, color.G, color.B);
}
if (value is SolidColorBrush brush)
{
return OxyColor.FromArgb(brush.Color.A, brush.Color.R, brush.Color.G, brush.Color.B);
}
}
return null;
}
}
}