0

I've noticed this on the MahApps site (bottom of the page): "It is also possible to create an accent resource dictionary dynamically by using a specific color.", but I found more nowhere.

Is there really a built-in method (or anything else) for this?
I've only found ThemeManager.AddAccent(string name, Uri resourceAddress), and creating a new accent (currently) possible with new Accent(string name, Uri resourceAddress), so a name and a resource uri is everyhow needed...
Any ideas?

punker76
  • 14,326
  • 5
  • 58
  • 96
nvi9
  • 1,733
  • 2
  • 15
  • 20

2 Answers2

3

Here is a simple sample to create a dynamic resource dictionary and add it to the ThemeManager:

public static class ThemeManagerHelper
{
    public static void CreateAppStyleBy(Color color, bool changeImmediately = false)
    {
        // create a runtime accent resource dictionary

        var resourceDictionary = new ResourceDictionary();

        resourceDictionary.Add("HighlightColor", color);
        resourceDictionary.Add("AccentColor", Color.FromArgb((byte)(204), color.R, color.G, color.B));
        resourceDictionary.Add("AccentColor2", Color.FromArgb((byte)(153), color.R, color.G, color.B));
        resourceDictionary.Add("AccentColor3", Color.FromArgb((byte)(102), color.R, color.G, color.B));
        resourceDictionary.Add("AccentColor4", Color.FromArgb((byte)(51), color.R, color.G, color.B));

        resourceDictionary.Add("HighlightBrush", new SolidColorBrush((Color)resourceDictionary["HighlightColor"]));
        resourceDictionary.Add("AccentColorBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
        resourceDictionary.Add("AccentColorBrush2", new SolidColorBrush((Color)resourceDictionary["AccentColor2"]));
        resourceDictionary.Add("AccentColorBrush3", new SolidColorBrush((Color)resourceDictionary["AccentColor3"]));
        resourceDictionary.Add("AccentColorBrush4", new SolidColorBrush((Color)resourceDictionary["AccentColor4"]));
        resourceDictionary.Add("WindowTitleColorBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));

        resourceDictionary.Add("ProgressBrush", new LinearGradientBrush(
            new GradientStopCollection(new[]
            {
                new GradientStop((Color)resourceDictionary["HighlightColor"], 0),
                new GradientStop((Color)resourceDictionary["AccentColor3"], 1)
            }),
            new Point(0.001, 0.5), new Point(1.002, 0.5)));

        resourceDictionary.Add("CheckmarkFill", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
        resourceDictionary.Add("RightArrowFill", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));

        resourceDictionary.Add("IdealForegroundColor", Colors.White);
        resourceDictionary.Add("IdealForegroundColorBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
        resourceDictionary.Add("AccentSelectedColorBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));

        // DataGrid brushes since latest alpha after 1.1.2
        resourceDictionary.Add("MetroDataGrid.HighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
        resourceDictionary.Add("MetroDataGrid.HighlightTextBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
        resourceDictionary.Add("MetroDataGrid.MouseOverHighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor3"]));
        resourceDictionary.Add("MetroDataGrid.FocusBorderBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
        resourceDictionary.Add("MetroDataGrid.InactiveSelectionHighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor2"]));
        resourceDictionary.Add("MetroDataGrid.InactiveSelectionHighlightTextBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));

        // applying theme to MahApps

        var resDictName = string.Format("ApplicationAccent_{0}.xaml", color.ToString().Replace("#", string.Empty));
        var fileName = Path.Combine(Path.GetTempPath(), resDictName);
        using (var writer = System.Xml.XmlWriter.Create(fileName, new System.Xml.XmlWriterSettings { Indent = true }))
        {
            System.Windows.Markup.XamlWriter.Save(resourceDictionary, writer);
            writer.Close();
        }

        resourceDictionary = new ResourceDictionary() { Source = new Uri(fileName, UriKind.Absolute) };

        var newAccent = new Accent { Name = resDictName, Resources = resourceDictionary };
        ThemeManager.AddAccent(newAccent.Name, newAccent.Resources.Source);

        if (changeImmediately)
        {
            var application = Application.Current;
            var applicationTheme = ThemeManager.AppThemes.First(x => string.Equals(x.Name, "BaseLight"));
            ThemeManager.ChangeAppStyle(application, newAccent, applicationTheme);
        }
    }
}

Usage:

ThemeManagerHelper.CreateAppStyleBy(Colors.Indigo, true);

This is taken from my code samples (MahAppsMetroThemesSample) hosted at GitHub

Hope this helps!

punker76
  • 14,326
  • 5
  • 58
  • 96
  • Wow it is amazing, thank you very much!!! Just a suggestion: for `IdealForegroundColor` the color can be created programmatically too, based on the brightness of the `color` parameter, like this: `resourceDictionary.Add("IdealForegroundColor", (int)Math.Sqrt(color.R * color.R * .241 + color.G * color.G * .691 + color.B * color.B * .068) < 130 ? Colors.White : Colors.Black);` ([source](http://themergency.com/calculate-text-color-based-on-background-color-brightness/)) – nvi9 Sep 22 '15 at 18:13
1

ThemeManager has since been changed and now natively supports creating a custom theme from a color.

Here's an example from MahApps documentation:

// Add Dark and Light versions of DarkRed
ThemeManager.Current.AddTheme(new Theme("CustomDarkRed", "CustomDarkRed", "Dark", "Red", Colors.DarkRed, Brushes.DarkRed, true, false));
ThemeManager.Current.AddTheme(new Theme("CustomLightRed", "CustomLightRed", "Light", "Red", Colors.DarkRed, Brushes.DarkRed, true, false));

//Set current theme to CustomDarkRed
ThemeManager.Current.ChangeTheme(this, "CustomDarkRed");
DaemonFire
  • 573
  • 6
  • 13