浏览代码

Remove ThirdComponentConverter replacing it with ColorSpectrum.ThirdComponent property

robloo 3 年之前
父节点
当前提交
e96772e707

+ 0 - 1
samples/ControlCatalog/Pages/ColorPickerPage.xaml

@@ -11,7 +11,6 @@
              x:Class="ControlCatalog.Pages.ColorPickerPage">
 
   <UserControl.Resources>
-    <pc:ThirdComponentConverter x:Key="ThirdComponent" />
   </UserControl.Resources>
 
   <Grid x:Name="LayoutRoot"

+ 24 - 0
src/Avalonia.Controls.ColorPicker/ColorSpectrum/ColorSpectrum.Properties.cs

@@ -93,6 +93,14 @@ namespace Avalonia.Controls.Primitives
                 nameof(Shape),
                 ColorSpectrumShape.Box);
 
+        /// <summary>
+        /// Defines the <see cref="ThirdComponent"/> property.
+        /// </summary>
+        public static readonly StyledProperty<ColorComponent> ThirdComponentProperty =
+            AvaloniaProperty.Register<ColorSpectrum, ColorComponent>(
+                nameof(ThirdComponent),
+                ColorComponent.Component3); // Value
+
         /// <summary>
         /// Gets or sets the currently selected color in the RGB color model.
         /// </summary>
@@ -218,5 +226,21 @@ namespace Avalonia.Controls.Primitives
             get => GetValue(ShapeProperty);
             set => SetValue(ShapeProperty, value);
         }
+
+        /// <summary>
+        /// Gets the third HSV color component that is NOT displayed by the spectrum.
+        /// This is automatically calculated from the <see cref="Components"/> property.
+        /// </summary>
+        /// <remarks>
+        /// This property should be used for any external color slider that represents the
+        /// third component of the color. Note that this property uses the generic
+        /// <see cref="ColorComponent"/> type instead of the more accurate <see cref="HsvComponent"/>
+        /// to allow direct usage by the generalized color sliders.
+        /// </remarks>
+        public ColorComponent ThirdComponent
+        {
+            get => GetValue(ThirdComponentProperty);
+            private set => SetValue(ThirdComponentProperty, value);
+        }
     }
 }

+ 17 - 0
src/Avalonia.Controls.ColorPicker/ColorSpectrum/ColorSpectrum.cs

@@ -503,6 +503,23 @@ namespace Avalonia.Controls.Primitives
             }
             else if (change.Property == ComponentsProperty)
             {
+                // Calculate and update the ThirdComponent value
+                switch (Components)
+                {
+                    case ColorSpectrumComponents.HueSaturation:
+                    case ColorSpectrumComponents.SaturationHue:
+                        ThirdComponent = (ColorComponent)HsvComponent.Value;
+                        break;
+                    case ColorSpectrumComponents.HueValue:
+                    case ColorSpectrumComponents.ValueHue:
+                        ThirdComponent = (ColorComponent)HsvComponent.Saturation;
+                        break;
+                    case ColorSpectrumComponents.SaturationValue:
+                    case ColorSpectrumComponents.ValueSaturation:
+                        ThirdComponent = (ColorComponent)HsvComponent.Hue;
+                        break;
+                }
+
                 CreateBitmapsAndColorMap();
             }
 

+ 0 - 53
src/Avalonia.Controls.ColorPicker/Converters/ThirdComponentConverter.cs

@@ -1,53 +0,0 @@
-using System;
-using System.Globalization;
-using Avalonia.Data.Converters;
-
-namespace Avalonia.Controls.Primitives.Converters
-{
-    /// <summary>
-    /// Gets the third <see cref="ColorComponent"/> corresponding with a given
-    /// <see cref="ColorSpectrumComponents"/> that represents the other two components.
-    /// </summary>
-    /// <remarks>
-    /// This is a highly-specialized converter for the color picker.
-    /// </remarks>
-    public class ThirdComponentConverter : IValueConverter
-    {
-        /// <inheritdoc/>
-        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;
-        }
-
-        /// <inheritdoc/>
-        public object? ConvertBack(
-            object? value,
-            Type targetType,
-            object? parameter,
-            CultureInfo culture)
-        {
-            return AvaloniaProperty.UnsetValue;
-        }
-    }
-}

+ 1 - 2
src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorView.xaml

@@ -8,7 +8,6 @@
                     x:CompileBindings="True">
 
   <pc:ContrastBrushConverter x:Key="ContrastBrushConverter" />
-  <pc:ThirdComponentConverter x:Key="ThirdComponentConverter" />
   <converters:ColorToDisplayNameConverter x:Key="ColorToDisplayNameConverter" />
   <converters:ColorToHexConverter x:Key="ColorToHexConverter" />
   <converters:DoNothingForNullConverter x:Key="DoNothingForNullConverter" />
@@ -368,7 +367,7 @@
                                         IsSaturationValueMaxForced="False"
                                         Orientation="Vertical"
                                         ColorModel="Hsva"
-                                        ColorComponent="{Binding Components, ElementName=ColorSpectrum, Converter={StaticResource ThirdComponentConverter}}"
+                                        ColorComponent="{Binding ThirdComponent, ElementName=ColorSpectrum}"
                                         HsvColor="{Binding HsvColor, ElementName=ColorSpectrum}"
                                         HorizontalAlignment="Center"
                                         VerticalAlignment="Stretch"

+ 1 - 2
src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorView.xaml

@@ -8,7 +8,6 @@
                     x:CompileBindings="True">
 
   <pc:ContrastBrushConverter x:Key="ContrastBrushConverter" />
-  <pc:ThirdComponentConverter x:Key="ThirdComponentConverter" />
   <converters:ColorToDisplayNameConverter x:Key="ColorToDisplayNameConverter" />
   <converters:ColorToHexConverter x:Key="ColorToHexConverter" />
   <converters:DoNothingForNullConverter x:Key="DoNothingForNullConverter" />
@@ -330,7 +329,7 @@
                                         IsSaturationValueMaxForced="False"
                                         Orientation="Vertical"
                                         ColorModel="Hsva"
-                                        ColorComponent="{Binding Components, ElementName=ColorSpectrum, Converter={StaticResource ThirdComponentConverter}}"
+                                        ColorComponent="{Binding ThirdComponent, ElementName=ColorSpectrum}"
                                         HsvColor="{Binding HsvColor, ElementName=ColorSpectrum}"
                                         HorizontalAlignment="Center"
                                         VerticalAlignment="Stretch"