1
+ using System ;
2
+ using System . Windows ;
3
+ using System . Windows . Media ;
4
+
5
+ namespace MahApps . Metro . Tests . TestHelpers
6
+ {
7
+ public static class AccentHelper
8
+ {
9
+ public static void ApplyColor ( Color color , string accentName = null )
10
+ {
11
+ // create a runtime accent resource dictionary
12
+
13
+ var resDictName = accentName ?? $ "ApplicationAccent_{ color . ToString ( ) . Replace ( "#" , string . Empty ) } .xaml";
14
+
15
+ var resourceDictionary = new ResourceDictionary ( ) ;
16
+
17
+ resourceDictionary . Add ( "HighlightColor" , color ) ;
18
+ resourceDictionary . Add ( "AccentBaseColor" , color ) ;
19
+ resourceDictionary . Add ( "AccentColor" , Color . FromArgb ( ( byte ) ( 204 ) , color . R , color . G , color . B ) ) ;
20
+ resourceDictionary . Add ( "AccentColor2" , Color . FromArgb ( ( byte ) ( 153 ) , color . R , color . G , color . B ) ) ;
21
+ resourceDictionary . Add ( "AccentColor3" , Color . FromArgb ( ( byte ) ( 102 ) , color . R , color . G , color . B ) ) ;
22
+ resourceDictionary . Add ( "AccentColor4" , Color . FromArgb ( ( byte ) ( 51 ) , color . R , color . G , color . B ) ) ;
23
+
24
+ resourceDictionary . Add ( "HighlightBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "HighlightColor" ] ) ) ;
25
+ resourceDictionary . Add ( "AccentBaseColorBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentBaseColor" ] ) ) ;
26
+ resourceDictionary . Add ( "AccentColorBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor" ] ) ) ;
27
+ resourceDictionary . Add ( "AccentColorBrush2" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor2" ] ) ) ;
28
+ resourceDictionary . Add ( "AccentColorBrush3" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor3" ] ) ) ;
29
+ resourceDictionary . Add ( "AccentColorBrush4" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor4" ] ) ) ;
30
+
31
+ resourceDictionary . Add ( "WindowTitleColorBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor" ] ) ) ;
32
+
33
+ resourceDictionary . Add ( "ProgressBrush" , new LinearGradientBrush (
34
+ new GradientStopCollection ( new [ ]
35
+ {
36
+ new GradientStop ( ( Color ) resourceDictionary [ "HighlightColor" ] , 0 ) ,
37
+ new GradientStop ( ( Color ) resourceDictionary [ "AccentColor3" ] , 1 )
38
+ } ) ,
39
+ // StartPoint="1.002,0.5" EndPoint="0.001,0.5"
40
+ startPoint : new Point ( 1.002 , 0.5 ) , endPoint : new Point ( 0.001 , 0.5 ) ) ) ;
41
+
42
+ resourceDictionary . Add ( "CheckmarkFill" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor" ] ) ) ;
43
+ resourceDictionary . Add ( "RightArrowFill" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor" ] ) ) ;
44
+
45
+ resourceDictionary . Add ( "IdealForegroundColor" , IdealTextColor ( color ) ) ;
46
+ resourceDictionary . Add ( "IdealForegroundColorBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "IdealForegroundColor" ] ) ) ;
47
+ resourceDictionary . Add ( "IdealForegroundDisabledBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "IdealForegroundColor" ] , 0.4 ) ) ;
48
+ resourceDictionary . Add ( "AccentSelectedColorBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "IdealForegroundColor" ] ) ) ;
49
+
50
+ resourceDictionary . Add ( "MetroDataGrid.HighlightBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor" ] ) ) ;
51
+ resourceDictionary . Add ( "MetroDataGrid.HighlightTextBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "IdealForegroundColor" ] ) ) ;
52
+ resourceDictionary . Add ( "MetroDataGrid.MouseOverHighlightBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor3" ] ) ) ;
53
+ resourceDictionary . Add ( "MetroDataGrid.FocusBorderBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor" ] ) ) ;
54
+ resourceDictionary . Add ( "MetroDataGrid.InactiveSelectionHighlightBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor2" ] ) ) ;
55
+ resourceDictionary . Add ( "MetroDataGrid.InactiveSelectionHighlightTextBrush" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "IdealForegroundColor" ] ) ) ;
56
+
57
+ resourceDictionary . Add ( "MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchBrush.Win10" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor" ] ) ) ;
58
+ resourceDictionary . Add ( "MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchMouseOverBrush.Win10" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "AccentColor2" ] ) ) ;
59
+ resourceDictionary . Add ( "MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorCheckedBrush.Win10" , GetSolidColorBrush ( ( Color ) resourceDictionary [ "IdealForegroundColor" ] ) ) ;
60
+
61
+ // applying theme to MahApps
62
+ ThemeManager . AddAccent ( resDictName , resourceDictionary ) ;
63
+ var newAccent = ThemeManager . GetAccent ( resDictName ) ;
64
+ // detect current application theme
65
+ Tuple < AppTheme , Accent > applicationTheme = ThemeManager . DetectAppStyle ( Application . Current ) ;
66
+ ThemeManager . ChangeAppStyle ( Application . Current , newAccent , applicationTheme . Item1 ) ;
67
+ }
68
+
69
+ /// <summary>
70
+ /// Determining Ideal Text Color Based on Specified Background Color
71
+ /// http://www.codeproject.com/KB/GDI-plus/IdealTextColor.aspx
72
+ /// </summary>
73
+ /// <param name = "color">The bg.</param>
74
+ /// <returns></returns>
75
+ private static Color IdealTextColor ( Color color )
76
+ {
77
+ const int nThreshold = 105 ;
78
+ var bgDelta = System . Convert . ToInt32 ( ( color . R * 0.299 ) + ( color . G * 0.587 ) + ( color . B * 0.114 ) ) ;
79
+ var foreColor = ( 255 - bgDelta < nThreshold ) ? Colors . Black : Colors . White ;
80
+ return foreColor ;
81
+ }
82
+
83
+ private static SolidColorBrush GetSolidColorBrush ( Color color , double opacity = 1d )
84
+ {
85
+ var brush = new SolidColorBrush ( color ) { Opacity = opacity } ;
86
+ brush . Freeze ( ) ;
87
+ return brush ;
88
+ }
89
+ }
90
+ }
0 commit comments