diff --git a/App.xaml.cs b/App.xaml.cs index 8c7cdbf..dac81c3 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -15,7 +15,7 @@ public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { - _ = new SettingsPres(); + SettingsPres settingsPres = new(); #region Primary Color PaletteHelper paletteHelper = new(); @@ -38,10 +38,12 @@ public partial class App : Application #region Foreground Color Style newButtonStyle = new(typeof(Button), Current.Resources[typeof(Button)] as Style); - Color? newForegroundColor = ForegroundGenerator.GetForeground(newPrimaryColor.R, newPrimaryColor.G, newPrimaryColor.B); + (Color? newForegroundColor, Color newAccentForegroundColor) = ForegroundGenerator.GetForeground(newPrimaryColor.R, newPrimaryColor.G, newPrimaryColor.B); newButtonStyle.Setters.Add(new Setter(Button.ForegroundProperty, newForegroundColor.HasValue ? new SolidColorBrush(newForegroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); Current.Resources[typeof(Button)] = newButtonStyle; + + settingsPres.AccentForegroundColor = newAccentForegroundColor; #endregion Foreground Color new MainWin(e.Args).Show(); diff --git a/Convs/AboutAccentButtonForegroundConv.cs b/Convs/AboutAccentButtonForegroundConv.cs new file mode 100644 index 0000000..277c84b --- /dev/null +++ b/Convs/AboutAccentButtonForegroundConv.cs @@ -0,0 +1,18 @@ +using System; +using System.Globalization; +using System.Windows.Data; +using System.Windows.Media; + +namespace Sheas_Cealer.Convs; + +internal class AboutAccentButtonForegroundConv : IValueConverter +{ + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + Color accentForegroundColor = (Color)value; + + return new SolidColorBrush(accentForegroundColor); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException(); +} \ No newline at end of file diff --git a/Preses/GlobalPres.cs b/Preses/GlobalPres.cs index 8c00412..af24383 100644 --- a/Preses/GlobalPres.cs +++ b/Preses/GlobalPres.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using System.Windows; +using System.Windows.Media; using CommunityToolkit.Mvvm.ComponentModel; using MaterialDesignThemes.Wpf; using Sheas_Cealer.Props; @@ -36,4 +37,7 @@ internal partial class GlobalPres : ObservableObject Settings.Default.IsLightTheme = (sbyte)(value.HasValue ? value.Value ? 1 : 0 : -1); Settings.Default.Save(); } + + [ObservableProperty] + private static Color accentForegroundColor = (Color)ColorConverter.ConvertFromString("#2196f3"); } \ No newline at end of file diff --git a/Utils/ForegroundGenerator.cs b/Utils/ForegroundGenerator.cs index 2528fcc..33d19e5 100644 --- a/Utils/ForegroundGenerator.cs +++ b/Utils/ForegroundGenerator.cs @@ -5,16 +5,27 @@ namespace Sheas_Cealer.Utils; internal static class ForegroundGenerator { - internal static Color? GetForeground(int red, int green, int blue) + internal static (Color?, Color) GetForeground(int red, int green, int blue) { double luminance = 0.2126 * GammaCorrect(red / 255.0) + 0.7152 * GammaCorrect(green / 255.0) + 0.0722 * GammaCorrect(blue / 255.0); double blackContrast = (luminance + 0.05) / 0.05; double whiteContrast = 1.05 / (luminance + 0.05); - return blackContrast >= 4 && whiteContrast >= 3 ? null : - blackContrast >= whiteContrast ? Color.FromRgb(0, 0, 0) : Color.FromRgb(255, 255, 255); + double hue = GetHue(red / 255.0, green / 255.0, blue / 255.0); + + double blueContrast = Math.Min(Math.Abs(hue - 206.57), 360 - Math.Abs(hue - 206.57)); + double redContrast = Math.Min(Math.Abs(hue), 360 - Math.Abs(hue)); + + return (blackContrast >= 4 && whiteContrast >= 3 ? null : + blackContrast >= whiteContrast ? Color.FromRgb(0, 0, 0) : Color.FromRgb(255, 255, 255), + blueContrast >= redContrast ? (Color)ColorConverter.ConvertFromString("#2196f3") : Color.FromRgb(255, 0, 0)); } - private static double GammaCorrect(double c) => c <= 0.03928 ? c / 12.92 : Math.Pow((c + 0.055) / 1.055, 2.4); + private static double GammaCorrect(double component) => component <= 0.03928 ? component / 12.92 : Math.Pow((component + 0.055) / 1.055, 2.4); + + private static double GetHue(double redComponent, double greenComponent, double blueComponent) => + redComponent > greenComponent && redComponent > blueComponent ? 60 * ((greenComponent - blueComponent) / redComponent - Math.Min(greenComponent, blueComponent) + (greenComponent < blueComponent ? 6 : 0)) : + greenComponent > blueComponent && greenComponent > redComponent ? 60 * ((blueComponent - redComponent) / greenComponent - Math.Min(blueComponent, redComponent) + 2) : + blueComponent > redComponent && blueComponent > greenComponent ? 60 * ((redComponent - greenComponent) / blueComponent - Math.Min(redComponent, greenComponent) + 4) : 0; } \ No newline at end of file diff --git a/Wins/AboutWin.xaml b/Wins/AboutWin.xaml index 3a73ab6..92e26cb 100644 --- a/Wins/AboutWin.xaml +++ b/Wins/AboutWin.xaml @@ -22,11 +22,26 @@ - +