Skip to content

Commit c3b89d1

Browse files
committed
Changed Source to a dependency property, and added more comments on AccentAnalyzer
1 parent 1805ea0 commit c3b89d1

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

components/ColorAnalyzer/src/AccentAnalyzer.Properties.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ namespace CommunityToolkit.WinUI.Helpers;
99

1010
public partial class AccentAnalyzer
1111
{
12-
private UIElement? _source;
12+
/// <summary>
13+
/// Gets the <see cref="DependencyProperty"/> for the <see cref="Source"/> property.
14+
/// </summary>
15+
public static readonly DependencyProperty SourceProperty =
16+
DependencyProperty.Register(nameof(Source), typeof(UIElement), typeof(AccentAnalyzer), new PropertyMetadata(null, OnSourceChanged));
1317

1418
/// <summary>
1519
/// Gets the <see cref="DependencyProperty"/> for the <see cref="PrimaryAccentColor"/> property.
@@ -52,6 +56,15 @@ public partial class AccentAnalyzer
5256
/// </summary>
5357
public event EventHandler? AccentsUpdated;
5458

59+
/// <summary>
60+
/// Gets or sets the <see cref="UIElement"/> source for accent color analysis.
61+
/// </summary>
62+
public UIElement? Source
63+
{
64+
get => (UIElement)GetValue(SourceProperty);
65+
set => SetValue(SourceProperty, value);
66+
}
67+
5568
/// <summary>
5669
/// Gets the primary accent color as extracted from the <see cref="Source"/>.
5770
/// </summary>
@@ -111,13 +124,16 @@ public Color DominantColor
111124
get => (Color)GetValue(DominantColorProperty);
112125
protected set => SetValue(DominantColorProperty, value);
113126
}
114-
127+
115128
/// <summary>
116129
/// Gets the "colorfulness" of the <see cref="Source"/>.
117130
/// </summary>
118131
/// <remarks>
119132
/// Colorfulness is defined by David Hasler and Sabine Susstrunk's paper on measuring colorfulness
120-
/// <seealso href="https://infoscience.epfl.ch/server/api/core/bitstreams/77f5adab-e825-4995-92db-c9ff4cd8bf5a/content"/>
133+
/// <seealso href="https://infoscience.epfl.ch/server/api/core/bitstreams/77f5adab-e825-4995-92db-c9ff4cd8bf5a/content"/>.
134+
///
135+
/// An image with colors of high saturation and value will have a high colorfulness (around 1),
136+
/// meanwhile images that are mostly gray or white will have a low colorfulness (around 0).
121137
/// </remarks>
122138
public float Colorfulness
123139
{
@@ -135,18 +151,11 @@ public float Colorfulness
135151
/// </summary>
136152
public ICommand AccentUpdateCommand { get; }
137153

138-
/// <summary>
139-
/// Gets or sets the <see cref="UIElement"/>
140-
/// </summary>
141-
public UIElement? Source
154+
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
142155
{
143-
get => _source;
144-
set => SetSource(value);
145-
}
156+
if (d is not AccentAnalyzer analyzer)
157+
return;
146158

147-
private void SetSource(UIElement? source)
148-
{
149-
_source = source;
150-
_ = UpdateAccentAsync();
159+
_ = analyzer.UpdateAccentAsync();
151160
}
152161
}

components/ColorAnalyzer/src/AccentAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ private async Task<Vector3[]> SampleSourcePixelColorsAsync(int sampleCount)
164164
var sourceArea = sourceSize.X * sourceSize.Y;
165165
var sampleScale = MathF.Sqrt(sampleCount / sourceArea);
166166
var sampleSize = sourceSize * sampleScale;
167-
167+
168168
// Rerender the UIElement to a bitmap of about sampleCount pixels
169+
// Note: RenderTargetBitmap is not supported with Uno Platform.
169170
var bitmap = new RenderTargetBitmap();
170171
await bitmap.RenderAsync(Source, (int)sampleSize.X, (int)sampleSize.Y);
171172

components/ColorAnalyzer/src/AccentColorInfo.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ internal AccentColorInfo(Vector3 rgb, float prominence)
2929
/// <summary>
3030
/// Gets the colorfulness index of the accent color.
3131
/// </summary>
32+
/// <remarks>
33+
/// The exact definition of colorfulness is defined by David Hasler and Sabine Susstrunk's paper on measuring colorfulness
34+
/// <seealso href="https://infoscience.epfl.ch/server/api/core/bitstreams/77f5adab-e825-4995-92db-c9ff4cd8bf5a/content"/>.
35+
///
36+
/// Colors of high saturation and value will have a high colorfulness (around 1),
37+
/// while colors that are mostly gray or white will have a low colorfulness (around 0).
38+
/// </remarks>
3239
public float Colorfulness { get; }
3340

3441
/// <summary>

components/ColorAnalyzer/src/MultiTarget.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
MultiTarget is a custom property that indicates which target a project is designed to be built for / run on.
55
Used to create project references, generate solution files, enable/disable TargetFrameworks, and build nuget packages.
66
-->
7+
<!-- Uno is not currently supported because this component depends on the RenderTargetBitmap.-->
78
<MultiTarget>uwp;wasdk;</MultiTarget>
89
</PropertyGroup>
910
</Project>

0 commit comments

Comments
 (0)