Skip to content

Commit b71e3ee

Browse files
author
Johann Dirry
committed
Backporting to .NET 462
1 parent 33e1ebd commit b71e3ee

File tree

10 files changed

+98
-48
lines changed

10 files changed

+98
-48
lines changed

Directory.packages.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@
2929
<PackageVersion Include="TUnit" Version="0.25.21" />
3030
<PackageVersion Include="VirtualizingWrapPanel" Version="1.5.8" />
3131
<PackageVersion Include="XAMLTest" Version="1.3.0-ci616" />
32+
<PackageVersion Include="System.Memory" Version="4.6.3" />
33+
<PackageVersion Include="System.ValueTuple" Version="4.6.1" />
34+
<PackageVersion Include="Polyfill" Version="8.8.1" />
3235
</ItemGroup>
3336
</Project>

src/MaterialDesign3.MaterialColorUtilities/DynamicColor/ColorSpec2021.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -777,14 +777,14 @@ public TonalPalette GetSecondaryPalette(Variant variant, Hct sourceColorHct, boo
777777
Variant.EXPRESSIVE => TonalPalette.FromHueAndChroma(
778778
DynamicScheme.GetRotatedHue(
779779
sourceColorHct,
780-
new[] {0.0, 21.0, 51.0, 121.0, 151.0, 191.0, 271.0, 321.0, 360.0},
781-
new[] {45.0, 95.0, 45.0, 20.0, 45.0, 90.0, 45.0, 45.0, 45.0}
780+
[0.0, 21.0, 51.0, 121.0, 151.0, 191.0, 271.0, 321.0, 360.0],
781+
[45.0, 95.0, 45.0, 20.0, 45.0, 90.0, 45.0, 45.0, 45.0]
782782
), 24.0),
783783
Variant.VIBRANT => TonalPalette.FromHueAndChroma(
784784
DynamicScheme.GetRotatedHue(
785785
sourceColorHct,
786-
new[] {0.0, 41.0, 61.0, 101.0, 131.0, 181.0, 251.0, 301.0, 360.0},
787-
new[] {18.0, 15.0, 10.0, 12.0, 15.0, 18.0, 15.0, 12.0, 12.0}
786+
[0.0, 41.0, 61.0, 101.0, 131.0, 181.0, 251.0, 301.0, 360.0],
787+
[18.0, 15.0, 10.0, 12.0, 15.0, 18.0, 15.0, 12.0, 12.0]
788788
), 24.0),
789789
_ => TonalPalette.FromHueAndChroma(sourceColorHct.Hue, sourceColorHct.Chroma),
790790
};
@@ -803,14 +803,14 @@ public TonalPalette GetTertiaryPalette(Variant variant, Hct sourceColorHct, bool
803803
Variant.EXPRESSIVE => TonalPalette.FromHueAndChroma(
804804
DynamicScheme.GetRotatedHue(
805805
sourceColorHct,
806-
new[] {0.0, 21.0, 51.0, 121.0, 151.0, 191.0, 271.0, 321.0, 360.0},
807-
new[] {120.0, 120.0, 20.0, 45.0, 20.0, 15.0, 20.0, 120.0, 120.0}
806+
[0.0, 21.0, 51.0, 121.0, 151.0, 191.0, 271.0, 321.0, 360.0],
807+
[120.0, 120.0, 20.0, 45.0, 20.0, 15.0, 20.0, 120.0, 120.0]
808808
), 32.0),
809809
Variant.VIBRANT => TonalPalette.FromHueAndChroma(
810810
DynamicScheme.GetRotatedHue(
811811
sourceColorHct,
812-
new[] {0.0, 41.0, 61.0, 101.0, 131.0, 181.0, 251.0, 301.0, 360.0},
813-
new[] {35.0, 30.0, 20.0, 25.0, 30.0, 35.0, 30.0, 25.0, 25.0}
812+
[0.0, 41.0, 61.0, 101.0, 131.0, 181.0, 251.0, 301.0, 360.0],
813+
[35.0, 30.0, 20.0, 25.0, 30.0, 35.0, 30.0, 25.0, 25.0]
814814
), 32.0),
815815
_ => TonalPalette.FromHueAndChroma(sourceColorHct.Hue, sourceColorHct.Chroma),
816816
};

src/MaterialDesign3.MaterialColorUtilities/DynamicColor/ColorSpec2025.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,14 +1389,14 @@ private static ContrastCurve GetContrastCurve(double @default)
13891389
Variant.EXPRESSIVE => TonalPalette.FromHueAndChroma(
13901390
DynamicScheme.GetRotatedHue(
13911391
sourceColorHct,
1392-
new[] {0.0, 105.0, 140.0, 204.0, 253.0, 278.0, 300.0, 333.0, 360.0},
1393-
new[] {-160.0, 155.0, -100.0, 96.0, -96.0, -156.0, -165.0, -160.0}
1392+
[0.0, 105.0, 140.0, 204.0, 253.0, 278.0, 300.0, 333.0, 360.0],
1393+
[-160.0, 155.0, -100.0, 96.0, -96.0, -156.0, -165.0, -160.0]
13941394
), platform == Platform.PHONE ? (isDark ? 16 : 24) : 24),
13951395
Variant.VIBRANT => TonalPalette.FromHueAndChroma(
13961396
DynamicScheme.GetRotatedHue(
13971397
sourceColorHct,
1398-
new[] {0.0, 38.0, 105.0, 140.0, 333.0, 360.0},
1399-
new[] {-14.0, 10.0, -14.0, 10.0, -14.0}
1398+
[0.0, 38.0, 105.0, 140.0, 333.0, 360.0],
1399+
[-14.0, 10.0, -14.0, 10.0, -14.0]
14001400
), platform == Platform.PHONE ? 56 : 36),
14011401
_ => base.GetSecondaryPalette(variant, sourceColorHct, isDark, platform, contrastLevel),
14021402
};
@@ -1409,26 +1409,26 @@ private static ContrastCurve GetContrastCurve(double @default)
14091409
Variant.NEUTRAL => TonalPalette.FromHueAndChroma(
14101410
DynamicScheme.GetRotatedHue(
14111411
sourceColorHct,
1412-
new[] {0.0, 38.0, 105.0, 161.0, 204.0, 278.0, 333.0, 360.0},
1413-
new[] {-32.0, 26.0, 10.0, -39.0, 24.0, -15.0, -32.0}
1412+
[0.0, 38.0, 105.0, 161.0, 204.0, 278.0, 333.0, 360.0],
1413+
[-32.0, 26.0, 10.0, -39.0, 24.0, -15.0, -32.0]
14141414
), platform == Platform.PHONE ? 20 : 36),
14151415
Variant.TONAL_SPOT => TonalPalette.FromHueAndChroma(
14161416
DynamicScheme.GetRotatedHue(
14171417
sourceColorHct,
1418-
new[] {0.0, 20.0, 71.0, 161.0, 333.0, 360.0},
1419-
new[] {-40.0, 48.0, -32.0, 40.0, -32.0}
1418+
[0.0, 20.0, 71.0, 161.0, 333.0, 360.0],
1419+
[-40.0, 48.0, -32.0, 40.0, -32.0]
14201420
), platform == Platform.PHONE ? 28 : 32),
14211421
Variant.EXPRESSIVE => TonalPalette.FromHueAndChroma(
14221422
DynamicScheme.GetRotatedHue(
14231423
sourceColorHct,
1424-
new[] {0.0, 105.0, 140.0, 204.0, 253.0, 278.0, 300.0, 333.0, 360.0},
1425-
new[] {-165.0, 160.0, -105.0, 101.0, -101.0, -160.0, -170.0, -165.0}
1424+
[0.0, 105.0, 140.0, 204.0, 253.0, 278.0, 300.0, 333.0, 360.0],
1425+
[-165.0, 160.0, -105.0, 101.0, -101.0, -160.0, -170.0, -165.0]
14261426
), 48),
14271427
Variant.VIBRANT => TonalPalette.FromHueAndChroma(
14281428
DynamicScheme.GetRotatedHue(
14291429
sourceColorHct,
1430-
new[] {0.0, 38.0, 71.0, 105.0, 140.0, 161.0, 253.0, 333.0, 360.0},
1431-
new[] {-72.0, 35.0, 24.0, -24.0, 62.0, 50.0, 62.0, -72.0}
1430+
[0.0, 38.0, 71.0, 105.0, 140.0, 161.0, 253.0, 333.0, 360.0],
1431+
[-72.0, 35.0, 24.0, -24.0, 62.0, 50.0, 62.0, -72.0]
14321432
), 56),
14331433
_ => base.GetTertiaryPalette(variant, sourceColorHct, isDark, platform, contrastLevel),
14341434
};
@@ -1473,8 +1473,8 @@ private static ContrastCurve GetContrastCurve(double @default)
14731473
{
14741474
var errorHue = DynamicScheme.GetPiecewiseValue(
14751475
sourceColorHct,
1476-
new[] {0.0, 3.0, 13.0, 23.0, 33.0, 43.0, 153.0, 273.0, 360.0},
1477-
new[] {12.0, 22.0, 32.0, 12.0, 22.0, 32.0, 22.0, 12.0});
1476+
[0.0, 3.0, 13.0, 23.0, 33.0, 43.0, 153.0, 273.0, 360.0],
1477+
[12.0, 22.0, 32.0, 12.0, 22.0, 32.0, 22.0, 12.0]);
14781478
return variant switch
14791479
{
14801480
Variant.NEUTRAL => TonalPalette.FromHueAndChroma(errorHue, platform == Platform.PHONE ? 50.0 : 40.0),
@@ -1489,8 +1489,8 @@ private static double GetExpressiveNeutralHue(Hct sourceColorHct)
14891489
{
14901490
return DynamicScheme.GetRotatedHue(
14911491
sourceColorHct,
1492-
new[] {0.0, 71.0, 124.0, 253.0, 278.0, 300.0, 360.0},
1493-
new[] {10.0, 0.0, 10.0, 0.0, 10.0, 0.0});
1492+
[0.0, 71.0, 124.0, 253.0, 278.0, 300.0, 360.0],
1493+
[10.0, 0.0, 10.0, 0.0, 10.0, 0.0]);
14941494
}
14951495

14961496
private static double GetExpressiveNeutralChroma(Hct sourceColorHct, bool isDark, Platform platform)
@@ -1503,8 +1503,8 @@ private static double GetVibrantNeutralHue(Hct sourceColorHct)
15031503
{
15041504
return DynamicScheme.GetRotatedHue(
15051505
sourceColorHct,
1506-
new[] {0.0, 38.0, 105.0, 140.0, 333.0, 360.0},
1507-
new[] {-14.0, 10.0, -14.0, 10.0, -14.0});
1506+
[0.0, 38.0, 105.0, 140.0, 333.0, 360.0],
1507+
[-14.0, 10.0, -14.0, 10.0, -14.0]);
15081508
}
15091509

15101510
private static double GetVibrantNeutralChroma(Hct sourceColorHct, Platform platform)

src/MaterialDesign3.MaterialColorUtilities/Hct/ViewingConditions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using static System.Math;
2+
#if NET462
3+
using static System.MathCompat;
4+
#endif
25

36
namespace MaterialColorUtilities;
47

@@ -128,4 +131,4 @@ private ViewingConditions(
128131
FlRoot = flRoot;
129132
Z = z;
130133
}
131-
}
134+
}

src/MaterialDesign3.MaterialColorUtilities/MaterialColorUtilities.csproj

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
<PropertyGroup>
33
<RootNamespace>MaterialColorUtilities</RootNamespace>
44
<AssemblyName>MaterialColorUtilities</AssemblyName>
5-
<TargetFrameworks>net462;net8.0</TargetFrameworks>
6-
<ImplicitUsings>enable</ImplicitUsings>
5+
<TargetFrameworks>net462;net8.0-windows</TargetFrameworks>
6+
<LangVersion>13</LangVersion>
7+
<ImplicitUsings>disable</ImplicitUsings>
8+
<RootNamespace>MaterialColorUtilities</RootNamespace>
79
<Nullable>enable</Nullable>
10+
<UseWpf>true</UseWpf>
811
<MDIXColorsVersion Condition="$(MDIXColorsVersion) == '' Or $(MDIXColorsVersion) == '*Undefined*'">1.0.1</MDIXColorsVersion>
912
<MDIXColorsVersion>$([System.Text.RegularExpressions.Regex]::Replace("$(MDIXColorsVersion)", "-ci\d+$", ""))</MDIXColorsVersion>
1013
<Description>Material Design Color Utilities</Description>
@@ -25,4 +28,18 @@
2528
</AssemblyAttribute>
2629
</ItemGroup>
2730

31+
<ItemGroup Label="Compatibility Libraries for .NET462" Condition="'$(TargetFramework)' == 'net462'">
32+
<PackageReference Include="System.Memory" />
33+
<PackageReference Include="System.ValueTuple" />
34+
<PackageReference Include="Polyfill">
35+
<PrivateAssets>all</PrivateAssets>
36+
</PackageReference>
37+
</ItemGroup>
38+
39+
<ItemGroup Label="Shims for .NET462" Condition="'$(TargetFramework)' == 'net462'">
40+
<Compile Include="..\Shims\MathCompat.cs">
41+
<Link>MathCompat.cs</Link>
42+
</Compile>
43+
</ItemGroup>
44+
2845
</Project>

src/MaterialDesign3.MaterialColorUtilities/Quantize/QuantizerCelebi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public static Dictionary<int, int> Quantize(int[] pixels, int maxColors)
1616
wuClusters[index++] = kvp.Key;
1717
}
1818

19-
return QuantizerWsmeans.Quantize(pixels, wuClusters, maxColors);
19+
return QuantizerWsMeans.Quantize(pixels, wuClusters, maxColors);
2020
}
2121
}

src/MaterialDesign3.MaterialColorUtilities/Quantize/QuantizerWsmeans.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace MaterialColorUtilities;
44

5-
public sealed class QuantizerWsmeans
5+
public static class QuantizerWsMeans
66
{
7-
private QuantizerWsmeans() { }
8-
97
private sealed class Distance : IComparable<Distance>
108
{
119
public int Index;
@@ -76,7 +74,7 @@ public static Dictionary<int, int> Quantize(int[] inputPixels, int[] startingClu
7674
double l = random.NextDouble() * 100.0;
7775
double a = random.NextDouble() * 200.0 - 100.0;
7876
double b = random.NextDouble() * 200.0 - 100.0;
79-
clusters[clustersCreated + i] = new double[] { l, a, b };
77+
clusters[clustersCreated + i] = [l, a, b];
8078
}
8179
}
8280

@@ -162,7 +160,7 @@ public static Dictionary<int, int> Quantize(int[] inputPixels, int[] startingClu
162160
var componentASums = new double[clusterCount];
163161
var componentBSums = new double[clusterCount];
164162
var componentCSums = new double[clusterCount];
165-
Array.Fill(pixelCountSums, 0);
163+
pixelCountSums.AsSpan().Fill(0);
166164
for (int i = 0; i < pointCount; i++)
167165
{
168166
int clusterIndex = clusterIndices[i];
@@ -179,7 +177,7 @@ public static Dictionary<int, int> Quantize(int[] inputPixels, int[] startingClu
179177
int count = pixelCountSums[i];
180178
if (count == 0)
181179
{
182-
clusters[i] = new double[] { 0.0, 0.0, 0.0 };
180+
clusters[i] = [0.0, 0.0, 0.0];
183181
continue;
184182
}
185183
double a = componentASums[i] / count;
@@ -203,4 +201,4 @@ public static Dictionary<int, int> Quantize(int[] inputPixels, int[] startingClu
203201

204202
return argbToPopulation;
205203
}
206-
}
204+
}

src/MaterialDesign3.MaterialColorUtilities/Score/Score.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static IEnumerable<int> ScoreColors(
3030
bool filter = true)
3131
{
3232
// Get the HCT color for each Argb value, while finding the per hue count and total count.
33-
List<Hct> colorsHct = new();
33+
List<Hct> colorsHct = [];
3434
var huePopulation = new int[360];
3535
var populationSum = 0.0;
3636
foreach (var entry in colorsToPopulation)
@@ -56,7 +56,7 @@ public static IEnumerable<int> ScoreColors(
5656

5757
// Scores each HCT color based on usage and chroma, while optionally filtering out
5858
// values that do not have enough chroma or usage.
59-
List<ScoredHct> scoredHcts = new();
59+
List<ScoredHct> scoredHcts = [];
6060
foreach (var hct in colorsHct)
6161
{
6262
var hue = MathUtils.SanitizeDegreesInt((int)Math.Round(hct.Hue));

src/MaterialDesign3.MaterialColorUtilities/Temperature/TemperatureCache.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ public List<Hct> GetAnalogousColors(int count, int divisions)
8686
var startHct = GetHctsByHue()[startHue];
8787
var lastTemp = GetRelativeTemperature(startHct);
8888

89-
List<Hct> allColors = new();
90-
allColors.Add(startHct);
89+
List<Hct> allColors =
90+
[
91+
startHct
92+
];
9193

9294
var absoluteTotalTempDelta = 0.0;
9395
for (var i = 0; i < 360; i++)
@@ -135,8 +137,10 @@ public List<Hct> GetAnalogousColors(int count, int divisions)
135137
}
136138
}
137139

138-
List<Hct> answers = new();
139-
answers.Add(input);
140+
List<Hct> answers =
141+
[
142+
input
143+
];
140144

141145
var ccwCount = (int)Floor(((double)count - 1.0) / 2.0);
142146
for (var i = 1; i < (ccwCount + 1); i++)
@@ -215,7 +219,7 @@ private List<Hct> GetHctsByHue()
215219
{
216220
return precomputedHctsByHue;
217221
}
218-
List<Hct> hcts = new();
222+
List<Hct> hcts = [];
219223
for (var hue = 0.0; hue <= 360.0; hue += 1.0)
220224
{
221225
var colorAtHue = Hct.From(hue, input.Chroma, input.Tone);
@@ -235,8 +239,11 @@ private List<Hct> GetHctsByTemp()
235239
return precomputedHctsByTemp;
236240
}
237241

238-
List<Hct> hcts = new(GetHctsByHue());
239-
hcts.Add(input);
242+
List<Hct> hcts =
243+
[
244+
..GetHctsByHue(),
245+
input
246+
];
240247
hcts.Sort((a, b) => GetTempsByHct()[a].CompareTo(GetTempsByHct()[b]));
241248
precomputedHctsByTemp = hcts;
242249
return precomputedHctsByTemp;
@@ -252,8 +259,11 @@ private Dictionary<Hct, double> GetTempsByHct()
252259
return precomputedTempsByHct;
253260
}
254261

255-
List<Hct> allHcts = new(GetHctsByHue());
256-
allHcts.Add(input);
262+
List<Hct> allHcts =
263+
[
264+
..GetHctsByHue(),
265+
input
266+
];
257267

258268
Dictionary<Hct, double> temperaturesByHct = new();
259269
foreach (var hct in allHcts)

src/Shims/MathCompat.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#if NET462
2+
namespace System
3+
{
4+
internal static class MathCompat
5+
{
6+
/// <summary>
7+
/// Returns the cube root of a specified number.
8+
/// </summary>
9+
/// <remarks>
10+
/// Correctly handles negatives (real cube root), NaN, infinities, and signed zero.
11+
/// </remarks>
12+
public static double Cbrt(double x)
13+
{
14+
if (double.IsNaN(x) || double.IsInfinity(x) || x == 0) return x;
15+
return x < 0 ? -Math.Pow(-x, 1.0 / 3.0) : Math.Pow(x, 1.0 / 3.0);
16+
}
17+
}
18+
}
19+
#endif

0 commit comments

Comments
 (0)