Files
Taskbar-Launcher/Services/ThemeService.cs
2026-06-02 17:28:25 -04:00

85 lines
3.6 KiB
C#

using System.Windows;
using System.Windows.Media;
using Microsoft.Win32;
using MediaColor = System.Windows.Media.Color;
namespace TaskbarLauncher.Services;
public static class ThemeService
{
private const string PersonalizeKey = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
private const string DwmKey = @"Software\Microsoft\Windows\DWM";
public static void Apply()
{
bool isLight = IsLightTheme();
MediaColor accent = GetAccentColor();
MediaColor window = isLight ? MediaColor.FromRgb(0xF7, 0xF7, 0xF7) : MediaColor.FromRgb(0x20, 0x20, 0x20);
MediaColor surface = isLight ? System.Windows.Media.Colors.White : MediaColor.FromRgb(0x2B, 0x2B, 0x2B);
MediaColor control = isLight ? MediaColor.FromRgb(0xF3, 0xF3, 0xF3) : MediaColor.FromRgb(0x33, 0x33, 0x33);
MediaColor hover = isLight ? MediaColor.FromRgb(0xEA, 0xF3, 0xFF) : MediaColor.FromRgb(0x3B, 0x4A, 0x5C);
MediaColor text = isLight ? MediaColor.FromRgb(0x1A, 0x1A, 0x1A) : MediaColor.FromRgb(0xF3, 0xF3, 0xF3);
MediaColor muted = isLight ? MediaColor.FromRgb(0x66, 0x66, 0x66) : MediaColor.FromRgb(0xB8, 0xB8, 0xB8);
MediaColor border = isLight ? MediaColor.FromRgb(0xC8, 0xC8, 0xC8) : MediaColor.FromRgb(0x55, 0x55, 0x55);
MediaColor selectedText = IsDark(accent) ? System.Windows.Media.Colors.White : System.Windows.Media.Colors.Black;
SetBrush("AppWindowBrush", window);
SetBrush("AppSurfaceBrush", surface);
SetBrush("AppControlBrush", control);
SetBrush("AppControlHoverBrush", hover);
SetBrush("AppTextBrush", text);
SetBrush("AppMutedTextBrush", muted);
SetBrush("AppBorderBrush", border);
SetBrush("AppAccentBrush", accent);
SetBrush("AppSelectedTextBrush", selectedText);
SetBrush(System.Windows.SystemColors.WindowBrushKey, surface);
SetBrush(System.Windows.SystemColors.WindowTextBrushKey, text);
SetBrush(System.Windows.SystemColors.ControlBrushKey, control);
SetBrush(System.Windows.SystemColors.ControlTextBrushKey, text);
SetBrush(System.Windows.SystemColors.ControlDarkBrushKey, border);
SetBrush(System.Windows.SystemColors.HighlightBrushKey, accent);
SetBrush(System.Windows.SystemColors.HighlightTextBrushKey, selectedText);
}
private static bool IsLightTheme()
{
object? value = Registry.CurrentUser.OpenSubKey(PersonalizeKey)?.GetValue("AppsUseLightTheme");
return value is null || Convert.ToInt32(value) != 0;
}
private static MediaColor GetAccentColor()
{
object? value = Registry.CurrentUser.OpenSubKey(DwmKey)?.GetValue("AccentColor");
if (value is int colorValue)
{
byte r = (byte)(colorValue & 0xFF);
byte g = (byte)((colorValue >> 8) & 0xFF);
byte b = (byte)((colorValue >> 16) & 0xFF);
return MediaColor.FromRgb(r, g, b);
}
return MediaColor.FromRgb(0x00, 0x78, 0xD4);
}
private static void SetBrush(string key, MediaColor color)
{
SolidColorBrush brush = new(color);
brush.Freeze();
System.Windows.Application.Current.Resources[key] = brush;
}
private static void SetBrush(ResourceKey key, MediaColor color)
{
SolidColorBrush brush = new(color);
brush.Freeze();
System.Windows.Application.Current.Resources[key] = brush;
}
private static bool IsDark(MediaColor color)
{
double luminance = (0.299 * color.R) + (0.587 * color.G) + (0.114 * color.B);
return luminance < 140;
}
}