Skip to content

Commit 6534dee

Browse files
committed
Exposed AccentColorInfo data from accent analyzer
1 parent 58c6e78 commit 6534dee

File tree

4 files changed

+66
-26
lines changed

4 files changed

+66
-26
lines changed

components/AccentAnalyzer/src/AccentAnalyzer.Clustering.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,14 @@ private static int FindNearestClusterIndex(Vector3 point, Span<Vector3> centroid
147147
return nearestIndex;
148148
}
149149

150-
private static float FindColorfulness(Vector3 color)
150+
internal static float FindColorfulness(Vector3 color)
151151
{
152152
var rg = color.X - color.Y;
153153
var yb = ((color.X + color.Y) / 2) - color.Z;
154154
return 0.3f * new Vector2(rg, yb).Length();
155155
}
156156

157-
private static float FindColorfulness(Vector3[] colors)
157+
internal static float FindColorfulness(Vector3[] colors)
158158
{
159159
// Isolate rg and yb
160160
var rg = colors.Select(x => Math.Abs(x.X - x.Y));

components/AccentAnalyzer/src/AccentAnalyzer.Properties.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public partial class AccentAnalyzer
5252
public static readonly DependencyProperty ColorfulnessProperty =
5353
DependencyProperty.Register(nameof(Colorfulness), typeof(float), typeof(AccentAnalyzer), new PropertyMetadata(0f));
5454

55+
/// <summary>
56+
/// An event fired when the accent properties are updated.
57+
/// </summary>
58+
public event EventHandler? AccentsUpdated;
59+
5560
/// <summary>
5661
/// Gets the primary accent color as extracted from the <see cref="Source"/>.
5762
/// </summary>
@@ -125,6 +130,11 @@ public float Colorfulness
125130
private set => SetValue(ColorfulnessProperty, value);
126131
}
127132

133+
/// <summary>
134+
/// Gets the set of <see cref="AccentColorInfo"/> extracted on last update.
135+
/// </summary>
136+
public IReadOnlyList<AccentColorInfo>? AccentColors { get; private set; }
137+
128138
/// <summary>
129139
/// Gets a command that executes an accent update.
130140
/// </summary>
@@ -155,6 +165,8 @@ private void UpdateAccentProperties(Color primary, Color secondary, Color tertia
155165
DominantColor = dominantColor;
156166
BaseColor = baseColor;
157167
Colorfulness = colorfulness;
168+
169+
AccentsUpdated?.Invoke(this, EventArgs.Empty);
158170
});
159171
}
160172
}

components/AccentAnalyzer/src/AccentAnalyzer.cs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,6 @@ public void Execute(object? parameter)
4545
}
4646
}
4747

48-
private readonly struct AccentColorInfo
49-
{
50-
public AccentColorInfo(Vector3 rgb, float prominence)
51-
{
52-
Colorfulness = FindColorfulness(rgb);
53-
54-
rgb *= byte.MaxValue;
55-
Color = Color.FromArgb(byte.MaxValue, (byte)rgb.X, (byte)rgb.Y, (byte)rgb.Z);
56-
Prominence = prominence;
57-
}
58-
59-
public Color Color { get; }
60-
61-
public float Colorfulness { get; }
62-
63-
public float Prominence { get; }
64-
}
65-
6648
/// <summary>
6749
/// Initialize an instance of the <see cref="AccentAnalyzer"/> class.
6850
/// </summary>
@@ -98,25 +80,33 @@ private async Task UpdateAccentAsync()
9880
var colorData = clusters
9981
.Select((color, i) => new AccentColorInfo(color, (float)sizes[i] / samples.Length));
10082

83+
// Update accent colors property
84+
// Not a dependency property, so don't update form
85+
#if !WINDOWS_UWP
86+
AccentColors = [..colorData];
87+
#else
88+
AccentColors = colorData.ToList();
89+
#endif
90+
10191
// Select accent colors
10292
Color primary, secondary, tertiary, baseColor;
10393
(primary, secondary, tertiary, baseColor) = SelectAccents(colorData);
10494

10595
// Get dominant color by prominence
106-
#if NET6_0_OR_GREATER
96+
#if NET6_0_OR_GREATER
10797
var dominantColor = colorData
10898
.MaxBy(x => x.Prominence).Color;
109-
#else
99+
#else
110100
var dominantColor = colorData
111101
.OrderByDescending((x) => x.Prominence)
112102
.First().Color;
113-
#endif
103+
#endif
114104

115105
// Evaluate colorfulness
116106
// TODO: Should this be weighted by cluster sizes?
117107
var overallColorfulness = FindColorfulness(clusters);
118-
119-
// Set the various properties from the UI thread
108+
109+
// Update using the color data
120110
UpdateAccentProperties(primary, secondary, tertiary, baseColor, dominantColor, overallColorfulness);
121111
}
122112

@@ -192,7 +182,7 @@ private async Task<Vector3[]> SampleSourcePixelColorsAsync(int sampleCount)
192182
samples[colorIndex] = new Vector3(pixelBytes[2], pixelBytes[1], pixelBytes[0]) / byte.MaxValue;
193183
colorIndex++;
194184
}
195-
185+
196186
// If we skipped any pixels, trim the span
197187
#if !WINDOWS_UWP
198188
samples = samples[..colorIndex];
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 System.Numerics;
6+
using Windows.UI;
7+
8+
namespace CommunityToolkit.WinUI.Helpers;
9+
10+
/// <summary>
11+
/// A struct containing accent color info.
12+
/// </summary>
13+
public readonly struct AccentColorInfo
14+
{
15+
internal AccentColorInfo(Vector3 rgb, float prominence)
16+
{
17+
Colorfulness = AccentAnalyzer.FindColorfulness(rgb);
18+
19+
rgb *= byte.MaxValue;
20+
Color = Color.FromArgb(byte.MaxValue, (byte)rgb.X, (byte)rgb.Y, (byte)rgb.Z);
21+
Prominence = prominence;
22+
}
23+
24+
/// <summary>
25+
/// Gets the <see cref="Windows.UI.Color"/> of the accent color.
26+
/// </summary>
27+
public Color Color { get; }
28+
29+
/// <summary>
30+
/// Gets the colorfulness index of the accent color.
31+
/// </summary>
32+
public float Colorfulness { get; }
33+
34+
/// <summary>
35+
/// Gets the prominence of the accent color in the sampled image.
36+
/// </summary>
37+
public float Prominence { get; }
38+
}

0 commit comments

Comments
 (0)