From f3d429f055abe42fc26d1fd9d8591a57619970bb Mon Sep 17 00:00:00 2001 From: robloo Date: Thu, 28 Apr 2022 20:13:56 -0400 Subject: [PATCH] Add ThirdComponentConverter and use it --- .../ControlCatalog/Pages/ColorPickerPage.xaml | 84 ++++++++++++------- .../Converters/AccentColorConverter.cs | 1 + .../Converters/ThirdComponentConverter.cs | 51 +++++++++++ 3 files changed, 108 insertions(+), 28 deletions(-) create mode 100644 src/Avalonia.Controls.ColorPicker/Converters/ThirdComponentConverter.cs diff --git a/samples/ControlCatalog/Pages/ColorPickerPage.xaml b/samples/ControlCatalog/Pages/ColorPickerPage.xaml index 09ec15ad15..c0c83d6a35 100644 --- a/samples/ControlCatalog/Pages/ColorPickerPage.xaml +++ b/samples/ControlCatalog/Pages/ColorPickerPage.xaml @@ -3,49 +3,77 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:primitives="clr-namespace:Avalonia.Controls.Primitives;assembly=Avalonia.Controls" + xmlns:pc="clr-namespace:Avalonia.Controls.Primitives.Converters;assembly=Avalonia.Controls.ColorPicker" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="ControlCatalog.Pages.ColorPickerPage"> - - - + + + + + - + + - + - + - + + + + + + + - + + HsvColor="{Binding HsvColor, ElementName=ColorSpectrum2}" /> diff --git a/src/Avalonia.Controls.ColorPicker/Converters/AccentColorConverter.cs b/src/Avalonia.Controls.ColorPicker/Converters/AccentColorConverter.cs index 6b459fe626..01a4bbe800 100644 --- a/src/Avalonia.Controls.ColorPicker/Converters/AccentColorConverter.cs +++ b/src/Avalonia.Controls.ColorPicker/Converters/AccentColorConverter.cs @@ -7,6 +7,7 @@ namespace Avalonia.Controls.Primitives.Converters { /// /// Creates an accent color for a given base color value and step parameter. + /// This is a highly-specialized converter for the color picker. /// public class AccentColorConverter : IValueConverter { diff --git a/src/Avalonia.Controls.ColorPicker/Converters/ThirdComponentConverter.cs b/src/Avalonia.Controls.ColorPicker/Converters/ThirdComponentConverter.cs new file mode 100644 index 0000000000..220a993f99 --- /dev/null +++ b/src/Avalonia.Controls.ColorPicker/Converters/ThirdComponentConverter.cs @@ -0,0 +1,51 @@ +using System; +using System.Globalization; +using Avalonia.Data.Converters; + +namespace Avalonia.Controls.Primitives.Converters +{ + /// + /// Gets the third corresponding with a given + /// that represents the other two components. + /// This is a highly-specialized converter for the color picker. + /// + public class ThirdComponentConverter : IValueConverter + { + /// + public object? Convert( + object? value, + Type targetType, + object? parameter, + CultureInfo culture) + { + if (value is ColorSpectrumComponents components) + { + // Note: Alpha is not relevant here + switch (components) + { + case ColorSpectrumComponents.HueSaturation: + case ColorSpectrumComponents.SaturationHue: + return (ColorComponent)HsvComponent.Value; + case ColorSpectrumComponents.HueValue: + case ColorSpectrumComponents.ValueHue: + return (ColorComponent)HsvComponent.Saturation; + case ColorSpectrumComponents.SaturationValue: + case ColorSpectrumComponents.ValueSaturation: + return (ColorComponent)HsvComponent.Hue; + } + } + + return AvaloniaProperty.UnsetValue; + } + + /// + public object? ConvertBack( + object? value, + Type targetType, + object? parameter, + CultureInfo culture) + { + return AvaloniaProperty.UnsetValue; + } + } +}