Skip to content

Commit 0ed8662

Browse files
committed
adding conversion methods for converting argb colors between System.Int32 and System.Windows.Media.Color representations
1 parent 65c1d80 commit 0ed8662

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

src/MaterialDesign3.MaterialColorUtilities/MaterialColorUtilities.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
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+
2732
<ItemGroup>
2833
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
2934
<_Parameter1>MDIXColorsVersion</_Parameter1>

src/MaterialDesign3.MaterialColorUtilities/Utils/ColorUtils.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ 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
30+
/// <summary>
31+
/// Converts a color in ARGB format to a <see cref="System.Windows.Media.Color"/>.
32+
/// </summary>
33+
public static System.Windows.Media.Color ColorFromArgb(int argb) =>
34+
System.Windows.Media.Color.FromArgb(
35+
(byte)AlphaFromArgb(argb),
36+
(byte)RedFromArgb(argb),
37+
(byte)GreenFromArgb(argb),
38+
(byte)BlueFromArgb(argb));
39+
40+
/// <summary>
41+
/// Converts a <see cref="System.Windows.Media.Color"/> to ARGB format.
42+
/// </summary>
43+
public static int ArgbFromColor(System.Windows.Media.Color color) =>
44+
(color.A << 24) | (color.R << 16) | (color.G << 8) | color.B;
45+
#endif
46+
2947
/// <summary>
3048
/// Converts a color from linear RGB components to ARGB format.
3149
/// </summary>

tests/MaterialColorUtilities.Tests/ColorUtilsTests.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace MaterialColorUtilities.Tests;
1+
#if WPF
2+
using System.Windows.Media;
3+
#endif
4+
5+
namespace MaterialColorUtilities.Tests;
26

37
public sealed class ColorUtilsTests
48
{
@@ -264,4 +268,43 @@ public async Task Linearize_Delinearize_RoundTrip()
264268
await Assert.That(converted).IsEqualTo(c);
265269
}
266270
}
271+
272+
#if WPF
273+
public static IEnumerable<Func<(int argb, Color color)>> TestColors()
274+
{
275+
yield return () => (unchecked((int)0xFFFF0000), Colors.Red);
276+
yield return () => (unchecked((int)0xFF00FF00), Colors.Lime);
277+
yield return () => (unchecked((int)0xFF0000FF), Colors.Blue);
278+
yield return () => (unchecked((int)0xFFFF00FF), Colors.Magenta);
279+
yield return () => (unchecked((int)0xFFFFFF00), Colors.Yellow);
280+
yield return () => (unchecked((int)0xFF00FFFF), Colors.Cyan);
281+
yield return () => (unchecked((int)0xFFFFFFFF), Colors.White);
282+
yield return () => (unchecked((int)0xFF000000), Colors.Black);
283+
yield return () => (unchecked((int)0x00FFFFFF), Colors.Transparent);
284+
}
285+
286+
[Test]
287+
[DisplayName("colorFromArgb returns known Colors")]
288+
[MethodDataSource(nameof(TestColors))]
289+
public async Task ColorFromArgb_KnownColors(int argb, Color color)
290+
{
291+
var converted = ColorUtils.ColorFromArgb(argb);
292+
293+
string result = $"{converted.A:x}{converted.R:x}{converted.G:x}{converted.B:X}";
294+
string expected = $"{color.A:x}{color.R:x}{color.G:x}{color.B:X}";
295+
296+
await Assert.That(result).IsEqualTo(expected);
297+
}
298+
299+
[Test]
300+
[DisplayName("argbFromColor returns known ints")]
301+
[MethodDataSource(nameof(TestColors))]
302+
public async Task ArgbFromColor_KnownColors(int argb, Color color)
303+
{
304+
string result = ColorUtils.ArgbFromColor(color).ToString("X");
305+
string expected = argb.ToString("X");
306+
307+
await Assert.That(result).IsEqualTo(expected);
308+
}
309+
#endif
267310
}

tests/MaterialColorUtilities.Tests/MaterialColorUtilities.Tests.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>net8.0-windows</TargetFrameworks>
4+
<TargetFramework>net8.0-windows</TargetFramework>
55
<AssemblyTitle>MaterialColorUtilities.Tests</AssemblyTitle>
66
<Product>MaterialColorUtilities.Tests</Product>
77
<OutputType>Exe</OutputType>
@@ -13,6 +13,12 @@
1313
<EnableSourceLink>false</EnableSourceLink>
1414
<SuppressImplicitGitSourceLink>true</SuppressImplicitGitSourceLink>
1515
</PropertyGroup>
16+
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+
1622
<ItemGroup>
1723
<ProjectReference Include="..\..\src\MaterialDesign3.MaterialColorUtilities\MaterialColorUtilities.csproj" />
1824
</ItemGroup>

0 commit comments

Comments
 (0)