Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
201 lines (168 sloc) 6.75 KB
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using System.Windows.Media;
using MaterialDesignColors;
using MaterialDesignColors.WpfExample.Domain;
using MaterialDesignThemes.Wpf;
namespace MaterialDesignDemo
{
internal class ColorToolViewModel : INotifyPropertyChanged
{
private readonly PaletteHelper _paletteHelper = new PaletteHelper();
private ColorScheme _activeScheme;
public ColorScheme ActiveScheme
{
get => _activeScheme;
set
{
if (_activeScheme != value)
{
_activeScheme = value;
OnPropertyChanged();
}
}
}
private Color? _selectedColor;
public Color? SelectedColor
{
get => _selectedColor;
set
{
if (_selectedColor != value)
{
_selectedColor = value;
OnPropertyChanged();
if (value is Color color)
{
ChangeCustomColor(color);
}
}
}
}
public IEnumerable<ISwatch> Swatches { get; } = SwatchHelper.Swatches;
public ICommand ChangeCustomHueCommand { get; }
public ICommand ChangeHueCommand { get; }
public ICommand ChangeToPrimaryCommand { get; }
public ICommand ChangeToSecondaryCommand { get; }
public ICommand ChangeToPrimaryForegroundCommand { get; }
public ICommand ChangeToSecondaryForegroundCommand { get; }
public ICommand ToggleBaseCommand { get; }
private void ApplyBase(bool isDark)
{
ITheme theme = _paletteHelper.GetTheme();
IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme();
theme.SetBaseTheme(baseTheme);
_paletteHelper.SetTheme(theme);
}
public ColorToolViewModel()
{
ToggleBaseCommand = new AnotherCommandImplementation(o => ApplyBase((bool)o));
ChangeHueCommand = new AnotherCommandImplementation(ChangeHue);
ChangeCustomHueCommand = new AnotherCommandImplementation(ChangeCustomColor);
ChangeToPrimaryCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.Primary));
ChangeToSecondaryCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.Secondary));
ChangeToPrimaryForegroundCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.PrimaryForeground));
ChangeToSecondaryForegroundCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.SecondaryForeground));
ITheme theme = _paletteHelper.GetTheme();
_primaryColor = theme.PrimaryMid.Color;
_secondaryColor = theme.SecondaryMid.Color;
SelectedColor = _primaryColor;
}
private void ChangeCustomColor(object obj)
{
var color = (Color)obj;
if (ActiveScheme == ColorScheme.Primary)
{
_paletteHelper.ChangePrimaryColor(color);
_primaryColor = color;
}
else if (ActiveScheme == ColorScheme.Secondary)
{
_paletteHelper.ChangeSecondaryColor(color);
_secondaryColor = color;
}
else if (ActiveScheme == ColorScheme.PrimaryForeground)
{
SetPrimaryForegroundToSingleColor(color);
_primaryForegroundColor = color;
}
else if (ActiveScheme == ColorScheme.SecondaryForeground)
{
SetSecondaryForegroundToSingleColor(color);
_secondaryForegroundColor = color;
}
}
private void ChangeScheme(ColorScheme scheme)
{
ActiveScheme = scheme;
if (ActiveScheme == ColorScheme.Primary)
{
SelectedColor = _primaryColor;
}
else if (ActiveScheme == ColorScheme.Secondary)
{
SelectedColor = _secondaryColor;
}
else if (ActiveScheme == ColorScheme.PrimaryForeground)
{
SelectedColor = _primaryForegroundColor;
}
else if (ActiveScheme == ColorScheme.SecondaryForeground)
{
SelectedColor = _secondaryForegroundColor;
}
}
private Color? _primaryColor;
private Color? _secondaryColor;
private Color? _primaryForegroundColor;
private Color? _secondaryForegroundColor;
private void ChangeHue(object obj)
{
var hue = (Color)obj;
SelectedColor = hue;
if (ActiveScheme == ColorScheme.Primary)
{
_paletteHelper.ChangePrimaryColor(hue);
_primaryColor = hue;
}
else if (ActiveScheme == ColorScheme.Secondary)
{
_paletteHelper.ChangeSecondaryColor(hue);
_secondaryColor = hue;
}
else if (ActiveScheme == ColorScheme.PrimaryForeground)
{
SetPrimaryForegroundToSingleColor(hue);
_primaryForegroundColor = hue;
}
else if (ActiveScheme == ColorScheme.SecondaryForeground)
{
SetSecondaryForegroundToSingleColor(hue);
_secondaryForegroundColor = hue;
}
}
private void SetPrimaryForegroundToSingleColor(Color color)
{
ITheme theme = _paletteHelper.GetTheme();
theme.PrimaryLight = new ColorPair(theme.PrimaryLight.Color, color);
theme.PrimaryMid = new ColorPair(theme.PrimaryMid.Color, color);
theme.PrimaryDark = new ColorPair(theme.PrimaryDark.Color, color);
_paletteHelper.SetTheme(theme);
}
private void SetSecondaryForegroundToSingleColor(Color color)
{
ITheme theme = _paletteHelper.GetTheme();
theme.SecondaryLight = new ColorPair(theme.SecondaryLight.Color, color);
theme.SecondaryMid = new ColorPair(theme.SecondaryMid.Color, color);
theme.SecondaryDark = new ColorPair(theme.SecondaryDark.Color, color);
_paletteHelper.SetTheme(theme);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
You can’t perform that action at this time.