This commit is contained in:
wieslawsoltes 2018-11-21 10:03:43 +00:00
Родитель 1e990e626f
Коммит 24ac8a8cbb
3 изменённых файлов: 54 добавлений и 41 удалений

Просмотреть файл

@ -184,6 +184,44 @@ namespace ThemeEditor.Controls.ColorPicker
}
}
public class HexToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string s && targetType == typeof(Color))
{
try
{
if (ColorHelpers.IsValidHexColor(s))
{
return ColorHelpers.FromHexColor(s);
}
}
catch (Exception)
{
return AvaloniaProperty.UnsetValue;
}
}
return AvaloniaProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Color c && targetType == typeof(string))
{
try
{
return ColorHelpers.ToHexColor(c);
}
catch (Exception)
{
return AvaloniaProperty.UnsetValue;
}
}
return AvaloniaProperty.UnsetValue;
}
}
public abstract class ColorPickerProperties : AvaloniaObject
{
public static readonly StyledProperty<ColorPicker> ColorPickerProperty =

Просмотреть файл

@ -168,19 +168,6 @@
</TabItem>
</TabControl>
<TabControl Classes="picker" Grid.Row="2">
<TabItem Header="HSV" Classes="picker">
<Grid RowDefinitions="Auto,Auto,Auto" ColumnDefinitions="Auto,90" Classes="properties">
<Grid.DataContext>
<local:HsvProperties ColorPicker="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
</Grid.DataContext>
<TextBlock Text="H" ToolTip.Tip="Hue" Classes="picker" Grid.Row="0" Grid.Column="0"/>
<NumericUpDown Value="{Binding Hue, Mode=TwoWay}" Minimum="0" Maximum="360" Increment="0.5" FormatString="{}{0:N2} °" Classes="picker" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="S" ToolTip.Tip="Saturation" Classes="picker" Grid.Row="1" Grid.Column="0"/>
<NumericUpDown Value="{Binding Saturation, Mode=TwoWay}" Minimum="0" Maximum="100" Increment="0.5" FormatString="{}{0:N2} %" Classes="picker" Grid.Row="1" Grid.Column="1"/>
<TextBlock Text="V" ToolTip.Tip="Value" Classes="picker" Grid.Row="2" Grid.Column="0"/>
<NumericUpDown Value="{Binding Value, Mode=TwoWay}" Minimum="0" Maximum="100" Increment="0.5" FormatString="{}{0:N2} %" Classes="picker" Grid.Row="2" Grid.Column="1"/>
</Grid>
</TabItem>
<TabItem Header="RGB" Classes="picker">
<Grid RowDefinitions="Auto,Auto,Auto" ColumnDefinitions="Auto,90" Classes="properties">
<Grid.DataContext>
@ -194,6 +181,19 @@
<NumericUpDown Value="{Binding Blue, Mode=TwoWay}" Minimum="0" Maximum="255" Increment="1" FormatString="G" Classes="picker" Grid.Row="2" Grid.Column="1"/>
</Grid>
</TabItem>
<TabItem Header="HSV" Classes="picker">
<Grid RowDefinitions="Auto,Auto,Auto" ColumnDefinitions="Auto,90" Classes="properties">
<Grid.DataContext>
<local:HsvProperties ColorPicker="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
</Grid.DataContext>
<TextBlock Text="H" ToolTip.Tip="Hue" Classes="picker" Grid.Row="0" Grid.Column="0"/>
<NumericUpDown Value="{Binding Hue, Mode=TwoWay}" Minimum="0" Maximum="360" Increment="0.5" FormatString="{}{0:N2} °" Classes="picker" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="S" ToolTip.Tip="Saturation" Classes="picker" Grid.Row="1" Grid.Column="0"/>
<NumericUpDown Value="{Binding Saturation, Mode=TwoWay}" Minimum="0" Maximum="100" Increment="0.5" FormatString="{}{0:N2} %" Classes="picker" Grid.Row="1" Grid.Column="1"/>
<TextBlock Text="V" ToolTip.Tip="Value" Classes="picker" Grid.Row="2" Grid.Column="0"/>
<NumericUpDown Value="{Binding Value, Mode=TwoWay}" Minimum="0" Maximum="100" Increment="0.5" FormatString="{}{0:N2} %" Classes="picker" Grid.Row="2" Grid.Column="1"/>
</Grid>
</TabItem>
<TabItem Header="CMYK" Classes="picker">
<Grid RowDefinitions="Auto,Auto,Auto,Auto" ColumnDefinitions="Auto,90" Classes="properties">
<Grid.DataContext>

Просмотреть файл

@ -3,36 +3,11 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:converters="clr-namespace:ThemeEditor.Converters;assembly=ThemeEditor"
xmlns:cp="clr-namespace:ThemeEditor.Controls.ColorPicker;assembly=ThemeEditor.Controls.ColorPicker"
mc:Ignorable="d"
d:DesignWidth="500" d:DesignHeight="400">
<UserControl.Resources>
<converters:ColorViewModelToBrushConverter x:Key="ColorViewModelToBrushConverter"/>
<cp:HexToColorConverter x:Key="HexToColorConverter"/>
</UserControl.Resources>
<Grid RowDefinitions="Auto,30,Auto,Auto,Auto,Auto" ColumnDefinitions="Auto,50,*">
<TextBox Text="{Binding Hex, Mode=TwoWay}" IsReadOnly="False" Classes="left" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"/>
<Border Classes="color" Background="{DynamicResource ColorCheckerBoard}" Margin="2" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3"/>
<Border Classes="color" Margin="2" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3">
<Border.Background>
<MultiBinding Mode="OneWay" Converter="{StaticResource ColorViewModelToBrushConverter}">
<Binding Path="A"/>
<Binding Path="R"/>
<Binding Path="G"/>
<Binding Path="B"/>
</MultiBinding>
</Border.Background>
</Border>
<TextBlock Text="A" Classes="left" Grid.Row="2" Grid.Column="0"/>
<TextBox Text="{Binding A, Mode=TwoWay}" Classes="left" Grid.Row="2" Grid.Column="1"/>
<Slider Classes="slider" Value="{Binding A}" Minimum="0" Maximum="255" Grid.Row="2" Grid.Column="2"/>
<TextBlock Text="R" Classes="left" Grid.Row="3" Grid.Column="0"/>
<TextBox Text="{Binding R, Mode=TwoWay}" Classes="left" Grid.Row="3" Grid.Column="1"/>
<Slider Classes="slider" Value="{Binding R, Mode=TwoWay}" Minimum="0" Maximum="255" Grid.Row="3" Grid.Column="2"/>
<TextBlock Text="G" Classes="left" Grid.Row="4" Grid.Column="0"/>
<TextBox Text="{Binding G, Mode=TwoWay}" Classes="left" Grid.Row="4" Grid.Column="1"/>
<Slider Classes="slider" Value="{Binding G, Mode=TwoWay}" Minimum="0" Maximum="255" Grid.Row="4" Grid.Column="2"/>
<TextBlock Text="B" Classes="left" Grid.Row="5" Grid.Column="0"/>
<TextBox Text="{Binding B, Mode=TwoWay}" Classes="left" Grid.Row="5" Grid.Column="1"/>
<Slider Classes="slider" Value="{Binding B}" Minimum="0" Maximum="255" Grid.Row="5" Grid.Column="2"/>
</Grid>
<cp:ColorPicker Color="{Binding Hex, Mode=TwoWay, Converter={StaticResource HexToColorConverter}}"/>
</UserControl>