Skip to content

Commit 1f07641

Browse files
committed
Created ContrastHelper samples
1 parent a37c0ef commit 1f07641

11 files changed

+185
-96
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: ContrastHelper
3+
author: Avid29
4+
description: A tool for extracting colors from an image
5+
keywords: Color, Helpers
6+
dev_langs:
7+
- csharp
8+
category: Helpers
9+
subcategory: Miscellaneous
10+
discussion-id: 254
11+
issue-id: 0
12+
icon: assets/icon.png
13+
---
14+
15+
# ContrastHelper
16+
17+
### Using on TextBlocks or Controls
18+
19+
> [!Sample TextBlockContrastSample]
20+
21+
### Using on SolidColorBrush
22+
23+
> [!Sample SolidColorBrushContrastSample]

components/ColorAnalyzer/samples/ContrastHelper/ContrastHelperSample.xaml

Lines changed: 0 additions & 39 deletions
This file was deleted.

components/ColorAnalyzer/samples/ContrastHelper/ContrastHelperSample.xaml.cs renamed to components/ColorAnalyzer/samples/ContrastHelper/ContrastHelperSampleBase.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using Microsoft.UI;
65
using Windows.UI;
76

87
namespace ColorAnalyzerExperiment.Samples;
98

10-
/// <summary>
11-
/// An example sample page of a custom control inheriting from Panel.
12-
/// </summary>
13-
[ToolkitSample(id: nameof(ContrastHelperSample), "ContrastAnalyzer helper", description: $"A sample for showing how the contrast analyzer can be used.")]
14-
public sealed partial class ContrastHelperSample : Page
9+
public abstract partial class ContrastHelperSampleBase : Page
1510
{
1611
public static readonly DependencyProperty DesiredBackgroundProperty =
17-
DependencyProperty.Register(nameof(DesiredBackground), typeof(Color), typeof(ContrastHelperSample), new PropertyMetadata(Colors.Black));
12+
DependencyProperty.Register(nameof(DesiredBackground), typeof(Color), typeof(TextBlockContrastSample), new PropertyMetadata(Colors.Black));
1813

1914
public static readonly DependencyProperty DesiredForegroundProperty =
20-
DependencyProperty.Register(nameof(DesiredForeground), typeof(Color), typeof(ContrastHelperSample), new PropertyMetadata(Colors.White));
15+
DependencyProperty.Register(nameof(DesiredForeground), typeof(Color), typeof(TextBlockContrastSample), new PropertyMetadata(Colors.White));
2116

2217
private static readonly DependencyProperty MinRatioProperty =
23-
DependencyProperty.Register(nameof(MinRatio), typeof(double), typeof(ContrastHelperSample), new PropertyMetadata(0d));
18+
DependencyProperty.Register(nameof(MinRatio), typeof(double), typeof(TextBlockContrastSample), new PropertyMetadata(3d));
19+
20+
public ContrastHelperSampleBase()
21+
{
22+
}
2423

2524
public Color DesiredBackground
2625
{
@@ -39,9 +38,4 @@ public double MinRatio
3938
get => (double)GetValue(MinRatioProperty);
4039
set => SetValue(MinRatioProperty, value);
4140
}
42-
43-
public ContrastHelperSample()
44-
{
45-
this.InitializeComponent();
46-
}
4741
}
Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="ColorAnalyzerExperiment.Samples.ContrastOptionsPane"
1+
<UserControl x:Class="ColorAnalyzerExperiment.Samples.ContrastOptionsPane"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
@@ -8,34 +8,30 @@
88
mc:Ignorable="d">
99

1010
<StackPanel Spacing="2">
11-
<TextBlock Text="Desired Foreground" />
12-
<controls:ColorPicker ColorChanged="Foreground_ColorChanged"
13-
IsColorChannelTextInputVisible="True"
14-
IsColorSliderVisible="True"
15-
Color="White" />
16-
17-
<TextBlock Text="Background" />
18-
<controls:ColorPicker ColorChanged="Background_ColorChanged"
19-
IsColorChannelTextInputVisible="True"
20-
IsColorSliderVisible="True"
21-
Color="Black" />
11+
<StackPanel Orientation="Horizontal">
12+
<StackPanel>
13+
<TextBlock Text="Desired Foreground" />
14+
<controls:ColorPicker ColorChanged="Foreground_ColorChanged"
15+
IsColorChannelTextInputVisible="True"
16+
IsColorSliderVisible="True"
17+
Color="White" />
18+
</StackPanel>
19+
<StackPanel>
20+
<TextBlock Text="Background" />
21+
<controls:ColorPicker ColorChanged="Background_ColorChanged"
22+
IsColorChannelTextInputVisible="True"
23+
IsColorSliderVisible="True"
24+
Color="Black" />
25+
</StackPanel>
26+
</StackPanel>
2227

2328
<Slider Header="Minimum Ratio"
2429
Maximum="21"
2530
Minimum="1"
31+
StepFrequency="0.1"
2632
ValueChanged="Ratio_ValueChanged"
33+
HorizontalAlignment="Center"
34+
MinWidth="256"
2735
Value="3" />
28-
29-
<Slider Header="Font Size"
30-
Maximum="48"
31-
Minimum="8"
32-
ValueChanged="FontSize_ValueChanged"
33-
Value="18" />
34-
35-
<Slider Header="Stroke Thickness"
36-
Maximum="8"
37-
Minimum="0"
38-
ValueChanged="Thickness_ValueChanged"
39-
Value="4" />
4036
</StackPanel>
4137
</UserControl>

components/ColorAnalyzer/samples/ContrastHelper/ContrastOptionsPane.xaml.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010

1111
namespace ColorAnalyzerExperiment.Samples;
1212

13-
[ToolkitSampleOptionsPane(nameof(ContrastHelperSample))]
13+
[ToolkitSampleOptionsPane(nameof(TextBlockContrastSample))]
14+
[ToolkitSampleOptionsPane(nameof(SolidColorBrushContrastSample))]
1415
public partial class ContrastOptionsPane : UserControl
1516
{
16-
private ContrastHelperSample _sample;
17-
private ContrastHelperSample.XamlNamedPropertyRelay _sampleXamlRelay;
17+
private ContrastHelperSampleBase _sample;
1818

19-
public ContrastOptionsPane(ContrastHelperSample sample)
19+
public ContrastOptionsPane(ContrastHelperSampleBase sample)
2020
{
2121
_sample = sample;
22-
_sampleXamlRelay = new ContrastHelperSample.XamlNamedPropertyRelay(sample);
2322

2423
this.InitializeComponent();
2524
}
@@ -46,14 +45,4 @@ private void Ratio_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
4645
{
4746
_sample.MinRatio = (double)e.NewValue;
4847
}
49-
50-
private void FontSize_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
51-
{
52-
_sampleXamlRelay.TextSample.FontSize = (double)e.NewValue;
53-
}
54-
55-
private void Thickness_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
56-
{
57-
_sampleXamlRelay.ShapeSample.StrokeThickness = (double)e.NewValue;
58-
}
5948
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
<local:ContrastHelperSampleBase x:Class="ColorAnalyzerExperiment.Samples.SolidColorBrushContrastSample"
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+
<local:ContrastHelperSampleBase.Background>
13+
<SolidColorBrush Color="{x:Bind DesiredBackground, Mode=OneWay}" />
14+
</local:ContrastHelperSampleBase.Background>
15+
16+
<StackPanel Margin="20" HorizontalAlignment="Center" VerticalAlignment="Center">
17+
<Rectangle Width="64"
18+
Height="48"
19+
StrokeThickness="2">
20+
<Rectangle.Stroke>
21+
<SolidColorBrush x:Name="BoundBrush"
22+
Color="{x:Bind DesiredForeground, Mode=OneWay}"
23+
helpers:ContrastHelper.MinRatio="{x:Bind MinRatio, Mode=OneWay}"
24+
helpers:ContrastHelper.Opponent="{x:Bind DesiredBackground, Mode=OneWay}"/>
25+
</Rectangle.Stroke>
26+
</Rectangle>
27+
<TextBlock Text="{x:Bind BoundBrush.(helpers:ContrastHelper.ContrastRatio), Mode=OneWay}"
28+
helpers:ContrastHelper.MinRatio="{x:Bind MinRatio, Mode=OneWay}"
29+
helpers:ContrastHelper.Opponent="{x:Bind DesiredBackground, Mode=OneWay}"
30+
FontSize="24">
31+
<TextBlock.Foreground>
32+
<SolidColorBrush Color="{x:Bind DesiredForeground, Mode=OneWay}" />
33+
</TextBlock.Foreground>
34+
</TextBlock>
35+
</StackPanel>
36+
37+
</local:ContrastHelperSampleBase>
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(SolidColorBrushContrastSample), "ContrastAnalyzer helper", description: $"A sample for showing how the contrast analyzer can be used.")]
11+
public sealed partial class SolidColorBrushContrastSample : ContrastHelperSampleBase
12+
{
13+
public SolidColorBrushContrastSample()
14+
{
15+
this.InitializeComponent();
16+
}
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
<local:ContrastHelperSampleBase x:Class="ColorAnalyzerExperiment.Samples.TextBlockContrastSample"
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+
<local:ContrastHelperSampleBase.Background>
13+
<SolidColorBrush Color="{x:Bind DesiredBackground, Mode=OneWay}" />
14+
</local:ContrastHelperSampleBase.Background>
15+
16+
<StackPanel Orientation="Horizontal" Margin="20" HorizontalAlignment="Center" VerticalAlignment="Center">
17+
<TextBlock x:Name="BoundTextBlock"
18+
Text="Contrast Ratio:"
19+
helpers:ContrastHelper.MinRatio="{x:Bind MinRatio, Mode=OneWay}"
20+
helpers:ContrastHelper.Opponent="{x:Bind DesiredBackground, Mode=OneWay}"
21+
FontSize="24">
22+
<TextBlock.Foreground>
23+
<SolidColorBrush Color="{x:Bind DesiredForeground, Mode=OneWay}" />
24+
</TextBlock.Foreground>
25+
</TextBlock>
26+
<TextBlock Text="{x:Bind BoundTextBlock.(helpers:ContrastHelper.ContrastRatio), Mode=OneWay}"
27+
helpers:ContrastHelper.MinRatio="{x:Bind MinRatio, Mode=OneWay}"
28+
helpers:ContrastHelper.Opponent="{x:Bind DesiredBackground, Mode=OneWay}"
29+
FontSize="24"
30+
Margin="4,0,0,0">
31+
<TextBlock.Foreground>
32+
<SolidColorBrush Color="{x:Bind DesiredForeground, Mode=OneWay}" />
33+
</TextBlock.Foreground>
34+
</TextBlock>
35+
</StackPanel>
36+
37+
</local:ContrastHelperSampleBase>
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(TextBlockContrastSample), "ContrastAnalyzer helper", description: $"A sample for showing how the contrast analyzer can be used.")]
11+
public sealed partial class TextBlockContrastSample : ContrastHelperSampleBase
12+
{
13+
public TextBlockContrastSample()
14+
{
15+
this.InitializeComponent();
16+
}
17+
}

components/ColorAnalyzer/src/ColorExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal static double ContrastRatio(this Color color1, Color color2)
3838
double darker = Math.Min(luminance1, luminance2);
3939

4040
// Calculate contrast ratio
41-
return (lighter + 0.05f) / (darker + 0.05f);
41+
return (lighter + 0.05) / (darker + 0.05);
4242
}
4343

4444
internal static double PerceivedLuminance(this Color color)

0 commit comments

Comments
 (0)