Skip to content

Commit a7e534b

Browse files
committed
Renamed ContrastAnalyzer to ContrastHelper and repaired binding issues
1 parent d071fa9 commit a7e534b

File tree

6 files changed

+264
-60
lines changed

6 files changed

+264
-60
lines changed

components/ColorAnalyzer/samples/AccentAnalyzer.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ icon: assets/icon.png
1717
The AccentAnalyzer provides a pure XAML way to use the colors extracted from an image as a binding source for any `Color` property.
1818

1919
> [!Sample AccentAnalyzerSample]
20+
21+
> [!Sample ContrastHelperSample]

components/ColorAnalyzer/samples/AccentAnalyzerSample.xaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
22
<Page x:Class="ColorAnalyzerExperiment.Samples.AccentAnalyzerSample"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -62,7 +62,7 @@
6262
<Border.Background>
6363
<SolidColorBrush Color="{x:Bind AccentAnalyzer.DominantColor, Mode=OneWay}" />
6464
</Border.Background>
65-
<TextBlock helpers:ContrastAnalyzer.Opponent="{x:Bind AccentAnalyzer.DominantColor, Mode=OneWay}"
65+
<TextBlock helpers:ContrastHelper.Opponent="{x:Bind AccentAnalyzer.DominantColor, Mode=OneWay}"
6666
Text="Dominant" />
6767
</Border>
6868

@@ -76,8 +76,8 @@
7676
</Border.Background>
7777
<TextBlock Text="Base">
7878
<TextBlock.Foreground>
79-
<SolidColorBrush helpers:ContrastAnalyzer.MinRatio="5"
80-
helpers:ContrastAnalyzer.Opponent="{x:Bind AccentAnalyzer.BaseColor, Mode=OneWay}"
79+
<SolidColorBrush helpers:ContrastHelper.MinRatio="5"
80+
helpers:ContrastHelper.Opponent="{x:Bind AccentAnalyzer.BaseColor, Mode=OneWay}"
8181
Color="{x:Bind AccentAnalyzer.PrimaryAccentColor, Mode=OneWay}" />
8282
</TextBlock.Foreground>
8383
</TextBlock>
@@ -91,7 +91,7 @@
9191
<Border.Background>
9292
<SolidColorBrush Color="{x:Bind AccentAnalyzer.PrimaryAccentColor, Mode=OneWay}" />
9393
</Border.Background>
94-
<TextBlock helpers:ContrastAnalyzer.Opponent="{x:Bind AccentAnalyzer.PrimaryAccentColor, Mode=OneWay}"
94+
<TextBlock helpers:ContrastHelper.Opponent="{x:Bind AccentAnalyzer.PrimaryAccentColor, Mode=OneWay}"
9595
Text="Primary" />
9696
</Border>
9797
<!-- Secondary -->
@@ -102,7 +102,7 @@
102102
<Border.Background>
103103
<SolidColorBrush Color="{x:Bind AccentAnalyzer.SecondaryAccentColor, Mode=OneWay}" />
104104
</Border.Background>
105-
<TextBlock helpers:ContrastAnalyzer.Opponent="{x:Bind AccentAnalyzer.SecondaryAccentColor, Mode=OneWay}"
105+
<TextBlock helpers:ContrastHelper.Opponent="{x:Bind AccentAnalyzer.SecondaryAccentColor, Mode=OneWay}"
106106
Text="Secondary" />
107107
</Border>
108108
<!-- Tertiary -->
@@ -113,7 +113,7 @@
113113
<Border.Background>
114114
<SolidColorBrush Color="{x:Bind AccentAnalyzer.TertiaryAccentColor, Mode=OneWay}" />
115115
</Border.Background>
116-
<TextBlock helpers:ContrastAnalyzer.Opponent="{x:Bind AccentAnalyzer.TertiaryAccentColor, Mode=OneWay}"
116+
<TextBlock helpers:ContrastHelper.Opponent="{x:Bind AccentAnalyzer.TertiaryAccentColor, Mode=OneWay}"
117117
Text="Tertiary" />
118118
</Border>
119119

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
2+
<Page x:Class="ColorAnalyzerExperiment.Samples.ContrastHelperSample"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:behaviors="using:CommunityToolkit.WinUI.Behaviors"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:helpers="using:CommunityToolkit.WinUI.Helpers"
8+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
9+
xmlns:local="using:ColorAnalyzerExperiment.Samples"
10+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
11+
mc:Ignorable="d">
12+
13+
<Grid>
14+
<Grid.RowDefinitions>
15+
<RowDefinition/>
16+
<RowDefinition Height="auto"/>
17+
</Grid.RowDefinitions>
18+
19+
<Grid.ColumnDefinitions>
20+
<ColumnDefinition/>
21+
<ColumnDefinition/>
22+
</Grid.ColumnDefinitions>
23+
24+
<Border Grid.ColumnSpan="2">
25+
<Border.Background>
26+
<SolidColorBrush Color="{x:Bind BackgroundPicker.Color, Mode=OneWay}"/>
27+
</Border.Background>
28+
<StackPanel Spacing="2">
29+
<TextBlock Text="Always legible text"
30+
FontSize="24"
31+
helpers:ContrastHelper.Opponent="{x:Bind BackgroundPicker.Color, Mode=OneWay}"/>
32+
<TextBlock Text="Legible text (MinRatio: 5)"
33+
FontSize="24"
34+
helpers:ContrastHelper.Opponent="{x:Bind BackgroundPicker.Color, Mode=OneWay}"
35+
helpers:ContrastHelper.MinRatio="5">
36+
<TextBlock.Foreground>
37+
<SolidColorBrush Color="{x:Bind ForegroundPicker.Color, Mode=OneWay}"/>
38+
</TextBlock.Foreground>
39+
</TextBlock>
40+
<TextBlock Text="Legible text (MinRatio: 3)"
41+
FontSize="16"
42+
helpers:ContrastHelper.Opponent="{x:Bind BackgroundPicker.Color, Mode=OneWay}"
43+
helpers:ContrastHelper.MinRatio="3">
44+
<TextBlock.Foreground>
45+
<SolidColorBrush Color="{x:Bind ForegroundPicker.Color, Mode=OneWay}"/>
46+
</TextBlock.Foreground>
47+
</TextBlock>
48+
<TextBlock Text="Potentially illegible text"
49+
FontSize="16">
50+
<TextBlock.Foreground>
51+
<SolidColorBrush Color="{x:Bind ForegroundPicker.Color, Mode=OneWay}"/>
52+
</TextBlock.Foreground>
53+
</TextBlock>
54+
</StackPanel>
55+
</Border>
56+
57+
<StackPanel Grid.Row="1"
58+
HorizontalAlignment="Center">
59+
<TextBlock Text="Foreground"/>
60+
<ColorPicker x:Name="ForegroundPicker"
61+
Color="Black"
62+
IsHexInputVisible="False"
63+
IsColorChannelTextInputVisible="False"/>
64+
</StackPanel>
65+
66+
<StackPanel Grid.Column="1"
67+
Grid.Row="1"
68+
HorizontalAlignment="Center">
69+
<TextBlock Text="Backround"/>
70+
<ColorPicker x:Name="BackgroundPicker"
71+
IsHexInputVisible="False"
72+
IsColorChannelTextInputVisible="False"/>
73+
</StackPanel>
74+
</Grid>
75+
</Page>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace ColorAnalyzerExperiment.Samples;
6+
7+
/// <summary>
8+
/// An example sample page of a custom control inheriting from Panel.
9+
/// </summary>
10+
[ToolkitSample(id: nameof(ContrastHelperSample), "ContrastAnalyzer helper", description: $"A sample for showing how the contrast analyzer can be used.")]
11+
public sealed partial class ContrastHelperSample : Page
12+
{
13+
public ContrastHelperSample()
14+
{
15+
this.InitializeComponent();
16+
}
17+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Windows.UI;
6+
7+
namespace CommunityToolkit.WinUI.Helpers;
8+
9+
public partial class ContrastHelper
10+
{
11+
/// <summary>
12+
/// An attached property that defines the color to compare against.
13+
/// </summary>
14+
public static readonly DependencyProperty OpponentProperty =
15+
DependencyProperty.RegisterAttached(
16+
"Opponent",
17+
typeof(Color),
18+
typeof(ContrastHelper),
19+
new PropertyMetadata(Colors.Transparent, OnOpponentChanged));
20+
21+
/// <summary>
22+
/// An attached property that defines the minimum acceptable contrast ratio against the opponent color.
23+
/// </summary>
24+
/// <remarks>
25+
/// Range: 1 to 21 (inclusive). Default is 21 (maximum contrast).
26+
/// </remarks>
27+
public static readonly DependencyProperty MinRatioProperty =
28+
DependencyProperty.RegisterAttached(
29+
"MinRatio",
30+
typeof(double),
31+
typeof(ContrastHelper),
32+
new PropertyMetadata(21d, OnMinRatioChanged));
33+
34+
/// <summary>
35+
/// An attached property that records the original color before adjusting for contrast.
36+
/// </summary>
37+
public static readonly DependencyProperty OriginalColorProperty =
38+
DependencyProperty.RegisterAttached(
39+
"Original",
40+
typeof(Color),
41+
typeof(ContrastHelper),
42+
new PropertyMetadata(Colors.Transparent));
43+
44+
// Tracks the callback on the original brush being updated.
45+
private static readonly DependencyProperty CallbackProperty =
46+
DependencyProperty.RegisterAttached(
47+
"Callback",
48+
typeof(long),
49+
typeof(ContrastHelper),
50+
new PropertyMetadata(0L));
51+
52+
/// <summary>
53+
/// Get the opponent color to compare against.
54+
/// </summary>
55+
/// <returns>The opponent color.</returns>
56+
public static Color GetOpponent(DependencyObject obj) => (Color)obj.GetValue(OpponentProperty);
57+
58+
/// <summary>
59+
/// Set the opponent color to compare against.
60+
/// </summary>
61+
public static void SetOpponent(DependencyObject obj, Color value) => obj.SetValue(OpponentProperty, value);
62+
63+
/// <summary>
64+
/// Get the minimum acceptable contrast ratio against the opponent color.
65+
/// </summary>
66+
public static double GetMinRatio(DependencyObject obj) => (double)obj.GetValue(MinRatioProperty);
67+
68+
/// <summary>
69+
/// Set the minimum acceptable contrast ratio against the opponent color.
70+
/// </summary>
71+
public static void SetMinRatio(DependencyObject obj, double value) => obj.SetValue(MinRatioProperty, value);
72+
73+
/// <summary>
74+
/// Gets the original color before adjustment for contrast.
75+
/// </summary>
76+
public static Color GetOriginal(DependencyObject obj) => (Color)obj.GetValue(OriginalColorProperty);
77+
78+
private static void SetOriginal(DependencyObject obj, Color color) => obj.SetValue(OriginalColorProperty, color);
79+
80+
private static long GetCallback(DependencyObject obj) => (long)obj.GetValue(CallbackProperty);
81+
82+
private static void SetCallback(DependencyObject obj, long value) => obj.SetValue(CallbackProperty, value);
83+
}

0 commit comments

Comments
 (0)