-
Notifications
You must be signed in to change notification settings - Fork 79
ContrastHelper #747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Arlodotexe
merged 14 commits into
CommunityToolkit:main
from
Avid29:ColorAnalyzer/Contrast
Oct 10, 2025
Merged
ContrastHelper #747
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
225404e
ContrastAnalyzer for Foreground adjustments against opponent
Avid29 d071fa9
Added MinRatio property for ContrastAnalyzer to specify threshold WCA…
Avid29 a7e534b
Renamed ContrastAnalyzer to ContrastHelper and repaired binding issues
Avid29 fd2fb18
Applied XAML styling
Avid29 6cbdcb5
Relabeled Foreground ColorPicker as Desired Foreground to improve usa…
Avid29 8cb46a0
Fixed callback subscription edge cases
Avid29 615d355
Added missing code to handle transparent base color as a sentinel value
Avid29 9eb4fd8
Refactor ContrastHelper and added ContrastRatio and OriginalContrastR…
Avid29 a04eca4
Changes default image on AccentAnalyzerSample to flowers, from the re…
Avid29 103ed36
Improved ContrastHelper samples
Avid29 bb3c1c6
Applied XAML styles
Avid29 1ea8925
Merge branch 'main' into ColorAnalyzer/Contrast
Avid29 7d20d6a
Update components/ColorAnalyzer/samples/AccentAnalyzerSample.xaml
Arlodotexe 2afc220
Run xaml styler
Avid29 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
components/ColorAnalyzer/samples/ContrastHelperSample.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <!-- 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. --> | ||
| <Page x:Class="ColorAnalyzerExperiment.Samples.ContrastHelperSample" | ||
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| xmlns:behaviors="using:CommunityToolkit.WinUI.Behaviors" | ||
| xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
| xmlns:helpers="using:CommunityToolkit.WinUI.Helpers" | ||
| xmlns:interactivity="using:Microsoft.Xaml.Interactivity" | ||
| xmlns:local="using:ColorAnalyzerExperiment.Samples" | ||
| xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
| mc:Ignorable="d"> | ||
| <Page.Background> | ||
| <SolidColorBrush Color="{x:Bind DesiredBackground, Mode=OneWay}" /> | ||
| </Page.Background> | ||
|
|
||
| <StackPanel Padding="20" | ||
| HorizontalAlignment="Center" | ||
| VerticalAlignment="Top" | ||
| Spacing="2"> | ||
| <TextBlock x:Name="TextSample" | ||
| helpers:ContrastHelper.MinRatio="{x:Bind MinRatio, Mode=OneWay}" | ||
| helpers:ContrastHelper.Opponent="{x:Bind DesiredBackground, Mode=OneWay}" | ||
| FontSize="24" | ||
| Text="Legible Text"> | ||
| <TextBlock.Foreground> | ||
| <SolidColorBrush Color="{x:Bind DesiredForeground, Mode=OneWay}" /> | ||
| </TextBlock.Foreground> | ||
| </TextBlock> | ||
| <Rectangle x:Name="ShapeSample" | ||
| Width="28" | ||
| Height="28"> | ||
| <Rectangle.Stroke> | ||
| <SolidColorBrush helpers:ContrastHelper.MinRatio="{x:Bind MinRatio, Mode=OneWay}" | ||
| helpers:ContrastHelper.Opponent="{x:Bind DesiredBackground, Mode=OneWay}" | ||
| Color="{x:Bind DesiredForeground, Mode=OneWay}" /> | ||
| </Rectangle.Stroke> | ||
| </Rectangle> | ||
| </StackPanel> | ||
| </Page> |
47 changes: 47 additions & 0 deletions
47
components/ColorAnalyzer/samples/ContrastHelperSample.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // 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. | ||
|
|
||
| using Microsoft.UI; | ||
| using Windows.UI; | ||
|
|
||
| namespace ColorAnalyzerExperiment.Samples; | ||
|
|
||
| /// <summary> | ||
| /// An example sample page of a custom control inheriting from Panel. | ||
| /// </summary> | ||
| [ToolkitSample(id: nameof(ContrastHelperSample), "ContrastAnalyzer helper", description: $"A sample for showing how the contrast analyzer can be used.")] | ||
| public sealed partial class ContrastHelperSample : Page | ||
| { | ||
| public static readonly DependencyProperty DesiredBackgroundProperty = | ||
| DependencyProperty.Register(nameof(DesiredBackground), typeof(Color), typeof(ContrastHelperSample), new PropertyMetadata(Colors.Black)); | ||
|
|
||
| public static readonly DependencyProperty DesiredForegroundProperty = | ||
| DependencyProperty.Register(nameof(DesiredForeground), typeof(Color), typeof(ContrastHelperSample), new PropertyMetadata(Colors.White)); | ||
|
|
||
| private static readonly DependencyProperty MinRatioProperty = | ||
| DependencyProperty.Register(nameof(MinRatio), typeof(double), typeof(ContrastHelperSample), new PropertyMetadata(0d)); | ||
|
|
||
| public Color DesiredBackground | ||
| { | ||
| get => (Color)GetValue(DesiredBackgroundProperty); | ||
| set => SetValue(DesiredBackgroundProperty, value); | ||
| } | ||
|
|
||
| public Color DesiredForeground | ||
| { | ||
| get => (Color)GetValue(DesiredForegroundProperty); | ||
| set => SetValue(DesiredForegroundProperty, value); | ||
| } | ||
|
|
||
| public double MinRatio | ||
| { | ||
| get => (double)GetValue(MinRatioProperty); | ||
| set => SetValue(MinRatioProperty, value); | ||
| } | ||
|
|
||
| public ContrastHelperSample() | ||
| { | ||
| this.InitializeComponent(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <UserControl x:Class="ColorAnalyzerExperiment.Samples.ContrastOptionsPane" | ||
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| xmlns:controls="using:CommunityToolkit.WinUI.Controls" | ||
| xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
| xmlns:local="using:ColorAnalyzerExperiment.Samples" | ||
| xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
| mc:Ignorable="d"> | ||
|
|
||
| <StackPanel Spacing="2"> | ||
| <TextBlock Text="Desired Foreground" /> | ||
| <controls:ColorPicker ColorChanged="Foreground_ColorChanged" | ||
| IsColorChannelTextInputVisible="True" | ||
| IsColorSliderVisible="True" | ||
| Color="White" /> | ||
|
|
||
| <TextBlock Text="Background" /> | ||
| <controls:ColorPicker ColorChanged="Background_ColorChanged" | ||
| IsColorChannelTextInputVisible="True" | ||
| IsColorSliderVisible="True" | ||
| Color="Black" /> | ||
|
|
||
| <Slider Header="Minimum Ratio" | ||
| Maximum="21" | ||
| Minimum="1" | ||
| ValueChanged="Ratio_ValueChanged" | ||
| Value="3" /> | ||
|
|
||
| <Slider Header="Font Size" | ||
| Maximum="48" | ||
| Minimum="8" | ||
| ValueChanged="FontSize_ValueChanged" | ||
| Value="18" /> | ||
|
|
||
| <Slider Header="Stroke Thickness" | ||
| Maximum="8" | ||
| Minimum="0" | ||
| ValueChanged="Thickness_ValueChanged" | ||
| Value="4" /> | ||
| </StackPanel> | ||
| </UserControl> |
59 changes: 59 additions & 0 deletions
59
components/ColorAnalyzer/samples/ContrastOptionsPane.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // 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. | ||
|
|
||
| #if !WINDOWS_UWP | ||
| using Microsoft.UI.Xaml.Media.Imaging; | ||
| #elif WINDOWS_UWP | ||
| using Windows.UI.Xaml.Media.Imaging; | ||
| #endif | ||
|
|
||
| namespace ColorAnalyzerExperiment.Samples; | ||
|
|
||
| [ToolkitSampleOptionsPane(nameof(ContrastHelperSample))] | ||
| public partial class ContrastOptionsPane : UserControl | ||
| { | ||
| private ContrastHelperSample _sample; | ||
| private ContrastHelperSample.XamlNamedPropertyRelay _sampleXamlRelay; | ||
|
|
||
| public ContrastOptionsPane(ContrastHelperSample sample) | ||
| { | ||
| _sample = sample; | ||
| _sampleXamlRelay = new ContrastHelperSample.XamlNamedPropertyRelay(sample); | ||
|
|
||
| this.InitializeComponent(); | ||
| } | ||
|
|
||
| private void Foreground_ColorChanged(MUXC.ColorPicker sender, MUXC.ColorChangedEventArgs args) | ||
| { | ||
| // TODO: Disect the colorpicker | ||
| if (args.NewColor.A != 255) | ||
| return; | ||
|
|
||
| _sample.DesiredForeground = args.NewColor; | ||
| } | ||
|
|
||
| private void Background_ColorChanged(MUXC.ColorPicker sender, MUXC.ColorChangedEventArgs args) | ||
| { | ||
| // TODO: Disect the colorpicker | ||
| if (args.NewColor.A != 255) | ||
| return; | ||
|
|
||
| _sample.DesiredBackground = args.NewColor; | ||
| } | ||
|
|
||
| private void Ratio_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) | ||
| { | ||
| _sample.MinRatio = (double)e.NewValue; | ||
| } | ||
|
|
||
| private void FontSize_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) | ||
| { | ||
| _sampleXamlRelay.TextSample.FontSize = (double)e.NewValue; | ||
| } | ||
|
|
||
| private void Thickness_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) | ||
| { | ||
| _sampleXamlRelay.ShapeSample.StrokeThickness = (double)e.NewValue; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.