using WPFluent.Extensions;
using WPFluent.Interop;
namespace WPFluent.Appearance;
///
/// 允许通过交换动态资源来更新应用程序中控件使用的Accent。
///
///
///
/// AccentColorManager.ChangeAppTheme(Color.FromArgb(0xFF, 0xEE, 0x00, 0xBB),ThemeType.Dark,false);
///
///
/// AccentColorManager.ChangeAppTheme(AccentColorManager.GetColorizationColor(),ThemeType.Dark,false);
///
///
public static class AccentColorManager
{
///
/// 背景 HSV 亮度的最大值,超过该值后,重音上的文字将变暗。
///
private const double BackgroundBrightnessThresholdValue = 80d;
///
/// Updates application resources.
///
private static void UpdateColorResources(
Color systemAccent,
Color primaryAccent,
Color secondaryAccent,
Color tertiaryAccent)
{
if (secondaryAccent.GetBrightness() > BackgroundBrightnessThresholdValue)
{
System.Diagnostics.Debug.WriteLine("INFO | Text on accent is DARK", "WPFluent.Primary");
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(
0x00,
0x00,
0x00,
0x00);
ThemeManager.Current.Resources["AccentTextFillColorDisabled"] = Color.FromArgb(0x5D, 0x00, 0x00, 0x00);
}
else
{
System.Diagnostics.Debug.WriteLine("INFO | Text on accent is LIGHT", "WPFluent.Primary");
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(
0xFF,
0xFF,
0xFF,
0xFF);
ThemeManager.Current.Resources["AccentTextFillColorDisabled"] = Color.FromArgb(0x5D, 0xFF, 0xFF, 0xFF);
}
ThemeManager.Current.Resources["SystemAccentColor"] = systemAccent;
ThemeManager.Current.Resources["SystemFillColorAttention"] = primaryAccent;
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"] =
systemAccent.ToBrush();
ThemeManager.Current.Resources["AccentFillColorDefaultBrush"] = secondaryAccent.ToBrush();
ThemeManager.Current.Resources["AccentFillColorSecondaryBrush"] = secondaryAccent.ToBrush(0.9);
ThemeManager.Current.Resources["AccentFillColorTertiaryBrush"] = secondaryAccent.ToBrush(0.8);
}
///
/// 根据输入的颜色更改应用程序的色彩基调。
///
/// 原色强调色。
/// 如果是,颜色将有所不同。
///
/// 如果颜色取自玻璃色彩系统,则其亮度将借助 HSV 空间的操作而提高。
///
public static void Apply(
Color systemAccent,
ThemeType applicationTheme = ThemeType.Light,
bool systemGlassColor = false)
{
if (systemGlassColor)
{
// WindowGlassColor 比强调色深一点
systemAccent = systemAccent.UpdateBrightness(6f);
}
Color primaryAccent;
Color secondaryAccent;
Color tertiaryAccent;
if (applicationTheme == ThemeType.Dark)
{
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);
}
///
/// 根据输入的颜色更改应用程序的强调色。
///
/// 主色调
/// 浅色或深色可供选择。
/// 第二种备选的浅色或深色(最常用)。
/// 第三种备选的浅色或深色。
public static void Apply(Color systemAccent, Color primaryAccent, Color secondaryAccent, Color tertiaryAccent)
{ UpdateColorResources(systemAccent, primaryAccent, secondaryAccent, tertiaryAccent); }
///
/// 获取SystemFillColorAttention.
///
public static Color PrimaryAccent
{
get
{
object? resource = ThemeManager.Current.Resources["SystemFillColorAttention"];
if (resource is Color color)
{
return color;
}
return Colors.Transparent;
}
}
///
/// Gets the of the SystemFillColorAttention.
///
public static Brush PrimaryAccentBrush => new SolidColorBrush(PrimaryAccent);
///
/// Gets the SystemAccentColorSecondary.
///
public static Color SecondaryAccent
{
get
{
object? resource = ThemeManager.Current.Resources["SystemAccentColorSecondary"];
if (resource is Color color)
{
return color;
}
return Colors.Transparent;
}
}
///
/// Gets the of the SystemAccentColorSecondary.
///
public static Brush SecondaryAccentBrush => new SolidColorBrush(SecondaryAccent);
///
/// Gets the SystemAccentColor.
///
public static Color SystemAccent
{
get
{
object? resource = ThemeManager.Current.Resources["SystemAccentColor"];
if (resource is Color color)
{
return color;
}
return Colors.Transparent;
}
}
///
/// Gets the of the SystemAccentColor.
///
public static Brush SystemAccentBrush => new SolidColorBrush(SystemAccent);
///
/// Gets the SystemAccentColorTertiary.
///
public static Color TertiaryAccent
{
get
{
object? resource = ThemeManager.Current.Resources["SystemAccentColorTertiary"];
if (resource is Color color)
{
return color;
}
return Colors.Transparent;
}
}
///
/// Gets the of the SystemAccentColorTertiary.
///
public static Brush TertiaryAccentBrush => new SolidColorBrush(TertiaryAccent);
}