feat: complete SimpleColorView.
This commit is contained in:
parent
d2a1f1e277
commit
1cedc77ce1
@ -6,7 +6,7 @@
|
|||||||
xmlns:controls="using:Avalonia.Controls"
|
xmlns:controls="using:Avalonia.Controls"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
d:DesignHeight="450"
|
d:DesignHeight="1450"
|
||||||
d:DesignWidth="800"
|
d:DesignWidth="800"
|
||||||
mc:Ignorable="d">
|
mc:Ignorable="d">
|
||||||
<StackPanel Spacing="20">
|
<StackPanel Spacing="20">
|
||||||
@ -18,6 +18,14 @@
|
|||||||
<ColorView ColorSpectrumShape="Box" />
|
<ColorView ColorSpectrumShape="Box" />
|
||||||
<ColorView Palette="{DynamicResource SemiColorPalette}" />
|
<ColorView Palette="{DynamicResource SemiColorPalette}" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
<StackPanel
|
||||||
|
VerticalAlignment="Top"
|
||||||
|
Orientation="Horizontal">
|
||||||
|
<ColorView
|
||||||
|
Theme="{StaticResource SimpleColorView}"
|
||||||
|
IsAlphaVisible="True"
|
||||||
|
HsvColor="hsv(120,11%,10%)" />
|
||||||
|
</StackPanel>
|
||||||
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
|
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
|
||||||
<ColorPicker ColorSpectrumShape="Ring">
|
<ColorPicker ColorSpectrumShape="Ring">
|
||||||
<ColorPicker.Palette>
|
<ColorPicker.Palette>
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
xmlns:globalization="using:System.Globalization"
|
xmlns:globalization="using:System.Globalization"
|
||||||
xmlns:pc="using:Avalonia.Controls.Primitives.Converters"
|
xmlns:pc="using:Avalonia.Controls.Primitives.Converters"
|
||||||
xmlns:primitives="using:Avalonia.Controls.Primitives"
|
xmlns:primitives="using:Avalonia.Controls.Primitives"
|
||||||
|
xmlns:cvts="clr-namespace:Semi.Avalonia.ColorPicker.Converters"
|
||||||
x:CompileBindings="True">
|
x:CompileBindings="True">
|
||||||
<pc:ContrastBrushConverter x:Key="ContrastBrushConverter" />
|
<pc:ContrastBrushConverter x:Key="ContrastBrushConverter" />
|
||||||
<converters:ColorToDisplayNameConverter x:Key="ColorToDisplayNameConverter" />
|
<converters:ColorToDisplayNameConverter x:Key="ColorToDisplayNameConverter" />
|
||||||
@ -13,6 +14,9 @@
|
|||||||
<converters:DoNothingForNullConverter x:Key="DoNothingForNullConverter" />
|
<converters:DoNothingForNullConverter x:Key="DoNothingForNullConverter" />
|
||||||
<converters:EnumToBoolConverter x:Key="EnumToBoolConverter" />
|
<converters:EnumToBoolConverter x:Key="EnumToBoolConverter" />
|
||||||
<converters:ToBrushConverter x:Key="ToBrushConverter" />
|
<converters:ToBrushConverter x:Key="ToBrushConverter" />
|
||||||
|
<cvts:HsvColorToTextConverter x:Key="HsvColorToTextConverter" />
|
||||||
|
<cvts:ColorToTextConverter x:Key="ColorToTextConverter" />
|
||||||
|
<cvts:ToColorModel x:Key="ToColorModel" />
|
||||||
<globalization:NumberFormatInfo x:Key="ColorViewComponentNumberFormat" NumberDecimalDigits="0" />
|
<globalization:NumberFormatInfo x:Key="ColorViewComponentNumberFormat" NumberDecimalDigits="0" />
|
||||||
|
|
||||||
<VisualBrush
|
<VisualBrush
|
||||||
@ -562,30 +566,18 @@
|
|||||||
</ControlTheme>
|
</ControlTheme>
|
||||||
|
|
||||||
<ControlTheme x:Key="SimpleColorView" TargetType="ColorView">
|
<ControlTheme x:Key="SimpleColorView" TargetType="ColorView">
|
||||||
|
<Setter Property="Width" Value="280" />
|
||||||
<Setter Property="CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
|
<Setter Property="CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
|
||||||
<Setter Property="ColorSpectrumComponents" Value="SaturationValue" />
|
<Setter Property="ColorSpectrumComponents" Value="SaturationValue" />
|
||||||
<Setter Property="Template">
|
<Setter Property="Template">
|
||||||
<ControlTemplate TargetType="{x:Type ColorView}">
|
<ControlTemplate TargetType="{x:Type ColorView}">
|
||||||
<Grid>
|
<Grid>
|
||||||
<Grid.RowDefinitions>
|
<Grid.RowDefinitions>
|
||||||
<RowDefinition Height="*" MinHeight="280"/>
|
<RowDefinition Height="*" MinHeight="280" />
|
||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto" />
|
||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto" />
|
||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto" />
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
<!-- Backgrounds -->
|
|
||||||
<Border
|
|
||||||
x:Name="ContentBackgroundBorder"
|
|
||||||
Grid.Row="0"
|
|
||||||
Grid.RowSpan="3"
|
|
||||||
Height="{TemplateBinding Height}"
|
|
||||||
HorizontalAlignment="Stretch"
|
|
||||||
VerticalAlignment="Stretch"
|
|
||||||
Background="{DynamicResource ColorViewContentBackgroundBrush}"
|
|
||||||
BorderBrush="{DynamicResource ColorViewContentBorderBrush}"
|
|
||||||
BorderThickness="0,1,0,0"
|
|
||||||
CornerRadius="{TemplateBinding CornerRadius}" />
|
|
||||||
|
|
||||||
<!-- Spectrum Tab -->
|
<!-- Spectrum Tab -->
|
||||||
<Border
|
<Border
|
||||||
Grid.Row="0"
|
Grid.Row="0"
|
||||||
@ -596,7 +588,7 @@
|
|||||||
HorizontalAlignment="Stretch"
|
HorizontalAlignment="Stretch"
|
||||||
VerticalAlignment="Stretch"
|
VerticalAlignment="Stretch"
|
||||||
Components="{TemplateBinding ColorSpectrumComponents}"
|
Components="{TemplateBinding ColorSpectrumComponents}"
|
||||||
HsvColor="{Binding HsvColor, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
|
HsvColor="{TemplateBinding HsvColor, Mode=TwoWay}"
|
||||||
MaxHue="{TemplateBinding MaxHue}"
|
MaxHue="{TemplateBinding MaxHue}"
|
||||||
MaxSaturation="{TemplateBinding MaxSaturation}"
|
MaxSaturation="{TemplateBinding MaxSaturation}"
|
||||||
MaxValue="{TemplateBinding MaxValue}"
|
MaxValue="{TemplateBinding MaxValue}"
|
||||||
@ -644,22 +636,36 @@
|
|||||||
Width="20"
|
Width="20"
|
||||||
Height="20"
|
Height="20"
|
||||||
CornerRadius="4" />
|
CornerRadius="4" />
|
||||||
<Panel>
|
<Panel
|
||||||
|
x:Name="PART_TextBoxPanel"
|
||||||
|
Width="106"
|
||||||
|
Margin="4 0 0 0"
|
||||||
|
HorizontalAlignment="Stretch"
|
||||||
|
VerticalAlignment="Center">
|
||||||
<TextBox
|
<TextBox
|
||||||
x:Name="PART_HexTextBox"
|
x:Name="PART_HexTextBox"
|
||||||
HorizontalAlignment="Stretch"
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
Margin="4 0 0 0"
|
|
||||||
Classes="Small"
|
Classes="Small"
|
||||||
AutomationProperties.Name="Hexadecimal Color"
|
AutomationProperties.Name="Hexadecimal Color"
|
||||||
InnerLeftContent="#"
|
InnerLeftContent="#"
|
||||||
IsVisible="{TemplateBinding IsHexInputVisible}"
|
IsVisible="{Binding #ColorModelComboBox.SelectedValue, Converter={StaticResource ToColorModel}, ConverterParameter=Hex}"
|
||||||
|
Text="{Binding #ColorSpectrum.Color, Converter={StaticResource ColorToHexConverter}, Mode=TwoWay}"
|
||||||
MaxLength="8" />
|
MaxLength="8" />
|
||||||
|
<TextBox
|
||||||
|
x:Name="PART_HsvaTextBox"
|
||||||
|
Classes="Small"
|
||||||
|
IsVisible="{Binding #ColorModelComboBox.SelectedValue, Converter={StaticResource ToColorModel}, ConverterParameter=Hsva}"
|
||||||
|
Text="{TemplateBinding HsvColor, Converter={StaticResource HsvColorToTextConverter}, Mode=TwoWay}" />
|
||||||
|
<TextBox
|
||||||
|
x:Name="PART_RgbaTextBox"
|
||||||
|
Classes="Small"
|
||||||
|
IsVisible="{Binding #ColorModelComboBox.SelectedValue, Converter={StaticResource ToColorModel}, ConverterParameter=Rgba}"
|
||||||
|
Text="{TemplateBinding Color, Converter={StaticResource ColorToTextConverter}, Mode=TwoWay}" />
|
||||||
</Panel>
|
</Panel>
|
||||||
|
|
||||||
<NumericUpDown
|
<NumericUpDown
|
||||||
x:Name="AlphaComponentNumericUpDown"
|
x:Name="AlphaComponentNumericUpDown"
|
||||||
Width="70"
|
Width="70"
|
||||||
|
HorizontalAlignment="Right"
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
AllowSpin="True"
|
AllowSpin="True"
|
||||||
Classes="Small"
|
Classes="Small"
|
||||||
@ -669,27 +675,24 @@
|
|||||||
NumberFormat="{StaticResource ColorViewComponentNumberFormat}"
|
NumberFormat="{StaticResource ColorViewComponentNumberFormat}"
|
||||||
ShowButtonSpinner="False"
|
ShowButtonSpinner="False"
|
||||||
InnerRightContent="%"
|
InnerRightContent="%"
|
||||||
Value="{Binding #ColorSpectrumAlphaSlider.Value}">
|
IsVisible="{TemplateBinding IsAlphaVisible}"
|
||||||
<NumericUpDown.IsVisible>
|
Value="{Binding #ColorSpectrumAlphaSlider.Value}" />
|
||||||
<MultiBinding Converter="{x:Static BoolConverters.And}">
|
|
||||||
<Binding Path="IsAlphaVisible" RelativeSource="{RelativeSource TemplatedParent}" />
|
|
||||||
<Binding Path="IsComponentTextInputVisible" RelativeSource="{RelativeSource TemplatedParent}" />
|
|
||||||
</MultiBinding>
|
|
||||||
</NumericUpDown.IsVisible>
|
|
||||||
</NumericUpDown>
|
|
||||||
<ComboBox
|
<ComboBox
|
||||||
x:Name="ColorComponentComboBox"
|
x:Name="ColorModelComboBox"
|
||||||
Width="80"
|
Width="80"
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
Classes="Small"
|
Classes="Small"
|
||||||
SelectedValue="{TemplateBinding ColorModel}">
|
SelectedValue="Hex">
|
||||||
<ColorModel>Rgba</ColorModel>
|
Hex<ColorModel>Rgba</ColorModel>
|
||||||
<ColorModel>Hsva</ColorModel>
|
<ColorModel>Hsva</ColorModel>
|
||||||
</ComboBox>
|
</ComboBox>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</Grid>
|
</Grid>
|
||||||
</ControlTemplate>
|
</ControlTemplate>
|
||||||
</Setter>
|
</Setter>
|
||||||
|
<Style Selector="^[IsAlphaVisible=False] /template/ Panel#PART_TextBoxPanel">
|
||||||
|
<Setter Property="Width" Value="176" />
|
||||||
|
</Style>
|
||||||
</ControlTheme>
|
</ControlTheme>
|
||||||
|
|
||||||
<Design.PreviewWith>
|
<Design.PreviewWith>
|
||||||
@ -698,6 +701,6 @@
|
|||||||
IsAlphaVisible="True"
|
IsAlphaVisible="True"
|
||||||
IsAlphaEnabled="True"
|
IsAlphaEnabled="True"
|
||||||
ColorModel="Hsva"
|
ColorModel="Hsva"
|
||||||
HsvColor="hsv(120,1,1,30)" />
|
HsvColor="hsv(120,7%,90%)" />
|
||||||
</Design.PreviewWith>
|
</Design.PreviewWith>
|
||||||
</ResourceDictionary>
|
</ResourceDictionary>
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
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}" : 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 != 3 || 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))
|
||||||
|
{
|
||||||
|
return new Color(0xFF, r, g, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
return BindingOperations.DoNothing;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
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)}"
|
||||||
|
: 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 != 3 || 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))
|
||||||
|
{
|
||||||
|
return new HsvColor(1, h, s / 100, v / 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
return BindingOperations.DoNothing;
|
||||||
|
}
|
||||||
|
}
|
21
src/Semi.Avalonia.ColorPicker/Converters/ToColorModel.cs
Normal file
21
src/Semi.Avalonia.ColorPicker/Converters/ToColorModel.cs
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user