Skip to content

Commit 4252ca5

Browse files
FantasyTeddyKeboo
andauthored
Add settings for color adjustment to demo application (#2389)
* Add toggle button for color adjustment in demo application * Allow modification of color adjustment values in demo application * Fixing tests to account for tolerance being + or - Co-authored-by: Kevin Bost <[email protected]>
1 parent 6833dba commit 4252ca5

File tree

5 files changed

+173
-5
lines changed

5 files changed

+173
-5
lines changed

MainDemo.Wpf/Domain/PaletteSelectorViewModel.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Windows.Input;
45
using MaterialDesignColors;
56
using MaterialDesignThemes.Wpf;
@@ -17,6 +18,16 @@ public PaletteSelectorViewModel()
1718

1819
IsDarkTheme = theme.GetBaseTheme() == BaseTheme.Dark;
1920

21+
if (theme is Theme internalTheme)
22+
{
23+
_isColorAdjusted = internalTheme.ColorAdjustment is not null;
24+
25+
var colorAdjustment = internalTheme.ColorAdjustment ?? new ColorAdjustment();
26+
_desiredContrastRatio = colorAdjustment.DesiredContrastRatio;
27+
_contrastValue = colorAdjustment.Contrast;
28+
_colorSelectionValue = colorAdjustment.Colors;
29+
}
30+
2031
if (paletteHelper.GetThemeManager() is { } themeManager)
2132
{
2233
themeManager.ThemeChanged += (_, e) =>
@@ -39,6 +50,87 @@ public bool IsDarkTheme
3950
}
4051
}
4152

53+
private bool _isColorAdjusted;
54+
public bool IsColorAdjusted
55+
{
56+
get => _isColorAdjusted;
57+
set
58+
{
59+
if (SetProperty(ref _isColorAdjusted, value))
60+
{
61+
ModifyTheme(theme =>
62+
{
63+
if (theme is Theme internalTheme)
64+
{
65+
internalTheme.ColorAdjustment = value
66+
? new ColorAdjustment
67+
{
68+
DesiredContrastRatio = DesiredContrastRatio,
69+
Contrast = ContrastValue,
70+
Colors = ColorSelectionValue
71+
}
72+
: null;
73+
}
74+
});
75+
}
76+
}
77+
}
78+
79+
private float _desiredContrastRatio = 4.5f;
80+
public float DesiredContrastRatio
81+
{
82+
get => _desiredContrastRatio;
83+
set
84+
{
85+
if (SetProperty(ref _desiredContrastRatio, value))
86+
{
87+
ModifyTheme(theme =>
88+
{
89+
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
90+
internalTheme.ColorAdjustment.DesiredContrastRatio = value;
91+
});
92+
}
93+
}
94+
}
95+
96+
public IEnumerable<Contrast> ContrastValues => Enum.GetValues(typeof(Contrast)).Cast<Contrast>();
97+
98+
private Contrast _contrastValue;
99+
public Contrast ContrastValue
100+
{
101+
get => _contrastValue;
102+
set
103+
{
104+
if (SetProperty(ref _contrastValue, value))
105+
{
106+
ModifyTheme(theme =>
107+
{
108+
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
109+
internalTheme.ColorAdjustment.Contrast = value;
110+
});
111+
}
112+
}
113+
}
114+
115+
public IEnumerable<ColorSelection> ColorSelectionValues => Enum.GetValues(typeof(ColorSelection)).Cast<ColorSelection>();
116+
117+
private ColorSelection _colorSelectionValue;
118+
public ColorSelection ColorSelectionValue
119+
{
120+
get => _colorSelectionValue;
121+
set
122+
{
123+
if (SetProperty(ref _colorSelectionValue, value))
124+
{
125+
ModifyTheme(theme =>
126+
{
127+
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
128+
internalTheme.ColorAdjustment.Colors = value;
129+
});
130+
}
131+
}
132+
}
133+
42134
public IEnumerable<Swatch> Swatches { get; }
43135

44136
public ICommand ApplyPrimaryCommand { get; } = new AnotherCommandImplementation(o => ApplyPrimary((Swatch)o));

MainDemo.Wpf/PaletteSelector.xaml

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
1111
d:DataContext="{d:DesignInstance domain:PaletteSelectorViewModel}"
1212
d:DesignHeight="300"
13-
d:DesignWidth="300"
13+
d:DesignWidth="400"
1414
mc:Ignorable="d">
1515
<UserControl.Resources>
1616
<DataTemplate DataType="{x:Type materialDesignColors:Swatch}">
@@ -143,6 +143,81 @@
143143
<TextBlock
144144
VerticalAlignment="Center"
145145
Text="Dark"/>
146+
147+
<TextBlock
148+
VerticalAlignment="Center"
149+
Margin="50 0 0 0"
150+
Text="Color Adjustment"/>
151+
152+
<ToggleButton
153+
Margin="8 0 0 0"
154+
IsChecked="{Binding IsColorAdjusted}"/>
155+
156+
<wpf:PopupBox StaysOpen="True">
157+
<Grid Margin="8">
158+
<Grid.ColumnDefinitions>
159+
<ColumnDefinition Width="*"/>
160+
<ColumnDefinition Width="*"/>
161+
<ColumnDefinition Width="*"/>
162+
</Grid.ColumnDefinitions>
163+
164+
<Grid.RowDefinitions>
165+
<RowDefinition Height="*"/>
166+
<RowDefinition Height="*"/>
167+
<RowDefinition Height="*"/>
168+
</Grid.RowDefinitions>
169+
170+
<TextBlock
171+
Grid.Column="0"
172+
Grid.Row="0"
173+
Margin="10"
174+
VerticalAlignment="Center"
175+
Text="Desired Contrast Ratio"/>
176+
<Slider
177+
Grid.Column="1"
178+
Grid.Row="0"
179+
Minimum="1"
180+
Maximum="21"
181+
TickFrequency="0.1"
182+
Value="{Binding DesiredContrastRatio}"
183+
IsSnapToTickEnabled="True"
184+
VerticalAlignment="Center"
185+
Width="150"/>
186+
<TextBlock
187+
Grid.Column="2"
188+
Grid.Row="0"
189+
VerticalAlignment="Center"
190+
TextAlignment="Right"
191+
Margin="8"
192+
Width="40"
193+
Text="{Binding DesiredContrastRatio, StringFormat={}{0}:1}">
194+
</TextBlock>
195+
196+
<TextBlock
197+
Grid.Column="0"
198+
Grid.Row="1"
199+
Margin="10"
200+
VerticalAlignment="Center"
201+
Text="Contrast"/>
202+
<ComboBox
203+
Grid.Column="1"
204+
Grid.Row="1"
205+
ItemsSource="{Binding ContrastValues}"
206+
SelectedItem="{Binding ContrastValue}"/>
207+
208+
<TextBlock
209+
Grid.Column="0"
210+
Grid.Row="2"
211+
Margin="10"
212+
VerticalAlignment="Center"
213+
Text="Color Selection"/>
214+
<ComboBox
215+
Grid.Column="1"
216+
Grid.Row="2"
217+
ItemsSource="{Binding ColorSelectionValues}"
218+
SelectedItem="{Binding ColorSelectionValue}"/>
219+
</Grid>
220+
</wpf:PopupBox>
146221
</StackPanel>
147222

148223
<ItemsControl

MaterialDesignColors.Wpf.Tests/ColorAssistTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void EnsureContrastRatio_AdjustsColor()
1515
var adjusted = foreground.EnsureContrastRatio(background, 3.0f);
1616

1717
double contrastRatio = adjusted.ContrastRatio(background);
18-
Assert.True(contrastRatio >= 3.0);
18+
Assert.True(contrastRatio >= 2.9);
1919
Assert.True(contrastRatio <= 3.1);
2020
}
2121
}

MaterialDesignColors.Wpf/ColorManipulation/ColorAssist.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static Color EnsureContrastRatio(this Color foreground, Color background,
8888
Color finalColor = foreground;
8989
double? adjust = null;
9090

91-
while ((ratio < targetRatio || ratio > targetRatio + tollerance) &&
91+
while ((ratio < targetRatio - tollerance || ratio > targetRatio + tollerance) &&
9292
finalColor != Colors.White &&
9393
finalColor != Colors.Black)
9494
{

MaterialDesignThemes.UITests/WPF/Theme/ColorAdjustTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ public async Task PrimaryColor_AdjustToTheme(PrimaryColor primary)
5555

5656
async Task AssertContrastRatio()
5757
{
58+
const double tollerance = 0.1;
5859
Color? largeTextForeground = await largeText.GetForegroundColor();
5960
Color largeTextBackground = await largeText.GetEffectiveBackground();
6061

6162
Color? smallTextForeground = await smallText.GetForegroundColor();
6263
Color smallTextBackground = await smallText.GetEffectiveBackground();
6364

6465
var largeContrastRatio = ColorAssist.ContrastRatio(largeTextForeground.Value, largeTextBackground);
65-
Assert.True(largeContrastRatio >= MaterialDesignSpec.MinimumContrastLargeText, $"Large font contrast ratio '{largeContrastRatio}' does not meet material design spec {MaterialDesignSpec.MinimumContrastLargeText}");
66+
Assert.True(largeContrastRatio >= MaterialDesignSpec.MinimumContrastLargeText - tollerance, $"Large font contrast ratio '{largeContrastRatio}' does not meet material design spec {MaterialDesignSpec.MinimumContrastLargeText}");
6667
var smallContrastRatio = ColorAssist.ContrastRatio(smallTextForeground.Value, smallTextBackground);
67-
Assert.True(smallContrastRatio >= MaterialDesignSpec.MinimumContrastSmallText, $"Small font contrast ratio '{smallContrastRatio}' does not meet material design spec {MaterialDesignSpec.MinimumContrastSmallText}");
68+
Assert.True(smallContrastRatio >= MaterialDesignSpec.MinimumContrastSmallText - tollerance, $"Small font contrast ratio '{smallContrastRatio}' does not meet material design spec {MaterialDesignSpec.MinimumContrastSmallText}");
6869
}
6970
}
7071
}

0 commit comments

Comments
 (0)