コード例 #1
0
        private static void ChangeAllWindowCommandsBrush(this MetroWindow window, Brush brush, Position position = Position.Top)
        {
            if (brush == null)
            {
                // set the theme to light by default
                window.InvokeActionOnWindowCommands(x => x.SetValue(WindowCommands.ThemeProperty, Theme.Light),
                                                    x => x.SetValue(WindowButtonCommands.ThemeProperty, Theme.Light), position);

                // clear the foreground property
                window.InvokeActionOnWindowCommands(x => x.ClearValue(Control.ForegroundProperty), null, position);
            }
            else
            {
                // calculate brush color lightness
                var color = ((SolidColorBrush)brush).Color;

                var r = color.R / 255.0f;
                var g = color.G / 255.0f;
                var b = color.B / 255.0f;

                var max = r;
                var min = r;

                if (g > max)
                {
                    max = g;
                }
                if (b > max)
                {
                    max = b;
                }

                if (g < min)
                {
                    min = g;
                }
                if (b < min)
                {
                    min = b;
                }

                var lightness = (max + min) / 2;

                // set the theme based on color lightness
                if (lightness > 0.1)
                {
                    window.InvokeActionOnWindowCommands(x => x.SetValue(WindowCommands.ThemeProperty, Theme.Light),
                                                        x => x.SetValue(WindowButtonCommands.ThemeProperty, Theme.Light), position);
                }
                else
                {
                    window.InvokeActionOnWindowCommands(x => x.SetValue(WindowCommands.ThemeProperty, Theme.Dark),
                                                        x => x.SetValue(WindowButtonCommands.ThemeProperty, Theme.Dark), position);
                }

                // set the foreground property
                window.InvokeActionOnWindowCommands(x => x.SetValue(Control.ForegroundProperty, brush), null, position);
            }
        }