- 
                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 7 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
    
  
  
    
              
              
        
          
          
            75 changes: 75 additions & 0 deletions
          
          75 
        
  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,75 @@ | ||
| <!-- 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"> | ||
| 
     | 
||
| <Grid> | ||
| <Grid.RowDefinitions> | ||
| <RowDefinition /> | ||
| <RowDefinition Height="auto" /> | ||
| </Grid.RowDefinitions> | ||
| 
     | 
||
| <Grid.ColumnDefinitions> | ||
| <ColumnDefinition /> | ||
| <ColumnDefinition /> | ||
| </Grid.ColumnDefinitions> | ||
| 
     | 
||
| <Border Grid.ColumnSpan="2"> | ||
| <Border.Background> | ||
| <SolidColorBrush Color="{x:Bind BackgroundPicker.Color, Mode=OneWay}" /> | ||
| </Border.Background> | ||
| <StackPanel Spacing="2"> | ||
| <TextBlock helpers:ContrastHelper.Opponent="{x:Bind BackgroundPicker.Color, Mode=OneWay}" | ||
| FontSize="24" | ||
| Text="Always legible text" /> | ||
| <TextBlock helpers:ContrastHelper.MinRatio="5" | ||
| helpers:ContrastHelper.Opponent="{x:Bind BackgroundPicker.Color, Mode=OneWay}" | ||
| FontSize="24" | ||
| Text="Legible text (MinRatio: 5)"> | ||
| <TextBlock.Foreground> | ||
| <SolidColorBrush Color="{x:Bind ForegroundPicker.Color, Mode=OneWay}" /> | ||
| </TextBlock.Foreground> | ||
| </TextBlock> | ||
| <TextBlock helpers:ContrastHelper.MinRatio="3" | ||
| helpers:ContrastHelper.Opponent="{x:Bind BackgroundPicker.Color, Mode=OneWay}" | ||
| FontSize="16" | ||
| Text="Legible text (MinRatio: 3)"> | ||
| <TextBlock.Foreground> | ||
| <SolidColorBrush Color="{x:Bind ForegroundPicker.Color, Mode=OneWay}" /> | ||
| </TextBlock.Foreground> | ||
| </TextBlock> | ||
| <TextBlock FontSize="16" | ||
| Text="Potentially illegible text"> | ||
| <TextBlock.Foreground> | ||
| <SolidColorBrush Color="{x:Bind ForegroundPicker.Color, Mode=OneWay}" /> | ||
| </TextBlock.Foreground> | ||
| </TextBlock> | ||
| </StackPanel> | ||
| </Border> | ||
| 
     | 
||
| <StackPanel Grid.Row="1" | ||
| HorizontalAlignment="Center"> | ||
| <TextBlock Text="Desired Foreground" /> | ||
| <ColorPicker x:Name="ForegroundPicker" | ||
| IsColorChannelTextInputVisible="False" | ||
| IsHexInputVisible="False" | ||
| Color="Black" /> | ||
| </StackPanel> | ||
| 
     | 
||
| <StackPanel Grid.Row="1" | ||
| Grid.Column="1" | ||
| HorizontalAlignment="Center"> | ||
| <TextBlock Text="Backround" /> | ||
| <ColorPicker x:Name="BackgroundPicker" | ||
| IsColorChannelTextInputVisible="False" | ||
| IsHexInputVisible="False" /> | ||
| </StackPanel> | ||
| </Grid> | ||
| </Page> | 
        
          
          
            17 changes: 17 additions & 0 deletions
          
          17 
        
  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,17 @@ | ||
| // 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. | ||
| 
     | 
||
| 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 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
    
  
  
    
              
        
          
          
            102 changes: 102 additions & 0 deletions
          
          102 
        
  components/ColorAnalyzer/src/Contrast/ContrastHelper.Properties.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,102 @@ | ||
| // 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 Windows.UI; | ||
| 
     | 
||
| namespace CommunityToolkit.WinUI.Helpers; | ||
| 
     | 
||
| public partial class ContrastHelper | ||
| { | ||
| // TODO: Handle gradient brushes? | ||
| /// <summary> | ||
| /// An attached property that defines the color to compare against. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This property can be attached to any <see cref="TextBlock"/> or <see cref="Control"/> | ||
| /// to update their <see cref="TextBlock.Foreground"/> or <see cref="Control.Foreground"/>. | ||
| /// If the original Foreground is not a <see cref="SolidColorBrush"/>, it will always apply contrast. | ||
| /// It can also be attached to any <see cref="SolidColorBrush"/> to update the <see cref="SolidColorBrush.Color"/>. | ||
| /// </remarks> | ||
| public static readonly DependencyProperty OpponentProperty = | ||
| DependencyProperty.RegisterAttached( | ||
| "Opponent", | ||
| typeof(Color), | ||
| typeof(ContrastHelper), | ||
| new PropertyMetadata(Colors.Transparent, OnOpponentChanged)); | ||
| 
     | 
||
| /// <summary> | ||
| /// An attached property that defines the minimum acceptable contrast ratio against the opponent color. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Range: 1 to 21 (inclusive). Default is 21 (maximum contrast). | ||
| /// </remarks> | ||
| public static readonly DependencyProperty MinRatioProperty = | ||
| DependencyProperty.RegisterAttached( | ||
| "MinRatio", | ||
| typeof(double), | ||
| typeof(ContrastHelper), | ||
| new PropertyMetadata(21d, OnMinRatioChanged)); | ||
| 
     | 
||
| /// <summary> | ||
| /// An attached property that records the original color before adjusting for contrast. | ||
| /// </summary> | ||
| public static readonly DependencyProperty OriginalColorProperty = | ||
| DependencyProperty.RegisterAttached( | ||
| "Original", | ||
| typeof(Color), | ||
| typeof(ContrastHelper), | ||
| new PropertyMetadata(Colors.Transparent)); | ||
| 
     | 
||
| // Tracks the SolidColorBrush we're monitoring for changes | ||
| private static readonly DependencyProperty CallbackObjectProperty = | ||
| DependencyProperty.RegisterAttached( | ||
| "CallbackObject", | ||
| typeof(DependencyObject), | ||
| typeof(ContrastHelper), | ||
| new PropertyMetadata(null)); | ||
| 
     | 
||
| // Tracks the callback token from the SolidColorBrush we are monitoring | ||
| private static readonly DependencyProperty CallbackProperty = | ||
| DependencyProperty.RegisterAttached( | ||
| "Callback", | ||
| typeof(long), | ||
| typeof(ContrastHelper), | ||
| new PropertyMetadata(0L)); | ||
| 
     | 
||
| /// <summary> | ||
| /// Get the opponent color to compare against. | ||
| /// </summary> | ||
| /// <returns>The opponent color.</returns> | ||
| public static Color GetOpponent(DependencyObject obj) => (Color)obj.GetValue(OpponentProperty); | ||
| 
     | 
||
| /// <summary> | ||
| /// Set the opponent color to compare against. | ||
| /// </summary> | ||
| public static void SetOpponent(DependencyObject obj, Color value) => obj.SetValue(OpponentProperty, value); | ||
| 
     | 
||
| /// <summary> | ||
| /// Get the minimum acceptable contrast ratio against the opponent color. | ||
| /// </summary> | ||
| public static double GetMinRatio(DependencyObject obj) => (double)obj.GetValue(MinRatioProperty); | ||
| 
     | 
||
| /// <summary> | ||
| /// Set the minimum acceptable contrast ratio against the opponent color. | ||
| /// </summary> | ||
| public static void SetMinRatio(DependencyObject obj, double value) => obj.SetValue(MinRatioProperty, value); | ||
| 
     | 
||
| /// <summary> | ||
| /// Gets the original color before adjustment for contrast. | ||
| /// </summary> | ||
| public static Color GetOriginal(DependencyObject obj) => (Color)obj.GetValue(OriginalColorProperty); | ||
| 
     | 
||
| private static void SetOriginal(DependencyObject obj, Color color) => obj.SetValue(OriginalColorProperty, color); | ||
| 
     | 
||
| private static DependencyObject GetCallbackObject(DependencyObject obj) => (DependencyObject)obj.GetValue(CallbackObjectProperty); | ||
| 
     | 
||
| private static void SetCallbackObject(DependencyObject obj, DependencyObject dp) => obj.SetValue(CallbackObjectProperty, dp); | ||
| 
     | 
||
| private static long GetCallback(DependencyObject obj) => (long)obj.GetValue(CallbackProperty); | ||
| 
     | 
||
| private static void SetCallback(DependencyObject obj, long value) => obj.SetValue(CallbackProperty, value); | ||
| } | 
      
      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.