Skip to content

Commit 9e074c6

Browse files
author
Johann Dirry
committed
adding a factory method for creating dynamic color schemes
1 parent 18c453d commit 9e074c6

File tree

7 files changed

+71
-20
lines changed

7 files changed

+71
-20
lines changed

src/MaterialDesign3.MaterialColorUtilities/DynamicColor/DynamicColor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,8 @@ public int GetArgb(DynamicScheme scheme)
140140
return (argb & 0x00ffffff) | (alpha << 24);
141141
}
142142

143-
#if WPF
144143
public System.Windows.Media.Color GetColor(DynamicScheme scheme)
145144
=> ColorUtils.ColorFromArgb(GetArgb(scheme));
146-
#endif
147145

148146
public Hct GetHct(DynamicScheme scheme)
149147
{

src/MaterialDesign3.MaterialColorUtilities/MaterialColorUtilities.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
<NoWarn>CS0618</NoWarn>
2525
</PropertyGroup>
2626

27-
<!-- WPF may not be available when we want to target non-windows platforms in future -->
28-
<PropertyGroup Condition="'$(TargetFramework)' == 'net462' OR '$(TargetFramework)' == 'net8.0-windows'">
29-
<DefineConstants>$(DefineConstants);WPF</DefineConstants>
30-
</PropertyGroup>
31-
3227
<ItemGroup>
3328
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
3429
<_Parameter1>MDIXColorsVersion</_Parameter1>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Windows.Media;
2+
3+
namespace MaterialColorUtilities;
4+
5+
/// <summary>
6+
/// Factory for creating a dynamic color scheme.
7+
/// </summary>
8+
public static class DynamicSchemeFactory
9+
{
10+
/// <summary>
11+
/// Factory method for creating a dynamic color scheme.
12+
/// </summary>
13+
/// <remarks>
14+
/// The colors are optional. If any of them are null,
15+
/// the color will be automatically generated based on the source color.
16+
/// </remarks>
17+
public static DynamicScheme Create(
18+
Color sourceColor,
19+
Variant variant,
20+
bool isDark,
21+
double contrastLevel,
22+
Platform platform,
23+
SpecVersion specVersion,
24+
Color? primary,
25+
Color? secondary,
26+
Color? tertiary,
27+
Color? neutral,
28+
Color? neutralVariant,
29+
Color? error)
30+
{
31+
var sourceColorHct = Hct.FromInt(ColorUtils.ArgbFromColor(sourceColor));
32+
33+
TonalPalette primaryPalette = primary == null
34+
? ColorSpecs.Get(specVersion).GetPrimaryPalette(variant, sourceColorHct, isDark, platform, contrastLevel)
35+
: ColorSpecs.Get(specVersion).GetPrimaryPalette(variant, Hct.FromInt(ColorUtils.ArgbFromColor(primary.Value)), isDark, platform, contrastLevel);
36+
37+
TonalPalette secondaryPalette = secondary == null
38+
? ColorSpecs.Get(specVersion).GetSecondaryPalette(variant, sourceColorHct, isDark, platform, contrastLevel)
39+
: ColorSpecs.Get(specVersion).GetSecondaryPalette(variant, Hct.FromInt(ColorUtils.ArgbFromColor(secondary.Value)), isDark, platform, contrastLevel);
40+
41+
TonalPalette tertiaryPalette = tertiary == null
42+
? ColorSpecs.Get(specVersion).GetTertiaryPalette(variant, sourceColorHct, isDark, platform, contrastLevel)
43+
: ColorSpecs.Get(specVersion).GetTertiaryPalette(variant, Hct.FromInt(ColorUtils.ArgbFromColor(tertiary.Value)), isDark, platform, contrastLevel);
44+
45+
TonalPalette neutralPalette = neutral == null
46+
? ColorSpecs.Get(specVersion).GetNeutralPalette(variant, sourceColorHct, isDark, platform, contrastLevel)
47+
: ColorSpecs.Get(specVersion).GetNeutralPalette(variant, Hct.FromInt(ColorUtils.ArgbFromColor(neutral.Value)), isDark, platform, contrastLevel);
48+
49+
TonalPalette neutralVariantPalette = neutralVariant == null
50+
? ColorSpecs.Get(specVersion).GetNeutralVariantPalette(variant, sourceColorHct, isDark, platform, contrastLevel)
51+
: ColorSpecs.Get(specVersion).GetNeutralVariantPalette(variant, Hct.FromInt(ColorUtils.ArgbFromColor(neutralVariant.Value)), isDark, platform, contrastLevel);
52+
53+
TonalPalette? errorPalette = error == null
54+
? ColorSpecs.Get(specVersion).GetErrorPalette(variant, sourceColorHct, isDark, platform, contrastLevel)
55+
: ColorSpecs.Get(specVersion).GetErrorPalette(variant, Hct.FromInt(ColorUtils.ArgbFromColor(error.Value)), isDark, platform, contrastLevel);
56+
57+
return new DynamicScheme(
58+
sourceColorHct,
59+
variant,
60+
isDark,
61+
contrastLevel,
62+
platform,
63+
specVersion,
64+
primaryPalette,
65+
secondaryPalette,
66+
tertiaryPalette,
67+
neutralPalette,
68+
neutralVariantPalette,
69+
errorPalette);
70+
}
71+
}

src/MaterialDesign3.MaterialColorUtilities/Utils/ColorUtils.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public static class ColorUtils
2626
/// </summary>
2727
public static int ArgbFromRgb(int red, int green, int blue) => (255 << 24) | ((red & 255) << 16) | ((green & 255) << 8) | (blue & 255);
2828

29-
#if WPF
3029
/// <summary>
3130
/// Converts a color in ARGB format to a <see cref="System.Windows.Media.Color"/>.
3231
/// </summary>
@@ -42,7 +41,6 @@ public static System.Windows.Media.Color ColorFromArgb(int argb) =>
4241
/// </summary>
4342
public static int ArgbFromColor(System.Windows.Media.Color color) =>
4443
(color.A << 24) | (color.R << 16) | (color.G << 8) | color.B;
45-
#endif
4644

4745
/// <summary>
4846
/// Converts a color from linear RGB components to ARGB format.

tests/MaterialColorUtilities.Tests/ColorUtilsTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if WPF
21
using System.Windows.Media;
3-
#endif
42

53
namespace MaterialColorUtilities.Tests;
64

@@ -269,7 +267,6 @@ public async Task Linearize_Delinearize_RoundTrip()
269267
}
270268
}
271269

272-
#if WPF
273270
public static IEnumerable<Func<(int argb, Color color)>> TestColors()
274271
{
275272
yield return () => (unchecked((int)0xFFFF0000), Colors.Red);
@@ -306,5 +303,4 @@ public async Task ArgbFromColor_KnownColors(int argb, Color color)
306303

307304
await Assert.That(result).IsEqualTo(expected);
308305
}
309-
#endif
310306
}

tests/MaterialColorUtilities.Tests/DynamicSchemeTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public async Task RotationGreaterThan360_Wraps()
6666
await Assert.That(hue).IsEqualTo(163.0).Within(1.0);
6767
}
6868

69-
#if WPF
7069
/// <summary>
7170
/// Shows how te crate a theme from a primary color.
7271
/// </summary>
@@ -169,5 +168,4 @@ public async Task CreateThemeFromColor()
169168
() => mdc.TextHintInverse.GetColor(scheme).ShouldBe(Color.FromArgb(0xFF, 0x19, 0x1C, 0x17)));
170169
await Task.CompletedTask;
171170
}
172-
#endif
173171
}

tests/MaterialColorUtilities.Tests/MaterialColorUtilities.Tests.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
<SuppressImplicitGitSourceLink>true</SuppressImplicitGitSourceLink>
1515
</PropertyGroup>
1616

17-
<!-- WPF may not be available when we want to target non-windows platforms in future -->
18-
<PropertyGroup Condition="'$(TargetFramework)' == 'net462' OR '$(TargetFramework)' == 'net8.0-windows'">
19-
<DefineConstants>$(DefineConstants);WPF</DefineConstants>
20-
</PropertyGroup>
21-
2217
<ItemGroup>
2318
<ProjectReference Include="..\..\src\MaterialDesign3.MaterialColorUtilities\MaterialColorUtilities.csproj" />
2419
</ItemGroup>

0 commit comments

Comments
 (0)