### Configure Services in MauiProgram.cs Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md Add the UseMaterialColors extension method to your MauiAppBuilder to register necessary services for theming. This is the initial setup step. ```csharp +using MaterialColorUtilities.Maui; namespace YourApp; public static class MauiProgram { public static MauiApp CreateMauiApp() { MauiAppBuilder builder = MauiApp .CreateBuilder () + .UseMaterialColors () .UseMauiApp (); return builder.Build (); } } ``` -------------------------------- ### Initialize MaterialColorService in App.xaml.cs Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md For applications not using XAML for resource dictionaries, initialize the MaterialColorService directly in your App.xaml.cs file. This ensures the theming services are available when the application starts. ```csharp +using MaterialColorUtilities.Maui; public class App : Application { public App () { ``` -------------------------------- ### Dark Scheme Mapper for Custom Colors Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Implement DarkSchemeMapper to map custom core palette colors to the roles in a dark scheme. This example shows mapping the Orange palette to dark theme color roles. ```csharp public class MyDarkSchemeMapper : DarkSchemeMapper> { protected override void MapCore(MyCorePalette palette, MyScheme 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]; } } ``` -------------------------------- ### Fill and Use Core Palette with Style Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Shows how to initialize an empty CorePalette and then fill it with a seed color and a specific style, accessing a tone from the primary palette. ```csharp CorePalette corePalette = new(); corePalette.Fill(0x123456, Style.Expressive); uint primary60 = corePalette.Primary[60]; ``` -------------------------------- ### Initialize MaterialColorService in MAUI Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md Call this method during application initialization to set up the MaterialColorService. Ensure 'this.Resources' is accessible. ```csharp IMaterialColorService.Current.Initialize(this.Resources); ``` -------------------------------- ### Map Core Palette to Light Scheme Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Illustrates converting a CorePalette into a Scheme object using a LightSchemeMapper. ```csharp Scheme scheme = new LightSchemeMapper().Map(corePalette); ``` -------------------------------- ### Configure Services with Options Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md Customize the MaterialColorUtilities.Maui service configuration by providing options such as a fallback seed color or disabling dynamic color. This allows for more control over the theming behavior. ```csharp .UseMaterialColors (options => { options.FallbackSeed = 0xB000B5; options.UseDynamicColor = false; }) ``` -------------------------------- ### Register Custom MaterialColorService in MAUI Program Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md Register your custom MaterialColorService at startup using the UseMaterialColors extension method. ```diff public static class MauiProgram { public static MauiApp CreateMauiApp() { MauiAppBuilder builder = MauiApp .CreateBuilder() + .UseMaterialColors() .UseMauiApp(); return builder.Build(); } } ``` -------------------------------- ### Convert HCT to RGB Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Shows how to create an HCT color from hue, chroma, and tone, and then convert it back to an ARGB integer. ```csharp Hct hct = Hct.From(hue, chroma, tone); uint argbColor = Hct.ToInt(); // 0xFF0000FF ``` -------------------------------- ### Create Core Palette from Seed Color Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Demonstrates generating a CorePalette using a seed color, accessing its primary tonal palette, and retrieving a specific tone. ```csharp CorePalette corePalette = CorePalette.Of(0x123456); uint primary60 = corePalette.Primary[60]; ``` -------------------------------- ### Convert RGB to HCT and Update Tone Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Demonstrates converting an ARGB color to HCT, accessing its properties, and updating the tone, which recalculates chroma. ```csharp uint argbColor = 0x0000FF; // Blue Hct hct = Hct.FromInt(argbColor); Console.WriteLine(hct.Hue); // 283 Console.WriteLine(hct.Chroma); // 87 Console.WriteLine(hct.Tone); // 32 // All of the properties can be updated any time. This causes chroma to be recalculated. hct.Tone = 90; Console.WriteLine(hct.Hue); // 282 Console.WriteLine(hct.Chroma); // 19 Console.WriteLine(hct.Tone); // 90 ``` -------------------------------- ### Usage of Custom Color Schemes Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Instantiate a custom core palette, fill it with a seed color, map it to a custom scheme using the appropriate mapper, and convert the scheme to a desired format (e.g., hex string). ```csharp MyCorePalette myCorePalette = new(); myCorePalette.Fill(seedColor); MyScheme myDarkScheme = new MyDarkSchemeMapper().Map(myCorePalette); MyScheme myDarkScheme = myDarkScheme.Convert(StringUtils.HexFromArgb); ``` -------------------------------- ### Extract Seed Colors from Image Pixels Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Shows how to use ImageUtils to extract the best seed colors for theming from an array of ARGB pixel colors. ```csharp uint[] pixels = ...; List bestColors = ImageUtils.ColorsFromImage(pixels); uint seed = bestColors[0]; ``` -------------------------------- ### Access Tones from Tonal Palette Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Illustrates creating a TonalPalette from a seed color and accessing specific tones using integer or double indices. ```csharp TonalPalette blue = TonalPalette.FromInt(0x0000FF); uint tone69 = blue[69]; uint tone49_6 = blue[49.6]; ``` -------------------------------- ### Define Custom MaterialColorService with Custom Palettes/Schemes Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md Extend MaterialColorService and specify custom palettes and schemes as generic type arguments. ```csharp public class MyMaterialColorService : MaterialColorService, MyScheme, MyLightSchemeMapper, MyDarkSchemeMapper> { public MyMaterialColorService(IOptions options, IDynamicColorService dynamicColorService, IPreferences preferences) : base(options, dynamicColorService, preferences) { } } ``` -------------------------------- ### Convert Scheme Color Type Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Demonstrates converting a Scheme of uint colors to a Scheme of string hex codes or a Scheme of Color objects using the Convert method. ```csharp Scheme schemeString = scheme.Convert(x => "#" + x.ToString("x6")); Scheme schemeColor = scheme.Convert(Color.FromUint); ``` -------------------------------- ### Light Scheme Mapper for Custom Colors Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Implement LightSchemeMapper to map custom core palette colors (like Orange) to the corresponding roles in a light scheme. This includes defining primary, on, and container colors. ```csharp public class MyLightSchemeMapper : LightSchemeMapper> { protected override void MapCore(MyCorePalette palette, MyScheme 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]; } } ``` -------------------------------- ### Custom Core Palette with Orange Tones Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities/README.md Subclass CorePalette to add a custom TonalPalette (e.g., Orange) and override the Fill method to populate it with harmonized colors based on a seed. ```csharp public class MyCorePalette : CorePalette { public TonalPalette Orange { get; set; } public override void Fill(uint seed, Style style = Style.TonalSpot) { base.Fill(seed, style); // You can harmonize a color to make it closer to the seed color uint harmonizedOrange = Blender.Harmonize(0xFFA500, seed); Orange = TonalPalette.FromInt(harmonizedOrange); } } ``` -------------------------------- ### Update App.xaml for XAML Theming Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md Integrate the MaterialColorUtilities.Maui namespace and include the MaterialColorResourceDictionary in your App.xaml. This merges the generated color resources with your application's existing resources. ```xml + + + + ``` -------------------------------- ### Inject and Use MaterialColorService in C# Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md Inject MaterialColorService into your services to access current palettes, schemes, and update seed color or style. ```csharp using MaterialColorUtilites.Maui; public class MyService { public MyService(MaterialColorService materialColorService) { Scheme scheme = materialColorService.SchemeMaui; materialColorService.Seed = 0x123456; materialColorService.Style = Style.Spritz; } } ``` -------------------------------- ### XAML Color Definitions Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md Copy these Color and SolidColorBrush definitions into your App.xaml ResourceDictionary to enable Material Design color theming. These provide the base color resources used by the library. ```xml ``` -------------------------------- ### Access Primary Color in C# Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md Access colors in C# using Element.SetDynamicResource(). ```csharp Button button = new(); button.SetDynamicResource(Button.BackgroundProperty, "Primary"); ``` -------------------------------- ### Access Primary Color in XAML Source: https://github.com/albi005/materialcolorutilities/blob/main/MaterialColorUtilities.Maui/README.md Access colors as global resources in XAML using DynamicResource. ```xml