2025-05-05 17:04:06 +08:00
|
|
|
|
using WPFluent.Extensions;
|
2025-02-10 20:53:40 +08:00
|
|
|
|
using WPFluent.Interop;
|
|
|
|
|
|
|
|
|
|
|
|
namespace WPFluent.Appearance;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
/// 允许通过交换动态资源来更新应用程序中控件使用的Accent。
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <example>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
/// <code lang="csharp">
|
2025-05-05 17:04:06 +08:00
|
|
|
|
/// AccentColorManager.ChangeAppTheme(Color.FromArgb(0xFF, 0xEE, 0x00, 0xBB),ThemeType.Dark,false);
|
2025-04-24 20:56:44 +08:00
|
|
|
|
/// </code>
|
|
|
|
|
|
/// <code lang="csharp">
|
2025-05-05 17:04:06 +08:00
|
|
|
|
/// AccentColorManager.ChangeAppTheme(AccentColorManager.GetColorizationColor(),ThemeType.Dark,false);
|
2025-04-24 20:56:44 +08:00
|
|
|
|
///</code>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// </example>
|
2025-05-05 17:04:06 +08:00
|
|
|
|
public static class AccentColorManager
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
/// 背景 HSV 亮度的最大值,超过该值后,重音上的文字将变暗。
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
private const double BackgroundBrightnessThresholdValue = 80d;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Updates application resources.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static void UpdateColorResources(
|
|
|
|
|
|
Color systemAccent,
|
|
|
|
|
|
Color primaryAccent,
|
|
|
|
|
|
Color secondaryAccent,
|
|
|
|
|
|
Color tertiaryAccent)
|
|
|
|
|
|
{
|
2025-04-24 20:56:44 +08:00
|
|
|
|
|
|
|
|
|
|
if (secondaryAccent.GetBrightness() > BackgroundBrightnessThresholdValue)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
2025-07-11 09:20:23 +08:00
|
|
|
|
System.Diagnostics.Debug.WriteLine("INFO | Text on accent is DARK", "WPFluent.Primary");
|
2025-05-05 17:04:06 +08:00
|
|
|
|
ThemeManager.Current.Resources["TextOnAccentFillColorPrimary"] = Color.FromArgb(0xFF, 0x00, 0x00, 0x00);
|
|
|
|
|
|
ThemeManager.Current.Resources["TextOnAccentFillColorSecondary"] = Color.FromArgb(0x80, 0x00, 0x00, 0x00);
|
|
|
|
|
|
ThemeManager.Current.Resources["TextOnAccentFillColorDisabled"] = Color.FromArgb(0x77, 0x00, 0x00, 0x00);
|
|
|
|
|
|
ThemeManager.Current.Resources["TextOnAccentFillColorSelectedText"] = Color.FromArgb(
|
2025-02-10 20:53:40 +08:00
|
|
|
|
0x00,
|
|
|
|
|
|
0x00,
|
|
|
|
|
|
0x00,
|
|
|
|
|
|
0x00);
|
2025-05-05 17:04:06 +08:00
|
|
|
|
ThemeManager.Current.Resources["AccentTextFillColorDisabled"] = Color.FromArgb(0x5D, 0x00, 0x00, 0x00);
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-11 09:20:23 +08:00
|
|
|
|
System.Diagnostics.Debug.WriteLine("INFO | Text on accent is LIGHT", "WPFluent.Primary");
|
2025-05-05 17:04:06 +08:00
|
|
|
|
ThemeManager.Current.Resources["TextOnAccentFillColorPrimary"] = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
|
|
|
|
|
|
ThemeManager.Current.Resources["TextOnAccentFillColorSecondary"] = Color.FromArgb(0x80, 0xFF, 0xFF, 0xFF);
|
|
|
|
|
|
ThemeManager.Current.Resources["TextOnAccentFillColorDisabled"] = Color.FromArgb(0x87, 0xFF, 0xFF, 0xFF);
|
|
|
|
|
|
ThemeManager.Current.Resources["TextOnAccentFillColorSelectedText"] = Color.FromArgb(
|
2025-02-10 20:53:40 +08:00
|
|
|
|
0xFF,
|
|
|
|
|
|
0xFF,
|
|
|
|
|
|
0xFF,
|
|
|
|
|
|
0xFF);
|
2025-05-05 17:04:06 +08:00
|
|
|
|
ThemeManager.Current.Resources["AccentTextFillColorDisabled"] = Color.FromArgb(0x5D, 0xFF, 0xFF, 0xFF);
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-05 17:04:06 +08:00
|
|
|
|
ThemeManager.Current.Resources["SystemAccentColor"] = systemAccent;
|
2025-07-11 09:20:23 +08:00
|
|
|
|
ThemeManager.Current.Resources["SystemFillColorAttention"] = primaryAccent;
|
2025-05-05 17:04:06 +08:00
|
|
|
|
ThemeManager.Current.Resources["SystemAccentColorSecondary"] = secondaryAccent;
|
|
|
|
|
|
ThemeManager.Current.Resources["SystemAccentColorTertiary"] = tertiaryAccent;
|
|
|
|
|
|
|
|
|
|
|
|
ThemeManager.Current.Resources["SystemAccentBrush"] = secondaryAccent.ToBrush();
|
|
|
|
|
|
ThemeManager.Current.Resources["SystemFillColorAttentionBrush"] = secondaryAccent.ToBrush();
|
|
|
|
|
|
ThemeManager.Current.Resources["AccentTextFillColorPrimaryBrush"] = tertiaryAccent.ToBrush();
|
|
|
|
|
|
ThemeManager.Current.Resources["AccentTextFillColorSecondaryBrush"] = tertiaryAccent.ToBrush();
|
|
|
|
|
|
ThemeManager.Current.Resources["AccentTextFillColorTertiaryBrush"] = secondaryAccent.ToBrush();
|
|
|
|
|
|
ThemeManager.Current.Resources["AccentFillColorSelectedTextBackgroundBrush"] =
|
2025-02-10 20:53:40 +08:00
|
|
|
|
systemAccent.ToBrush();
|
2025-05-05 17:04:06 +08:00
|
|
|
|
ThemeManager.Current.Resources["AccentFillColorDefaultBrush"] = secondaryAccent.ToBrush();
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
2025-05-05 17:04:06 +08:00
|
|
|
|
ThemeManager.Current.Resources["AccentFillColorSecondaryBrush"] = secondaryAccent.ToBrush(0.9);
|
|
|
|
|
|
ThemeManager.Current.Resources["AccentFillColorTertiaryBrush"] = secondaryAccent.ToBrush(0.8);
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
/// 根据输入的颜色更改应用程序的色彩基调。
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// </summary>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
/// <param name="systemAccent">原色强调色。</param>
|
2025-05-05 17:04:06 +08:00
|
|
|
|
/// <param name="applicationTheme">如果是<see cref="ThemeType.Dark"/>,颜色将有所不同。</param>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <param name="systemGlassColor">
|
2025-04-24 20:56:44 +08:00
|
|
|
|
/// 如果颜色取自玻璃色彩系统,则其亮度将借助 HSV 空间的操作而提高。
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// </param>
|
|
|
|
|
|
public static void Apply(
|
|
|
|
|
|
Color systemAccent,
|
2025-05-05 17:04:06 +08:00
|
|
|
|
ThemeType applicationTheme = ThemeType.Light,
|
2025-02-10 20:53:40 +08:00
|
|
|
|
bool systemGlassColor = false)
|
|
|
|
|
|
{
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (systemGlassColor)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
2025-04-24 20:56:44 +08:00
|
|
|
|
// WindowGlassColor 比强调色深一点
|
2025-02-10 20:53:40 +08:00
|
|
|
|
systemAccent = systemAccent.UpdateBrightness(6f);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Color primaryAccent;
|
|
|
|
|
|
Color secondaryAccent;
|
|
|
|
|
|
Color tertiaryAccent;
|
|
|
|
|
|
|
2025-05-05 17:04:06 +08:00
|
|
|
|
if (applicationTheme == ThemeType.Dark)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
primaryAccent = systemAccent.Update(15f, -12f);
|
|
|
|
|
|
secondaryAccent = systemAccent.Update(30f, -24f);
|
|
|
|
|
|
tertiaryAccent = systemAccent.Update(45f, -36f);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
primaryAccent = systemAccent.UpdateBrightness(-5f);
|
|
|
|
|
|
secondaryAccent = systemAccent.UpdateBrightness(-10f);
|
|
|
|
|
|
tertiaryAccent = systemAccent.UpdateBrightness(-15f);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UpdateColorResources(systemAccent, primaryAccent, secondaryAccent, tertiaryAccent);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-05-05 17:04:06 +08:00
|
|
|
|
/// 根据输入的颜色更改应用程序的强调色。
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// </summary>
|
2025-05-05 17:04:06 +08:00
|
|
|
|
/// <param name="systemAccent">主色调</param>
|
|
|
|
|
|
/// <param name="primaryAccent">浅色或深色可供选择。</param>
|
|
|
|
|
|
/// <param name="secondaryAccent">第二种备选的浅色或深色(最常用)。</param>
|
|
|
|
|
|
/// <param name="tertiaryAccent">第三种备选的浅色或深色。</param>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
public static void Apply(Color systemAccent, Color primaryAccent, Color secondaryAccent, Color tertiaryAccent)
|
|
|
|
|
|
{ UpdateColorResources(systemAccent, primaryAccent, secondaryAccent, tertiaryAccent); }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-11 09:20:23 +08:00
|
|
|
|
/// 获取SystemFillColorAttention.
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Color PrimaryAccent
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2025-07-11 09:20:23 +08:00
|
|
|
|
object? resource = ThemeManager.Current.Resources["SystemFillColorAttention"];
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (resource is Color color)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
return color;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Colors.Transparent;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-11 09:20:23 +08:00
|
|
|
|
/// Gets the <see cref="Brush"/> of the SystemFillColorAttention.
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Brush PrimaryAccentBrush => new SolidColorBrush(PrimaryAccent);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the SystemAccentColorSecondary.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Color SecondaryAccent
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2025-05-05 17:04:06 +08:00
|
|
|
|
object? resource = ThemeManager.Current.Resources["SystemAccentColorSecondary"];
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (resource is Color color)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
return color;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Colors.Transparent;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the <see cref="Brush"/> of the SystemAccentColorSecondary.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Brush SecondaryAccentBrush => new SolidColorBrush(SecondaryAccent);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the SystemAccentColor.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Color SystemAccent
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2025-05-05 17:04:06 +08:00
|
|
|
|
object? resource = ThemeManager.Current.Resources["SystemAccentColor"];
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (resource is Color color)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
return color;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Colors.Transparent;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the <see cref="Brush"/> of the SystemAccentColor.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Brush SystemAccentBrush => new SolidColorBrush(SystemAccent);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the SystemAccentColorTertiary.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Color TertiaryAccent
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2025-05-05 17:04:06 +08:00
|
|
|
|
object? resource = ThemeManager.Current.Resources["SystemAccentColorTertiary"];
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (resource is Color color)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
return color;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Colors.Transparent;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the <see cref="Brush"/> of the SystemAccentColorTertiary.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Brush TertiaryAccentBrush => new SolidColorBrush(TertiaryAccent);
|
|
|
|
|
|
}
|