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); } }
private static void ChangeAllWindowCommandsBrush(this MetroWindow window, Brush brush, Position position = 2) { if (brush == null) { window.InvokeActionOnWindowCommands((Control x) => x.SetValue(WindowCommands.ThemeProperty, Theme.Light), (Control x) => x.SetValue(WindowButtonCommands.ThemeProperty, Theme.Light), position); window.InvokeActionOnWindowCommands((Control x) => x.ClearValue(Control.ForegroundProperty), null, position); return; } Color color = ((SolidColorBrush)brush).Color; float r = (float)color.R / 255f; float g = (float)color.G / 255f; float b = (float)color.B / 255f; float single = r; float single1 = r; if (g > single) { single = g; } if (b > single) { single = b; } if (g < single1) { single1 = g; } if (b < single1) { single1 = b; } if ((double)((single + single1) / 2f) <= 0.1) { window.InvokeActionOnWindowCommands((Control x) => x.SetValue(WindowCommands.ThemeProperty, Theme.Dark), (Control x) => x.SetValue(WindowButtonCommands.ThemeProperty, Theme.Dark), position); } else { window.InvokeActionOnWindowCommands((Control x) => x.SetValue(WindowCommands.ThemeProperty, Theme.Light), (Control x) => x.SetValue(WindowButtonCommands.ThemeProperty, Theme.Light), position); } window.InvokeActionOnWindowCommands((Control x) => x.SetValue(Control.ForegroundProperty, brush), null, position); }