Skip to content

Commit

Permalink
move method ChangeAccentColor to App.xaml.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Dec 26, 2023
1 parent 6d244b0 commit e0fce2b
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
96 changes: 96 additions & 0 deletions src/Starward/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

using CommunityToolkit.WinUI.Helpers;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.Windows.AppLifecycle;
using Starward.Services;
using System;
using System.Globalization;
using System.IO;
using Vanara.PInvoke;
using Windows.UI;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
Expand Down Expand Up @@ -137,5 +141,97 @@ private void InitializeLanguage()



#region Accent Color



private static Color ColorMix(Color input, Color blend, double percent)
{
return Color.FromArgb(255,
(byte)(input.R * percent + blend.R * (1 - percent)),
(byte)(input.G * percent + blend.G * (1 - percent)),
(byte)(input.B * percent + blend.B * (1 - percent)));
}



public static void ChangeAccentColor(ElementTheme theme, Color? backColor = null, Color? foreColor = null)
{
try
{
var colors = new Color[14];
if (backColor != null && foreColor != null)
{
colors[0] = backColor.Value;
for (int i = 1; i < 4; i++)
{
double percent = 1 - 0.2 * i;
colors[i] = ColorMix(backColor.Value, Colors.White, percent);
}
for (int i = 4; i < 7; i++)
{
double percent = 1 - 0.2 * (i - 3);
colors[i] = ColorMix(backColor.Value, Colors.Black, percent);
}

colors[7] = foreColor.Value;
for (int i = 8; i < 11; i++)
{
double percent = 1 - 0.2 * (i - 7);
colors[i] = ColorMix(foreColor.Value, Colors.White, percent);
}
for (int i = 11; i < 14; i++)
{
double percent = 1 - 0.2 * (i - 10);
colors[i] = ColorMix(foreColor.Value, Colors.Black, percent);
}
AppConfig.AccentColor = backColor.Value.ToHex() + foreColor.Value.ToHex();
}
else
{
var setting = new Windows.UI.ViewManagement.UISettings();
colors[0] = setting.GetColorValue(Windows.UI.ViewManagement.UIColorType.Accent);
colors[1] = setting.GetColorValue(Windows.UI.ViewManagement.UIColorType.AccentLight1);
colors[2] = setting.GetColorValue(Windows.UI.ViewManagement.UIColorType.AccentLight2);
colors[3] = setting.GetColorValue(Windows.UI.ViewManagement.UIColorType.AccentLight3);
colors[4] = setting.GetColorValue(Windows.UI.ViewManagement.UIColorType.AccentDark1);
colors[5] = setting.GetColorValue(Windows.UI.ViewManagement.UIColorType.AccentDark2);
colors[6] = setting.GetColorValue(Windows.UI.ViewManagement.UIColorType.AccentDark3);
colors[7] = colors[0];
colors[8] = colors[1];
colors[9] = colors[2];
colors[10] = colors[3];
colors[11] = colors[4];
colors[12] = colors[5];
colors[13] = colors[6];
}
if (theme is ElementTheme.Dark)
{
Application.Current.Resources["SystemAccentColor"] = colors[0];
Application.Current.Resources["SystemAccentColorLight1"] = colors[1];
Application.Current.Resources["SystemAccentColorLight2"] = colors[2];
Application.Current.Resources["SystemAccentColorLight3"] = colors[3];
Application.Current.Resources["AccentTextFillColorPrimaryBrush"] = new SolidColorBrush(colors[10]);
Application.Current.Resources["AccentTextFillColorSecondaryBrush"] = new SolidColorBrush(colors[10]);
Application.Current.Resources["AccentTextFillColorTertiaryBrush"] = new SolidColorBrush(colors[9]);
}
if (theme is ElementTheme.Light)
{
Application.Current.Resources["SystemAccentColor"] = colors[0];
Application.Current.Resources["SystemAccentColorDark1"] = colors[4];
Application.Current.Resources["SystemAccentColorDark2"] = colors[5];
Application.Current.Resources["SystemAccentColorDark3"] = colors[6];
Application.Current.Resources["AccentTextFillColorPrimaryBrush"] = new SolidColorBrush(colors[12]);
Application.Current.Resources["AccentTextFillColorSecondaryBrush"] = new SolidColorBrush(colors[13]);
Application.Current.Resources["AccentTextFillColorTertiaryBrush"] = new SolidColorBrush(colors[11]);
}
}
catch { }
}


#endregion



}
27 changes: 25 additions & 2 deletions src/Starward/MyWindows/WindowEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public abstract class WindowEx : Window
{


public IntPtr WindowHandle { get; init; }
public IntPtr WindowHandle { get; private init; }

public IntPtr BridgeHandle { get; init; }
public IntPtr BridgeHandle { get; private init; }


public double UIScale => User32.GetDpiForWindow(WindowHandle) / 96d;
Expand Down Expand Up @@ -239,6 +239,29 @@ public void SetDragRectangles(params RectInt32[] value)
#endregion


#region Accent Color


public virtual void ChangeAccentColor(Color? backColor = null, Color? foreColor = null)
{
if (Content is FrameworkElement element)
{
App.ChangeAccentColor(element.ActualTheme, backColor, foreColor);
if (element.ActualTheme is ElementTheme.Dark)
{
element.RequestedTheme = ElementTheme.Light;
element.RequestedTheme = ElementTheme.Default;
}
if (element.ActualTheme is ElementTheme.Light)
{
element.RequestedTheme = ElementTheme.Dark;
element.RequestedTheme = ElementTheme.Default;
}
}
}


#endregion


}

0 comments on commit e0fce2b

Please sign in to comment.