Example #1
0
        private static ResourceDictionary GetColourResources(ThemeConfiguration config, AssemblyName asm)
        {
            var colourResources = new ResourceDictionary();

            if (config.GetColourMode() == ThemeColourMode.Light)
            {
                colourResources.MergedDictionaries.Add(new ResourceDictionary()
                {
                    Source = new Uri($"pack://application:,,,/{asm.Name};component/Themes/LightColours.xaml", UriKind.Absolute)
                });
            }
            else
            {
                colourResources.MergedDictionaries.Add(new ResourceDictionary()
                {
                    Source = new Uri($"pack://application:,,,/{asm.Name};component/Themes/DarkColours.xaml", UriKind.Absolute)
                });
            }

            var accent          = config.GetAccentColour();
            var accentLightness = 0.299 * ((double)accent.R / 255) + 0.587 * ((double)accent.G / 255) + 0.114 * ((double)accent.B / 255);

            colourResources["SystemAccentColor"] = accent;

            colourResources["SystemAccentLighten3Brush"] = new SolidColorBrush(accent.Lighten(0.6f));
            colourResources["SystemAccentLighten2Brush"] = new SolidColorBrush(accent.Lighten(0.4f));
            colourResources["SystemAccentLighten1Brush"] = new SolidColorBrush(accent.Lighten(0.2f));
            colourResources["SystemAccentBrush"]         = new SolidColorBrush(accent);
            colourResources["SystemAccentDarken1Brush"]  = new SolidColorBrush(accent.Darken(0.2f));
            colourResources["SystemAccentDarken2Brush"]  = new SolidColorBrush(accent.Darken(0.4f));
            colourResources["SystemAccentDarken3Brush"]  = new SolidColorBrush(accent.Darken(0.6f));

            colourResources["SystemAccentForegroundBrush"] = new SolidColorBrush(accentLightness > 0.5 ? Colors.Black : Colors.White);
            colourResources["SystemAccentBackgroundBrush"] = new SolidColorBrush(accent)
            {
                Opacity = 0.5
            };
            return(colourResources);
        }
Example #2
0
        private static void LoadTheme(ThemeConfiguration config, ResourceDictionary current)
        {
            CurrentConfiguration = config;

            var asm = Assembly.GetExecutingAssembly().GetName();

            current["SystemFontFamily"] = config.FontFamily;
            current["SystemFontSize"]   = config.FontSize;

            current["SystemMonospaceFontFamily"] = config.MonospaceFontFamily;
            current["SystemMonospaceFontSize"]   = config.MonospaceFontSize;
            var colourResources = GetColourResources(config, asm);

            current.MergedDictionaries.Add(colourResources);

            SystemEvents.UserPreferenceChanged += (o, e) =>
            {
                if (e.Category == UserPreferenceCategory.General)
                {
                    current.MergedDictionaries.Remove(colourResources);
                    colourResources = GetColourResources(config, asm);
                    current.MergedDictionaries.Add(colourResources);

                    foreach (var item in Application.Current.Windows.OfType <Window>())
                    {
                        item.InvalidateVisual();
                    }
                }
            };

            if (!config.NoLoad)
            {
                current.MergedDictionaries.Add(new ResourceDictionary()
                {
                    Source = new Uri($"pack://application:,,,/{asm.Name};component/Themes/All.xaml", UriKind.Absolute)
                });
            }
        }
Example #3
0
 public static void SetTheme(FrameworkElement element, ThemeConfiguration config)
 {
     LoadTheme(config, element.Resources);
 }
Example #4
0
 public static void SetTheme(ThemeConfiguration config)
 {
     LoadTheme(config, Application.Current.Resources);
 }