65 lines
1.9 KiB
C#
65 lines
1.9 KiB
C#
using System;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using StructureHelper.Infrastructure;
|
|
|
|
namespace StructureHelper
|
|
{
|
|
public class ColorPickerViewModel : ViewModelBase
|
|
{
|
|
private int red, green, blue;
|
|
|
|
public int Red
|
|
{
|
|
get => red;
|
|
set => OnColorItemChanged(value, ref red);
|
|
}
|
|
public int Green
|
|
{
|
|
get => green;
|
|
set => OnColorItemChanged(value, ref green);
|
|
}
|
|
public int Blue
|
|
{
|
|
get => blue;
|
|
set => OnColorItemChanged(value, ref blue);
|
|
}
|
|
|
|
private SolidColorBrush selectedColor;
|
|
public SolidColorBrush SelectedColor
|
|
{
|
|
get => selectedColor;
|
|
set => OnPropertyChanged(value, selectedColor);
|
|
}
|
|
public ICommand SetColor { get; }
|
|
public ColorPickerViewModel(PrimitiveDefinitionBase primitive)
|
|
{
|
|
if (primitive != null)
|
|
{
|
|
var solidBrush = primitive.Brush;
|
|
Red = solidBrush.Color.R;
|
|
Green = solidBrush.Color.G;
|
|
Blue = solidBrush.Color.B;
|
|
|
|
SetColor = new RelayCommand(o => primitive.Brush = SelectedColor);
|
|
}
|
|
}
|
|
private void OnColorItemChanged(int value, ref int colorItem, [CallerMemberName] string propertyName = null)
|
|
{
|
|
if (value >= 0 && value <= 255 && Math.Abs(colorItem - value) > 0.001)
|
|
{
|
|
colorItem = value;
|
|
OnPropertyChanged(propertyName);
|
|
UpdateSelectedColor();
|
|
}
|
|
}
|
|
private void UpdateSelectedColor()
|
|
{
|
|
var color = Color.FromRgb((byte)Red, (byte)Green, (byte)Blue);
|
|
SelectedColor = new SolidColorBrush(color);
|
|
OnPropertyChanged(nameof(SelectedColor));
|
|
}
|
|
}
|
|
}
|