1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- // --------------------------------------------------------------------------------------------------------------------
- // <copyright file="OxyColorConverter.cs" company="OxyPlot">
- // Copyright (c) 2014 OxyPlot contributors
- // </copyright>
- // <summary>
- // Converts between <see cref="OxyColor" /> and <see cref="Color" />.
- // </summary>
- // --------------------------------------------------------------------------------------------------------------------
- namespace OxyPlot.Avalonia.Converters
- {
- using global::Avalonia.Data.Converters;
- using global::Avalonia.Media;
- using System;
- using System.Globalization;
- /// <summary>
- /// Converts between <see cref="OxyColor" /> and <see cref="Color" />.
- /// </summary>
- public class OxyColorConverter : IValueConverter
- {
- /// <summary>
- /// Converts a value.
- /// </summary>
- /// <param name="value">The value produced by the binding source.</param>
- /// <param name="targetType">The type of the binding target property.</param>
- /// <param name="parameter">The converter parameter to use.</param>
- /// <param name="culture">The culture to use in the converter.</param>
- /// <returns>A converted value. If the method returns <c>null</c>, the valid <c>null</c> value is used.</returns>
- 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;
- }
- /// <summary>
- /// Converts a value.
- /// </summary>
- /// <param name="value">The value that is produced by the binding target.</param>
- /// <param name="targetType">The type to convert to.</param>
- /// <param name="parameter">The converter parameter to use.</param>
- /// <param name="culture">The culture to use in the converter.</param>
- /// <returns>A converted value. If the method returns <c>null</c>, the valid <c>null</c> value is used.</returns>
- 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;
- }
- }
- }
|