4
4
5
5
#if ! WINAPPSDK
6
6
global using Windows . UI . Xaml . Media . Imaging ;
7
+ global using Windows . System ;
7
8
#else
8
9
global using Microsoft . UI ;
9
10
global using Microsoft . UI . Dispatching ;
10
11
global using Microsoft . UI . Xaml . Media . Imaging ;
11
- using System ;
12
-
13
12
#endif
14
13
15
14
using System . Numerics ;
16
15
using System . Windows . Input ;
17
- using Windows . UI ;
18
-
19
16
20
17
namespace CommunityToolkit . WinUI . Helpers ;
21
18
@@ -79,38 +76,41 @@ private async Task UpdateAccentAsync()
79
76
var clusters = KMeansCluster ( samples , k , out var sizes ) ;
80
77
var colorData = clusters
81
78
. Select ( ( color , i ) => new AccentColorInfo ( color , ( float ) sizes [ i ] / samples . Length ) ) ;
79
+
80
+ // Evaluate colorfulness
81
+ // TODO: Should this be weighted by cluster sizes?
82
+ var overallColorfulness = FindColorfulness ( clusters ) ;
83
+
84
+ // Select accent colors
85
+ SelectAccentColors ( colorData , overallColorfulness ) ;
82
86
83
87
// Update accent colors property
84
- // Not a dependency property, so don't update form
88
+ // Not a dependency property, so no need to update from the UI Thread
85
89
#if ! WINDOWS_UWP
86
90
AccentColors = [ ..colorData ] ;
87
91
#else
88
92
AccentColors = colorData . ToList ( ) ;
89
93
#endif
90
94
91
- // Select accent colors
92
- Color primary , secondary , tertiary , baseColor ;
93
- ( primary , secondary , tertiary , baseColor ) = SelectAccents ( colorData ) ;
94
-
95
- // Get dominant color by prominence
96
- #if NET6_0_OR_GREATER
97
- var dominantColor = colorData
98
- . MaxBy ( x => x . Prominence ) . Color ;
99
- #else
100
- var dominantColor = colorData
101
- . OrderByDescending ( ( x ) => x . Prominence )
102
- . First ( ) . Color ;
103
- #endif
104
-
105
- // Evaluate colorfulness
106
- // TODO: Should this be weighted by cluster sizes?
107
- var overallColorfulness = FindColorfulness ( clusters ) ;
108
-
109
- // Update using the color data
110
- UpdateAccentProperties ( primary , secondary , tertiary , baseColor , dominantColor , overallColorfulness ) ;
95
+ // Update the colorfulness and invoke accents updated event,
96
+ // both from the UI thread
97
+ DispatcherQueue . GetForCurrentThread ( ) . TryEnqueue ( ( ) =>
98
+ {
99
+ Colorfulness = overallColorfulness ;
100
+ AccentsUpdated ? . Invoke ( this , EventArgs . Empty ) ;
101
+ } ) ;
111
102
}
112
103
113
- private ( Color primary , Color secondary , Color tertiary , Color baseColor ) SelectAccents ( IEnumerable < AccentColorInfo > colorData )
104
+ /// <summary>
105
+ /// This method takes the processed color information and selects the accent colors from it.
106
+ /// </summary>
107
+ /// <remarks>
108
+ /// There is no guarentee that this method will be called from the UI Thread.
109
+ /// Dependency properties should be updated using a dispatcher.
110
+ /// </remarks>
111
+ /// <param name="colorData">The analyzed accent color info from the image.</param>
112
+ /// <param name="imageColorfulness">The overall colorfulness of the image.</param>
113
+ protected virtual void SelectAccentColors ( IEnumerable < AccentColorInfo > colorData , float imageColorfulness )
114
114
{
115
115
// Select accent colors
116
116
var accentColors = colorData
@@ -127,8 +127,25 @@ private async Task UpdateAccentAsync()
127
127
// Get base color
128
128
var baseColor = accentColors . Last ( ) ;
129
129
130
- // Return palette
131
- return ( primary , secondary , tertiary , baseColor ) ;
130
+ // Get dominant color by prominence
131
+ #if NET6_0_OR_GREATER
132
+ var dominantColor = colorData
133
+ . MaxBy ( x => x . Prominence ) . Color ;
134
+ #else
135
+ var dominantColor = colorData
136
+ . OrderByDescending ( ( x ) => x . Prominence )
137
+ . First ( ) . Color ;
138
+ #endif
139
+
140
+ // Batch update the dependency properties in the UI Thread
141
+ DispatcherQueue . GetForCurrentThread ( ) . TryEnqueue ( ( ) =>
142
+ {
143
+ PrimaryAccentColor = primary ;
144
+ SecondaryAccentColor = secondary ;
145
+ TertiaryAccentColor = tertiary ;
146
+ BaseColor = baseColor ;
147
+ DominantColor = dominantColor ;
148
+ } ) ;
132
149
}
133
150
134
151
private async Task < Vector3 [ ] > SampleSourcePixelColorsAsync ( int sampleCount )
0 commit comments