Skip to content

Commit b1f968c

Browse files
committed
Added fallback behavior for secondary tertiary values
1 parent 6534dee commit b1f968c

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

components/AccentAnalyzer/src/AccentAnalyzer.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,14 @@ private async Task UpdateAccentAsync()
115115
// Select accent colors
116116
var accentColors = colorData
117117
.OrderByDescending(x => x.Colorfulness)
118-
.Take(3)
119118
.Select(x => x.Color);
120119

121120
// Get primary/secondary/tertiary accents
122121
var primary = accentColors.First();
123122
var secondary = accentColors.ElementAtOrDefault(1);
123+
secondary = secondary != default ? secondary : primary;
124124
var tertiary = accentColors.ElementAtOrDefault(2);
125+
tertiary = tertiary != default ? tertiary : secondary;
125126

126127
// Get base color
127128
var baseColor = accentColors.Last();
@@ -138,18 +139,18 @@ private async Task<Vector3[]> SampleSourcePixelColorsAsync(int sampleCount)
138139

139140
// Grab actual size
140141
// If actualSize is 0, replace with 1:1 aspect ratio
141-
var actualSize = Source.ActualSize;
142-
actualSize = actualSize != Vector2.Zero ? actualSize : Vector2.One;
142+
var sourceSize = Source.ActualSize;
143+
sourceSize = sourceSize != Vector2.Zero ? sourceSize : Vector2.One;
143144

144145
// Calculate size of scaled rerender using the actual size
145146
// scaled down to the sample count, maintaining aspect ration
146-
var actualArea = actualSize.X * actualSize.Y;
147-
var scale = MathF.Sqrt(sampleCount / actualArea);
148-
var scaledSize = actualSize * scale;
147+
var sourceArea = sourceSize.X * sourceSize.Y;
148+
var sampleScale = MathF.Sqrt(sampleCount / sourceArea);
149+
var sampleSize = sourceSize * sampleScale;
149150

150151
// Rerender the UIElement to a bitmap of about sampleCount pixels
151152
var bitmap = new RenderTargetBitmap();
152-
await bitmap.RenderAsync(Source, (int)scaledSize.X, (int)scaledSize.Y);
153+
await bitmap.RenderAsync(Source, (int)sampleSize.X, (int)sampleSize.Y);
153154

154155
// Create a stream from the bitmap
155156
var pixels = await bitmap.GetPixelsAsync();
@@ -161,7 +162,7 @@ private async Task<Vector3[]> SampleSourcePixelColorsAsync(int sampleCount)
161162

162163
// Read the stream into a a color array
163164
const int bytesPerPixel = 4;
164-
Vector3[] samples = new Vector3[(int)pixelByteStream.Length / bytesPerPixel];
165+
var samples = new Vector3[(int)pixelByteStream.Length / bytesPerPixel];
165166

166167
// Iterate through the stream reading a pixel (4 bytes) at a time
167168
// and storing them as a Vector3. Opacity info is dropped.

0 commit comments

Comments
 (0)