diff --git a/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.cs b/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.cs index dcbfa71..4a12060 100644 --- a/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.cs +++ b/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.cs @@ -1,8 +1,10 @@ using System.Globalization; +using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Media; +using Semi.Avalonia.Demo.Converters; namespace Semi.Avalonia.Demo.Controls; @@ -10,6 +12,7 @@ public class ColorDetailControl : TemplatedControl { public const string KEY_ResourceKey = "ResourceKey"; public const string KEY_Hex = "Hex"; + public const string KEY_Hex2 = "Hex2"; public const string KEY_Opacity = "Opacity"; public const string KEY_ColorResourceKey = "ColorResourceKey"; @@ -51,6 +54,17 @@ public class ColorDetailControl : TemplatedControl private set => SetAndRaise(HexProperty, ref _hex, value); } + private string? _hex2; + + public static readonly DirectProperty Hex2Property = + AvaloniaProperty.RegisterDirect(nameof(Hex2), o => o.Hex2); + + public string? Hex2 + { + get => _hex2; + set => SetAndRaise(Hex2Property, ref _hex2, value); + } + public static readonly DirectProperty OpacityNumberProperty = AvaloniaProperty.RegisterDirect(nameof(OpacityNumber), o => o.OpacityNumber); @@ -70,34 +84,30 @@ public class ColorDetailControl : TemplatedControl private void OnBackgroundChanged(AvaloniaPropertyChangedEventArgs args) { var color = args.GetNewValue(); - if (color is ISolidColorBrush b) + if (color is ISolidColorBrush brush) { - Hex = b.Color.ToString().ToUpperInvariant(); - OpacityNumber = b.Opacity.ToString(CultureInfo.InvariantCulture); + var hex1 = ColorConverter.ToHex.Convert(brush.Color, typeof(string), false, CultureInfo.InvariantCulture); + var hex2 = ColorConverter.ToHex.Convert(brush.Color, typeof(string), true, CultureInfo.InvariantCulture); + Hex = hex1 as string; + Hex2 = hex2 as string; + OpacityNumber = brush.Opacity.ToString(CultureInfo.InvariantCulture); } } - public async void Copy(object o) + public async Task Copy(object o) { string? text = null; if (o is string s) { - switch (s) + text = s switch { - case KEY_ResourceKey: - text = ResourceKey; - break; - case KEY_Hex: - text = Hex; - break; - case KEY_Opacity: - text = OpacityNumber; - break; - case KEY_ColorResourceKey: - text = ColorResourceKey; - break; - default: text = string.Empty; break; - } + KEY_ResourceKey => ResourceKey, + KEY_Hex => Hex, + KEY_Hex2 => Hex2, + KEY_Opacity => OpacityNumber, + KEY_ColorResourceKey => ColorResourceKey, + _ => string.Empty + }; } var toplevel = TopLevel.GetTopLevel(this); diff --git a/demo/Semi.Avalonia.Demo/Converters/ColorConverter.cs b/demo/Semi.Avalonia.Demo/Converters/ColorConverter.cs new file mode 100644 index 0000000..6b917f1 --- /dev/null +++ b/demo/Semi.Avalonia.Demo/Converters/ColorConverter.cs @@ -0,0 +1,22 @@ +using Avalonia.Data.Converters; +using Avalonia.Media; + +namespace Semi.Avalonia.Demo.Converters; + +public static class ColorConverter +{ + public static readonly IValueConverter ToHex = + new FuncValueConverter( + (obj, withAlpha) => + obj switch + { + Color color => withAlpha + ? $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}" + : $"#{color.R:X2}{color.G:X2}{color.B:X2}", + ISolidColorBrush brush => withAlpha + ? $"#{brush.Color.A:X2}{brush.Color.R:X2}{brush.Color.G:X2}{brush.Color.B:X2}" + : $"#{brush.Color.R:X2}{brush.Color.G:X2}{brush.Color.B:X2}", + _ => string.Empty + } + ); +} \ No newline at end of file diff --git a/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml b/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml index 9b236df..099b0b5 100644 --- a/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml +++ b/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml @@ -4,6 +4,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:Semi.Avalonia.Demo.ViewModels" xmlns:controls="clr-namespace:Semi.Avalonia.Demo.Controls" + xmlns:cvt="clr-namespace:Semi.Avalonia.Demo.Converters" mc:Ignorable="d" d:DesignWidth="1000" d:DesignHeight="1450" x:DataType="vm:HighContrastDemoViewModel" x:CompileBindings="True" @@ -277,7 +278,8 @@ + Text="{Binding Brush, + Converter={x:Static cvt:ColorConverter.ToHex},ConverterParameter={x:False}}" /> diff --git a/demo/Semi.Avalonia.Demo/Themes/ColorDetailControl.axaml b/demo/Semi.Avalonia.Demo/Themes/ColorDetailControl.axaml index ccfda64..17626a3 100644 --- a/demo/Semi.Avalonia.Demo/Themes/ColorDetailControl.axaml +++ b/demo/Semi.Avalonia.Demo/Themes/ColorDetailControl.axaml @@ -16,7 +16,7 @@ HorizontalAlignment="Stretch" Background="{TemplateBinding Background}" CornerRadius="6" /> - + - + + Text="Hex" /> - - - + + + +