diff --git a/demo/Semi.Avalonia.Demo/Pages/ColorPickerDemo.axaml b/demo/Semi.Avalonia.Demo/Pages/ColorPickerDemo.axaml index 1172cd4..cb5dfaf 100644 --- a/demo/Semi.Avalonia.Demo/Pages/ColorPickerDemo.axaml +++ b/demo/Semi.Avalonia.Demo/Pages/ColorPickerDemo.axaml @@ -6,35 +6,56 @@ xmlns:controls="using:Avalonia.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - d:DesignHeight="450" + d:DesignHeight="1450" d:DesignWidth="800" mc:Ignorable="d"> - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - + + \ No newline at end of file diff --git a/src/Semi.Avalonia.ColorPicker/Controls/ColorPicker.axaml b/src/Semi.Avalonia.ColorPicker/Controls/ColorPicker.axaml index 3498113..514af34 100644 --- a/src/Semi.Avalonia.ColorPicker/Controls/ColorPicker.axaml +++ b/src/Semi.Avalonia.ColorPicker/Controls/ColorPicker.axaml @@ -234,7 +234,7 @@ RowDefinitions="Auto,24,1*,1*,1*,1*,12" Tag="{TemplateBinding ColorModel}"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + HexRgba + Hsva + + + + + + + + + + + + + + + + + + diff --git a/src/Semi.Avalonia.ColorPicker/Controls/ColorView.axaml b/src/Semi.Avalonia.ColorPicker/Controls/ColorView.axaml index 34dfa31..f0805f6 100644 --- a/src/Semi.Avalonia.ColorPicker/Controls/ColorView.axaml +++ b/src/Semi.Avalonia.ColorPicker/Controls/ColorView.axaml @@ -6,13 +6,17 @@ xmlns:globalization="using:System.Globalization" xmlns:pc="using:Avalonia.Controls.Primitives.Converters" xmlns:primitives="using:Avalonia.Controls.Primitives" + xmlns:cvts="clr-namespace:Semi.Avalonia.ColorPicker.Converters" x:CompileBindings="True"> - + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + HexRgba + Hsva + + + + + + + + + + + diff --git a/src/Semi.Avalonia.ColorPicker/Converters/ColorToTextConverter.cs b/src/Semi.Avalonia.ColorPicker/Converters/ColorToTextConverter.cs new file mode 100644 index 0000000..18badbe --- /dev/null +++ b/src/Semi.Avalonia.ColorPicker/Converters/ColorToTextConverter.cs @@ -0,0 +1,34 @@ +using System; +using System.Globalization; +using System.Linq; +using Avalonia; +using Avalonia.Data; +using Avalonia.Data.Converters; +using Avalonia.Media; + +namespace Semi.Avalonia.ColorPicker.Converters; + +public class ColorToTextConverter : IValueConverter +{ + public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) + { + return value is Color color ? $"{color.R},{color.G},{color.B},{color.A}" : AvaloniaProperty.UnsetValue; + } + + public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) + { + if (value is not string str) return BindingOperations.DoNothing; + var parts = str.Split(','); + if (parts.Length != 4 || parts.Any(string.IsNullOrWhiteSpace)) return BindingOperations.DoNothing; + + if (byte.TryParse(parts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out var r) && + byte.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var g) && + byte.TryParse(parts[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out var b) && + byte.TryParse(parts[3], NumberStyles.Integer, CultureInfo.InvariantCulture, out var a)) + { + return new Color(a, r, g, b); + } + + return BindingOperations.DoNothing; + } +} \ No newline at end of file diff --git a/src/Semi.Avalonia.ColorPicker/Converters/HsvColorToTextConverter.cs b/src/Semi.Avalonia.ColorPicker/Converters/HsvColorToTextConverter.cs new file mode 100644 index 0000000..6c37588 --- /dev/null +++ b/src/Semi.Avalonia.ColorPicker/Converters/HsvColorToTextConverter.cs @@ -0,0 +1,36 @@ +using System; +using System.Globalization; +using System.Linq; +using Avalonia; +using Avalonia.Data; +using Avalonia.Data.Converters; +using Avalonia.Media; + +namespace Semi.Avalonia.ColorPicker.Converters; + +public class HsvColorToTextConverter : IValueConverter +{ + public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) + { + return value is HsvColor hsvColor + ? $"{Math.Round(hsvColor.H)},{Math.Round(hsvColor.S * 100)},{Math.Round(hsvColor.V * 100)},{Math.Round(hsvColor.A * 100)}" + : AvaloniaProperty.UnsetValue; + } + + public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) + { + if (value is not string str) return BindingOperations.DoNothing; + var parts = str.Split(','); + if (parts.Length != 4 || parts.Any(string.IsNullOrWhiteSpace)) return BindingOperations.DoNothing; + + if (double.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out var h) && + double.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out var s) && + double.TryParse(parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out var v) && + double.TryParse(parts[3], NumberStyles.Float, CultureInfo.InvariantCulture, out var a)) + { + return new HsvColor(a / 100, h, s / 100, v / 100); + } + + return BindingOperations.DoNothing; + } +} \ No newline at end of file diff --git a/src/Semi.Avalonia.ColorPicker/Converters/ToColorModel.cs b/src/Semi.Avalonia.ColorPicker/Converters/ToColorModel.cs new file mode 100644 index 0000000..d7f21a5 --- /dev/null +++ b/src/Semi.Avalonia.ColorPicker/Converters/ToColorModel.cs @@ -0,0 +1,21 @@ +using System; +using System.Globalization; +using Avalonia.Controls; +using Avalonia.Data.Converters; + +namespace Semi.Avalonia.ColorPicker.Converters; + +public class ToColorModel : IValueConverter +{ + public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) + { + return parameter is "Hex" && value is "Hex" || + parameter is "Rgba" && value is ColorModel.Rgba || + parameter is "Hsva" && value is ColorModel.Hsva; + } + + public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/src/Semi.Avalonia.ColorPicker/Shared.axaml b/src/Semi.Avalonia.ColorPicker/Shared.axaml index 08bc9af..a85aaf9 100644 --- a/src/Semi.Avalonia.ColorPicker/Shared.axaml +++ b/src/Semi.Avalonia.ColorPicker/Shared.axaml @@ -29,4 +29,5 @@ AnchorAndGravity + BottomEdgeAlignedLeft \ No newline at end of file diff --git a/src/Semi.Avalonia/Controls/CalendarDatePicker.axaml b/src/Semi.Avalonia/Controls/CalendarDatePicker.axaml index a3755ef..d9deb27 100644 --- a/src/Semi.Avalonia/Controls/CalendarDatePicker.axaml +++ b/src/Semi.Avalonia/Controls/CalendarDatePicker.axaml @@ -34,11 +34,10 @@ + ColumnDefinitions="*, Auto">