Skip to content

Commit 9639efd

Browse files
committed
Added UWP alternatives for AccentAnalyzer
1 parent c4cd666 commit 9639efd

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

components/AccentAnalyzer/samples/AccentAnalyzerSample.xaml.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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.Xaml.Media.Imaging;
6-
75
namespace AccentAnalyzerExperiment.Samples;
86

97
/// <summary>

components/AccentAnalyzer/samples/AccentAnalyzerSampleOptionsPane.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
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+
#if !WINDOWS_UWP
56
using Microsoft.UI.Xaml.Media.Imaging;
7+
#elif WINDOWS_UWP
8+
using Windows.UI.Xaml.Media.Imaging;
9+
#endif
610

711
namespace AccentAnalyzerExperiment.Samples;
812

components/AccentAnalyzer/src/AccentAnalyzer.Clustering.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Numerics;
6+
using Windows.UI;
67

78
namespace CommunityToolkit.WinUI.Helpers;
89

@@ -21,7 +22,12 @@ private static Vector3[] KMeansCluster(Span<Vector3> points, int k, out int[] co
2122

2223
// Split the points into arbitrary clusters
2324
// NOTE: Can this be rearranged to converge faster?
25+
#if !WINDOWS_UWP
2426
var offset = Random.Shared.Next(k); // Mathematically true random sampling
27+
#elif WINDOWS_UWP
28+
var rand = new Random();
29+
var offset = rand.Next(k);
30+
#endif
2531
for (int i = 0; i < clusterIds.Length; i++)
2632
clusterIds[i] = (i + offset) % k;
2733

@@ -68,8 +74,14 @@ private static Vector3[] KMeansCluster(Span<Vector3> points, int k, out int[] co
6874
pivot--;
6975
counts[i] = counts[pivot];
7076
}
77+
78+
#if !WINDOWS_UWP
7179
counts = counts[..pivot];
7280
centroids = centroids[..pivot];
81+
#elif WINDOWS_UWP
82+
Array.Resize(ref counts, pivot);
83+
centroids = centroids.Slice(0, pivot);
84+
#endif
7385

7486
// Division step in centroid calculation
7587
for (int i = 0; i < centroids.Length; i++)

components/AccentAnalyzer/src/AccentAnalyzer.Properties.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
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+
#if !WINDOWS_UWP
56
using Microsoft.UI;
67
using Microsoft.UI.Dispatching;
7-
using System.Windows.Input;
8+
#elif WINDOWS_UWP
9+
using Windows.System;
810
using Windows.UI;
11+
#endif
12+
13+
using System.Windows.Input;
914

1015

1116
namespace CommunityToolkit.WinUI.Helpers;

components/AccentAnalyzer/src/AccentAnalyzer.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
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+
#if !WINDOWS_UWP
56
using Microsoft.UI;
67
using Microsoft.UI.Dispatching;
78
using Microsoft.UI.Xaml.Media.Imaging;
9+
#elif WINDOWS_UWP
10+
using Windows.UI;
11+
using Windows.UI.Xaml.Media.Imaging;
12+
#endif
13+
814
using System.Numerics;
915
using System.Windows.Input;
10-
using Windows.UI;
16+
using System.Linq;
1117

1218
namespace CommunityToolkit.WinUI.Helpers;
1319

@@ -69,8 +75,13 @@ private async Task UpdateAccentAsync()
6975
// Read the stream into a a color array
7076
int pos = 0;
7177
Span<Vector3> colors = new Vector3[(int)stream.Length / 4]; // This should be 4096 (64x64), but it's good to be safe.
78+
#if !WINDOWS_UWP
7279
Span<byte> bytes = stackalloc byte[4];
7380
while (stream.Read(bytes) > 0)
81+
#elif WINDOWS_UWP
82+
byte[] bytes = new byte[4];
83+
while(stream.Read(bytes, 0, 4) > 0)
84+
#endif
7485
{
7586
// Safety check to avoid going out of bounds
7687
// This should never happen, but it's good to be safe.
@@ -86,7 +97,11 @@ private async Task UpdateAccentAsync()
8697
}
8798

8899
// If we skipped any pixels, trim the span
100+
#if !WINDOWS_UWP
89101
colors = colors[..pos];
102+
#elif WINDOWS_UWP
103+
colors = colors.Slice(0, pos);
104+
#endif
90105

91106
// Determine most prominent colors and assess colorfulness
92107
int k = 6; // Should this be adjustable?
@@ -106,9 +121,17 @@ private async Task UpdateAccentAsync()
106121
var baseColor = accentColors.Last();
107122

108123
// Get base color by prominence
124+
#if !WINDOWS_UWP
109125
var dominant = clusters
110126
.Select((color, i) => (color, sizes[i]))
111127
.MaxBy(x => x.Item2).color * 255;
128+
#elif WINDOWS_UWP
129+
var dominant = clusters
130+
.Select((color, i) => (color, sizes[i]))
131+
.OrderByDescending((x) => x.Item2)
132+
.First().color * 255;
133+
#endif
134+
112135
var dominantColor = Color.FromArgb(255, (byte)dominant.X, (byte)dominant.Y, (byte)dominant.Z);
113136

114137
// Evaluate colorfulness

components/AccentAnalyzer/src/Dependencies.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<!-- WinUI 2 / UWP -->
1313
<ItemGroup Condition="'$(IsUwp)' == 'true'">
1414
<!-- <PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls.Primitives" Version="7.1.2"/> -->
15+
<PackageReference Include="System.Memory" Version="4.6.3"/>
1516
</ItemGroup>
1617

1718
<!-- WinUI 2 / Uno -->

0 commit comments

Comments
 (0)