forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorToolViewModel.cs
More file actions
203 lines (173 loc) · 7.31 KB
/
ColorToolViewModel.cs
File metadata and controls
203 lines (173 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Windows.Input;
using System.Windows.Media;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
namespace MaterialDesignDemo.Domain
{
internal class ColorToolViewModel : ViewModelBase
{
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 we are triggering a change internally its a hue change and the colors will match
// so we don't want to trigger a custom color change.
var currentSchemeColor = ActiveScheme switch
{
ColorScheme.Primary => _primaryColor,
ColorScheme.Secondary => _secondaryColor,
ColorScheme.PrimaryForeground => _primaryForegroundColor,
ColorScheme.SecondaryForeground => _secondaryForegroundColor,
_ => throw new NotSupportedException($"{ActiveScheme} is not a handled ColorScheme.. Ye daft programmer!")
};
if (_selectedColor != currentSchemeColor && 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;
_primaryForegroundColor = _paletteHelper.GetTheme().PrimaryMid.GetForegroundColor();
}
else if (ActiveScheme == ColorScheme.Secondary)
{
_paletteHelper.ChangeSecondaryColor(hue);
_secondaryColor = hue;
_secondaryForegroundColor = _paletteHelper.GetTheme().SecondaryMid.GetForegroundColor();
}
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);
}
}
}