diff --git a/src/Snap.Hutao/Snap.Hutao/Control/Text/DescriptionTextBlock.cs b/src/Snap.Hutao/Snap.Hutao/Control/Text/DescriptionTextBlock.cs index 8ca40a96..fa2e4c67 100644 --- a/src/Snap.Hutao/Snap.Hutao/Control/Text/DescriptionTextBlock.cs +++ b/src/Snap.Hutao/Snap.Hutao/Control/Text/DescriptionTextBlock.cs @@ -3,6 +3,8 @@ // some part of this file came from: // https://github.com/xunkong/desktop/tree/main/src/Desktop/Desktop/Pages/CharacterInfoPage.xaml.cs +using CommunityToolkit.WinUI; +using CommunityToolkit.WinUI.Helpers; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Documents; @@ -101,10 +103,22 @@ public class DescriptionTextBlock : ContentControl private static void AppendColorText(TextBlock text, ReadOnlySpan slice, HexColor color) { + Color targetColor; + if (ThemeHelper.IsDarkMode(text.ActualTheme)) + { + targetColor = color; + } + else + { + HslColor hsl = color.ToHsl(); + hsl.L *= 0.3; + targetColor = HexColor.FromHsl(hsl); + } + text.Inlines.Add(new Run { Text = slice.ToString(), - Foreground = new SolidColorBrush(color), + Foreground = new SolidColorBrush(targetColor), }); } @@ -147,9 +161,112 @@ public class DescriptionTextBlock : ContentControl data = Convert.ToUInt32(hex.ToString(), 16); } + private HexColor(byte r, byte g, byte b, byte a) + { + data = 0; + R = r; + G = g; + B = b; + A = a; + } + public static implicit operator Color(HexColor hexColor) { return Color.FromArgb(hexColor.A, hexColor.R, hexColor.G, hexColor.B); } + + public static HexColor FromHsl(HslColor hsl) + { + double chroma = (1 - Math.Abs((2 * hsl.L) - 1)) * hsl.S; + double h1 = hsl.H / 60; + double x = chroma * (1 - Math.Abs((h1 % 2) - 1)); + double m = hsl.L - (0.5 * chroma); + double r1, g1, b1; + + if (h1 < 1) + { + r1 = chroma; + g1 = x; + b1 = 0; + } + else if (h1 < 2) + { + r1 = x; + g1 = chroma; + b1 = 0; + } + else if (h1 < 3) + { + r1 = 0; + g1 = chroma; + b1 = x; + } + else if (h1 < 4) + { + r1 = 0; + g1 = x; + b1 = chroma; + } + else if (h1 < 5) + { + r1 = x; + g1 = 0; + b1 = chroma; + } + else + { + r1 = chroma; + g1 = 0; + b1 = x; + } + + byte r = (byte)(255 * (r1 + m)); + byte g = (byte)(255 * (g1 + m)); + byte b = (byte)(255 * (b1 + m)); + byte a = (byte)(255 * hsl.A); + + return new(r, g, b, a); + } + + public HslColor ToHsl() + { + const double toDouble = 1.0 / 255; + double r = toDouble * R; + double g = toDouble * G; + double b = toDouble * B; + double max = Math.Max(Math.Max(r, g), b); + double min = Math.Min(Math.Min(r, g), b); + double chroma = max - min; + double h1; + + if (chroma == 0) + { + h1 = 0; + } + else if (max == r) + { + // The % operator doesn't do proper modulo on negative + // numbers, so we'll add 6 before using it + h1 = (((g - b) / chroma) + 6) % 6; + } + else if (max == g) + { + h1 = 2 + ((b - r) / chroma); + } + else + { + h1 = 4 + ((r - g) / chroma); + } + + double lightness = 0.5 * (max + min); + double saturation = chroma == 0 ? 0 : chroma / (1 - Math.Abs((2 * lightness) - 1)); + + HslColor ret; + ret.H = 60 * h1; + ret.S = saturation; + ret.L = lightness; + ret.A = toDouble * A; + return ret; + } } } diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Caching/CacheBase.cs b/src/Snap.Hutao/Snap.Hutao/Core/Caching/CacheBase.cs index e5f726a6..d4614705 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Caching/CacheBase.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Caching/CacheBase.cs @@ -244,7 +244,7 @@ public abstract class CacheBase { try { - logger.LogInformation(EventIds.CacheRemoveFile, "Removing file {file}", file); + logger.LogInformation(EventIds.CacheRemoveFile, "Removing file {file}", file.Path); await file.DeleteAsync().AsTask().ConfigureAwait(false); } catch diff --git a/src/Snap.Hutao/Snap.Hutao/Core/ThemeHelper.cs b/src/Snap.Hutao/Snap.Hutao/Core/ThemeHelper.cs index be0ab011..8d5d09fe 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/ThemeHelper.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/ThemeHelper.cs @@ -58,6 +58,17 @@ public static class ThemeHelper }; } + /// + /// 检查是否为暗黑模式 + /// + /// 当前元素主题 + /// 是否为暗黑模式 + public static bool IsDarkMode(ElementTheme elementTheme) + { + ApplicationTheme appTheme = Ioc.Default.GetRequiredService().RequestedTheme; + return IsDarkMode(elementTheme, appTheme); + } + /// /// 检查是否为暗黑模式 /// diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/AnnouncementContentPage.xaml.cs b/src/Snap.Hutao/Snap.Hutao/View/Page/AnnouncementContentPage.xaml.cs index e489dcb1..5cf3e316 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/AnnouncementContentPage.xaml.cs +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/AnnouncementContentPage.xaml.cs @@ -69,7 +69,9 @@ openInWebview: function(url){ location.href = url }}"; return rawContent; } - if (ThemeHelper.IsDarkMode(theme, Ioc.Default.GetRequiredService().RequestedTheme)) + bool isDarkMode = ThemeHelper.IsDarkMode(theme); + + if (isDarkMode) { rawContent = rawContent .Replace(DarkColor5, LightColor5) @@ -81,7 +83,7 @@ openInWebview: function(url){ location.href = url }}"; } // wrap a default color body around - return $@"().RequestedTheme) ? LightColor1 : DarkColor1)}"">{rawContent}"; + return $@"{rawContent}"; } private async Task LoadAnnouncementAsync(INavigationData data)