MaterialColorUtilities 0.1.0
See the version list below for details.
dotnet add package MaterialColorUtilities --version 0.1.0
NuGet\Install-Package MaterialColorUtilities -Version 0.1.0
<PackageReference Include="MaterialColorUtilities" Version="0.1.0" />
paket add MaterialColorUtilities --version 0.1.0
#r "nuget: MaterialColorUtilities, 0.1.0"
// Install MaterialColorUtilities as a Cake Addin #addin nuget:?package=MaterialColorUtilities&version=0.1.0 // Install MaterialColorUtilities as a Cake Tool #tool nuget:?package=MaterialColorUtilities&version=0.1.0
Material Color Utilities for .NET
This is a C# implementation of Google's Material Color Utilities, that can be used to extract a color from an image and then generate a Material Design 3 color scheme. Also includes helpers for working with schemes.
Install
Quickstart
Generate a seed color from an image (e.g. the user's wallpaper):
// Load the image into an int[].
// The image is stored in an embedded resource, and then decoded and resized using SkiaSharp.
string imageResourceId = "MaterialColorUtilities.Console.Resources.wallpaper.webp";
using Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(imageResourceId);
SKBitmap bitmap = SKBitmap.Decode(resourceStream).Resize(new SKImageInfo(112, 112), SKFilterQuality.Medium);
int[] pixels = bitmap.Pixels.Select(p => (int)(uint)p).ToArray();
// This is where the magic happens
int seedColor = ImageUtils.ColorsFromImage(pixels).First();
Use that color to create a CorePalette
that can be used to create any tone (0-100) of any key color:
CorePalette corePalette = new(seedColor);
int color = corePalette.Secondary[69];
A scheme contains the Material Design 3 named colors, like OnPrimary, Background, or TertiaryContainer. Turn the CorePalette
into a Scheme<int>
using a mapper:
Scheme<int> lightScheme = new LightSchemeMapper().Map(corePalette);
Scheme<int> darkScheme = new DarkSchemeMapper().Map(corePalette);
Then convert your scheme to one with a different color type:
Scheme<Color> lightSchemeColor = lightScheme.ConvertTo(Color.FromArgb);
Scheme<string> lightSchemeString = lightScheme.ConvertTo(x => "#" + x.ToString("X")[2..]);
Extension
Adding your own colors
- Define a new key color if you need by subclassing
CorePalette
:
public class MyCorePalette : CorePalette
{
public TonalPalette Orange { get; set; }
public MyCorePalette(int seed) : base(seed)
{
// You can harmonize a color to make it closer to the seed color
int harmonizedOrange = Blender.Harmonize(unchecked(0xFFA500), seed);
Orange = TonalPalette.FromInt(harmonizedOrange);
}
}
- Subclass
Scheme<TColor>
🤖 A source generator will add new converter methods automatically.
Make sure to mark the class
partial
and don't nest it inside another class.If you get warning
CS8032: An instance of analyzer MaterialColorUtilities.Schemes.SchemeConverterGenerator cannot be created from...
your IDE/compiler doesn't have Rosyln 4.0, so the source generator won't work. Make sure you are using Visual Studio 2022+ (as it has MSBuild 17) or .NET SDK 6+.
public partial class MyScheme<TColor> : Scheme<TColor>
{
public TColor Orange { get; set; }
public TColor OnOrange { get; set; }
public TColor OrangeContainer { get; set; }
public TColor OnOrangeContainer { get; set; }
}
- Create mappers
public class MyLightSchemeMapper : LightSchemeMapper<MyCorePalette, MyScheme<int>>
{
protected override void MapCore(MyCorePalette palette, MyScheme<int> scheme)
{
base.MapCore(palette, scheme);
scheme.Orange = palette.Orange[40];
scheme.OnOrange = palette.Orange[100];
scheme.OrangeContainer = palette.Orange[90];
scheme.OnOrangeContainer = palette.Orange[10];
// You can also override already mapped colors
scheme.Surface = palette.Neutral[100];
}
}
public class MyDarkSchemeMapper : DarkSchemeMapper<MyCorePalette, MyScheme<int>>
{
protected override void MapCore(MyCorePalette palette, MyScheme<int> scheme)
{
base.MapCore(palette, scheme);
scheme.Orange = palette.Orange[80];
scheme.OnOrange = palette.Orange[20];
scheme.OrangeContainer = palette.Orange[30];
scheme.OnOrangeContainer = palette.Orange[90];
}
}
- Profit!
MyCorePalette myCorePalette = new(seedColor);
MyScheme<string> myDarkScheme = new MyDarkSchemeMapper()
.Map(myCorePalette)
.ConvertTo(StringUtils.HexFromArgb);
For more info check out the source code and the example projects.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages (4)
Showing the top 4 NuGet packages that depend on MaterialColorUtilities:
Package | Downloads |
---|---|
Material.Components.Maui
Material design Components for .NET MAUI |
|
MaterialColorUtilities.Maui
Material You dynamic theming for .NET MAUI |
|
RichMvvm.MAUI
Package Description |
|
Lamparter.Material
The perfect libary for using Material 3 in MAUI 🎨🖌️ |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on MaterialColorUtilities:
Repository | Stars |
---|---|
mdc-maui/mdc-maui
Material design components for .NET MAUI
|