Skip to content

Commit 270ebf9

Browse files
author
Johann Dirry
committed
Implementing unit tests for MaterialColorUtilities
1 parent c160605 commit 270ebf9

37 files changed

+2609
-314
lines changed

MaterialDesignToolkit.Full.sln

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaterialDesignDemo.Shared",
6565
EndProject
6666
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaterialColorUtilities", "src\MaterialDesign3.MaterialColorUtilities\MaterialColorUtilities.csproj", "{2C29B80E-1689-43CE-85AC-71799666B4AC}"
6767
EndProject
68+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaterialColorUtilities.Tests", "tests\MaterialColorUtilities.Tests\MaterialColorUtilities.Tests.csproj", "{91485BEA-759F-406E-87B7-68D94CF66DE4}"
69+
EndProject
6870
Global
6971
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7072
Debug|Any CPU = Debug|Any CPU
@@ -269,6 +271,22 @@ Global
269271
{2C29B80E-1689-43CE-85AC-71799666B4AC}.Release|x64.Build.0 = Release|Any CPU
270272
{2C29B80E-1689-43CE-85AC-71799666B4AC}.Release|x86.ActiveCfg = Release|Any CPU
271273
{2C29B80E-1689-43CE-85AC-71799666B4AC}.Release|x86.Build.0 = Release|Any CPU
274+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
275+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
276+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Debug|ARM.ActiveCfg = Debug|Any CPU
277+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Debug|ARM.Build.0 = Debug|Any CPU
278+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Debug|x64.ActiveCfg = Debug|Any CPU
279+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Debug|x64.Build.0 = Debug|Any CPU
280+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Debug|x86.ActiveCfg = Debug|Any CPU
281+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Debug|x86.Build.0 = Debug|Any CPU
282+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
283+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Release|Any CPU.Build.0 = Release|Any CPU
284+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Release|ARM.ActiveCfg = Release|Any CPU
285+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Release|ARM.Build.0 = Release|Any CPU
286+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Release|x64.ActiveCfg = Release|Any CPU
287+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Release|x64.Build.0 = Release|Any CPU
288+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Release|x86.ActiveCfg = Release|Any CPU
289+
{91485BEA-759F-406E-87B7-68D94CF66DE4}.Release|x86.Build.0 = Release|Any CPU
272290
EndGlobalSection
273291
GlobalSection(SolutionProperties) = preSolution
274292
HideSolutionNode = FALSE
@@ -281,6 +299,7 @@ Global
281299
{98627CBE-F009-482E-97E9-C69C7135E91F} = {D34BE232-DE51-43C1-ABDC-B69003BB50FF}
282300
{B39795A7-D66A-4F2F-9F41-050838D14048} = {D34BE232-DE51-43C1-ABDC-B69003BB50FF}
283301
{2C29B80E-1689-43CE-85AC-71799666B4AC} = {9E303A4A-3712-44B9-91EE-830FDC087795}
302+
{91485BEA-759F-406E-87B7-68D94CF66DE4} = {9E303A4A-3712-44B9-91EE-830FDC087795}
284303
EndGlobalSection
285304
GlobalSection(ExtensibilityGlobals) = postSolution
286305
SolutionGuid = {730B2F9E-74AE-46CE-9E61-89AA5C6D5DD3}

src/MaterialDesign3.MaterialColorUtilities/Contrast/Contrast.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@ public static double RatioOfYs(double y1, double y2)
9494
/// color's lightness to in order to reach their desired contrast, instead of guessing & checking
9595
/// with hex codes.
9696
/// </summary>
97-
public static double RatioOfTones(double t1, double t2)
97+
public static double RatioOfTones(double toneA, double toneB)
9898
{
99-
return RatioOfYs(ColorUtils.YFromLstar(t1), ColorUtils.YFromLstar(t2));
99+
return RatioOfYs(
100+
ColorUtils.YFromLstar(MathUtils.Clamp(0.0, 100.0, toneA)),
101+
ColorUtils.YFromLstar(MathUtils.Clamp(0.0, 100.0, toneB)));
100102
}
101103

102104
/// <summary>
@@ -191,4 +193,4 @@ public static double DarkerUnsafe(double tone, double ratio)
191193
var darkerSafe = Darker(tone, ratio);
192194
return Max(0.0, darkerSafe);
193195
}
194-
}
196+
}

src/MaterialDesign3.MaterialColorUtilities/DynamicColor/ColorSpec2021.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public class ColorSpec2021 : ColorSpec
192192
.SetIsBackground(true)
193193
.SetBackground(HighestSurface)
194194
.SetContrastCurve(_ => new ContrastCurve(3.0, 4.5, 7.0, 7.0))
195-
.SetToneDeltaPair(_ => new ToneDeltaPair(PrimaryContainer, Primary, 10.0, TonePolarity.NEARER, false))
195+
.SetToneDeltaPair(_ => new ToneDeltaPair(PrimaryContainer, Primary, 10.0, TonePolarity.Nearer, false))
196196
.Build();
197197

198198
public DynamicColor? PrimaryDim => null;
@@ -217,7 +217,7 @@ public class ColorSpec2021 : ColorSpec
217217
.SetIsBackground(true)
218218
.SetBackground(HighestSurface)
219219
.SetContrastCurve(_ => new ContrastCurve(1.0, 1.0, 3.0, 4.5))
220-
.SetToneDeltaPair(_ => new ToneDeltaPair(PrimaryContainer, Primary, 10.0, TonePolarity.NEARER, false))
220+
.SetToneDeltaPair(_ => new ToneDeltaPair(PrimaryContainer, Primary, 10.0, TonePolarity.Nearer, false))
221221
.Build();
222222

223223
public DynamicColor OnPrimaryContainer => new DynamicColor.Builder()
@@ -249,7 +249,7 @@ public class ColorSpec2021 : ColorSpec
249249
.SetIsBackground(true)
250250
.SetBackground(HighestSurface)
251251
.SetContrastCurve(_ => new ContrastCurve(3.0, 4.5, 7.0, 7.0))
252-
.SetToneDeltaPair(_ => new ToneDeltaPair(SecondaryContainer, Secondary, 10.0, TonePolarity.NEARER, false))
252+
.SetToneDeltaPair(_ => new ToneDeltaPair(SecondaryContainer, Secondary, 10.0, TonePolarity.Nearer, false))
253253
.Build();
254254

255255
public DynamicColor? SecondaryDim => null;
@@ -275,7 +275,7 @@ public class ColorSpec2021 : ColorSpec
275275
.SetIsBackground(true)
276276
.SetBackground(HighestSurface)
277277
.SetContrastCurve(_ => new ContrastCurve(1.0, 1.0, 3.0, 4.5))
278-
.SetToneDeltaPair(_ => new ToneDeltaPair(SecondaryContainer, Secondary, 10.0, TonePolarity.NEARER, false))
278+
.SetToneDeltaPair(_ => new ToneDeltaPair(SecondaryContainer, Secondary, 10.0, TonePolarity.Nearer, false))
279279
.Build();
280280

281281
public DynamicColor OnSecondaryContainer => new DynamicColor.Builder()
@@ -299,7 +299,7 @@ public class ColorSpec2021 : ColorSpec
299299
.SetIsBackground(true)
300300
.SetBackground(HighestSurface)
301301
.SetContrastCurve(_ => new ContrastCurve(3.0, 4.5, 7.0, 7.0))
302-
.SetToneDeltaPair(_ => new ToneDeltaPair(TertiaryContainer, Tertiary, 10.0, TonePolarity.NEARER, false))
302+
.SetToneDeltaPair(_ => new ToneDeltaPair(TertiaryContainer, Tertiary, 10.0, TonePolarity.Nearer, false))
303303
.Build();
304304

305305
public DynamicColor? TertiaryDim => null;
@@ -325,7 +325,7 @@ public class ColorSpec2021 : ColorSpec
325325
.SetIsBackground(true)
326326
.SetBackground(HighestSurface)
327327
.SetContrastCurve(_ => new ContrastCurve(1.0, 1.0, 3.0, 4.5))
328-
.SetToneDeltaPair(_ => new ToneDeltaPair(TertiaryContainer, Tertiary, 10.0, TonePolarity.NEARER, false))
328+
.SetToneDeltaPair(_ => new ToneDeltaPair(TertiaryContainer, Tertiary, 10.0, TonePolarity.Nearer, false))
329329
.Build();
330330

331331
public DynamicColor OnTertiaryContainer => new DynamicColor.Builder()
@@ -349,7 +349,7 @@ public class ColorSpec2021 : ColorSpec
349349
.SetIsBackground(true)
350350
.SetBackground(HighestSurface)
351351
.SetContrastCurve(_ => new ContrastCurve(3.0, 4.5, 7.0, 7.0))
352-
.SetToneDeltaPair(_ => new ToneDeltaPair(ErrorContainer, Error, 10.0, TonePolarity.NEARER, false))
352+
.SetToneDeltaPair(_ => new ToneDeltaPair(ErrorContainer, Error, 10.0, TonePolarity.Nearer, false))
353353
.Build();
354354

355355
public DynamicColor? ErrorDim => null;
@@ -369,7 +369,7 @@ public class ColorSpec2021 : ColorSpec
369369
.SetIsBackground(true)
370370
.SetBackground(HighestSurface)
371371
.SetContrastCurve(_ => new ContrastCurve(1.0, 1.0, 3.0, 4.5))
372-
.SetToneDeltaPair(_ => new ToneDeltaPair(ErrorContainer, Error, 10.0, TonePolarity.NEARER, false))
372+
.SetToneDeltaPair(_ => new ToneDeltaPair(ErrorContainer, Error, 10.0, TonePolarity.Nearer, false))
373373
.Build();
374374

375375
public DynamicColor OnErrorContainer => new DynamicColor.Builder()
@@ -388,7 +388,7 @@ public class ColorSpec2021 : ColorSpec
388388
.SetIsBackground(true)
389389
.SetBackground(HighestSurface)
390390
.SetContrastCurve(_ => new ContrastCurve(1.0, 1.0, 3.0, 4.5))
391-
.SetToneDeltaPair(_ => new ToneDeltaPair(PrimaryFixed, PrimaryFixedDim, 10.0, TonePolarity.LIGHTER, true))
391+
.SetToneDeltaPair(_ => new ToneDeltaPair(PrimaryFixed, PrimaryFixedDim, 10.0, TonePolarity.Lighter, true))
392392
.Build();
393393

394394
public DynamicColor PrimaryFixedDim => new DynamicColor.Builder()
@@ -398,7 +398,7 @@ public class ColorSpec2021 : ColorSpec
398398
.SetIsBackground(true)
399399
.SetBackground(HighestSurface)
400400
.SetContrastCurve(_ => new ContrastCurve(1.0, 1.0, 3.0, 4.5))
401-
.SetToneDeltaPair(_ => new ToneDeltaPair(PrimaryFixed, PrimaryFixedDim, 10.0, TonePolarity.LIGHTER, true))
401+
.SetToneDeltaPair(_ => new ToneDeltaPair(PrimaryFixed, PrimaryFixedDim, 10.0, TonePolarity.Lighter, true))
402402
.Build();
403403

404404
public DynamicColor OnPrimaryFixed => new DynamicColor.Builder()
@@ -427,7 +427,7 @@ public class ColorSpec2021 : ColorSpec
427427
.SetIsBackground(true)
428428
.SetBackground(HighestSurface)
429429
.SetContrastCurve(_ => new ContrastCurve(1.0, 1.0, 3.0, 4.5))
430-
.SetToneDeltaPair(_ => new ToneDeltaPair(SecondaryFixed, SecondaryFixedDim, 10.0, TonePolarity.LIGHTER, true))
430+
.SetToneDeltaPair(_ => new ToneDeltaPair(SecondaryFixed, SecondaryFixedDim, 10.0, TonePolarity.Lighter, true))
431431
.Build();
432432

433433
public DynamicColor SecondaryFixedDim => new DynamicColor.Builder()
@@ -437,7 +437,7 @@ public class ColorSpec2021 : ColorSpec
437437
.SetIsBackground(true)
438438
.SetBackground(HighestSurface)
439439
.SetContrastCurve(_ => new ContrastCurve(1.0, 1.0, 3.0, 4.5))
440-
.SetToneDeltaPair(_ => new ToneDeltaPair(SecondaryFixed, SecondaryFixedDim, 10.0, TonePolarity.LIGHTER, true))
440+
.SetToneDeltaPair(_ => new ToneDeltaPair(SecondaryFixed, SecondaryFixedDim, 10.0, TonePolarity.Lighter, true))
441441
.Build();
442442

443443
public DynamicColor OnSecondaryFixed => new DynamicColor.Builder()
@@ -466,7 +466,7 @@ public class ColorSpec2021 : ColorSpec
466466
.SetIsBackground(true)
467467
.SetBackground(HighestSurface)
468468
.SetContrastCurve(_ => new ContrastCurve(1.0, 1.0, 3.0, 4.5))
469-
.SetToneDeltaPair(_ => new ToneDeltaPair(TertiaryFixed, TertiaryFixedDim, 10.0, TonePolarity.LIGHTER, true))
469+
.SetToneDeltaPair(_ => new ToneDeltaPair(TertiaryFixed, TertiaryFixedDim, 10.0, TonePolarity.Lighter, true))
470470
.Build();
471471

472472
public DynamicColor TertiaryFixedDim => new DynamicColor.Builder()
@@ -476,7 +476,7 @@ public class ColorSpec2021 : ColorSpec
476476
.SetIsBackground(true)
477477
.SetBackground(HighestSurface)
478478
.SetContrastCurve(_ => new ContrastCurve(1.0, 1.0, 3.0, 4.5))
479-
.SetToneDeltaPair(_ => new ToneDeltaPair(TertiaryFixed, TertiaryFixedDim, 10.0, TonePolarity.LIGHTER, true))
479+
.SetToneDeltaPair(_ => new ToneDeltaPair(TertiaryFixed, TertiaryFixedDim, 10.0, TonePolarity.Lighter, true))
480480
.Build();
481481

482482
public DynamicColor OnTertiaryFixed => new DynamicColor.Builder()
@@ -605,9 +605,9 @@ public double GetTone(DynamicScheme scheme, DynamicColor color)
605605
var polarity = toneDeltaPair.Polarity;
606606
var stayTogether = toneDeltaPair.StayTogether;
607607

608-
var aIsNearer = (polarity == TonePolarity.NEARER
609-
|| (polarity == TonePolarity.LIGHTER && !scheme.IsDark)
610-
|| (polarity == TonePolarity.DARKER && !scheme.IsDark));
608+
var aIsNearer = (polarity == TonePolarity.Nearer
609+
|| (polarity == TonePolarity.Lighter && !scheme.IsDark)
610+
|| (polarity == TonePolarity.Darker && !scheme.IsDark));
611611
var nearer = aIsNearer ? roleA : roleB;
612612
var farther = aIsNearer ? roleB : roleA;
613613
var amNearer = color.name.Equals(nearer.name, System.StringComparison.Ordinal);
@@ -848,8 +848,5 @@ public TonalPalette GetNeutralVariantPalette(Variant variant, Hct sourceColorHct
848848
};
849849
}
850850

851-
public TonalPalette? GetErrorPalette(Variant variant, Hct sourceColorHct, bool isDark, Platform platform, double contrastLevel)
852-
{
853-
return null;
854-
}
851+
public TonalPalette? GetErrorPalette(Variant variant, Hct sourceColorHct, bool isDark, Platform platform, double contrastLevel) => null;
855852
}

0 commit comments

Comments
 (0)