apply lightness decrease in light mode #25

This commit is contained in:
DismissedLight
2022-09-08 13:34:50 +08:00
parent a37bac11a9
commit c05ea7d8c6
4 changed files with 134 additions and 4 deletions

View File

@@ -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<char> 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;
}
}
}

View File

@@ -244,7 +244,7 @@ public abstract class CacheBase<T>
{
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

View File

@@ -58,6 +58,17 @@ public static class ThemeHelper
};
}
/// <summary>
/// 检查是否为暗黑模式
/// </summary>
/// <param name="elementTheme">当前元素主题</param>
/// <returns>是否为暗黑模式</returns>
public static bool IsDarkMode(ElementTheme elementTheme)
{
ApplicationTheme appTheme = Ioc.Default.GetRequiredService<App>().RequestedTheme;
return IsDarkMode(elementTheme, appTheme);
}
/// <summary>
/// 检查是否为暗黑模式
/// </summary>

View File

@@ -69,7 +69,9 @@ openInWebview: function(url){ location.href = url }}";
return rawContent;
}
if (ThemeHelper.IsDarkMode(theme, Ioc.Default.GetRequiredService<App>().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 $@"<body style=""{(ThemeHelper.IsDarkMode(theme, Ioc.Default.GetRequiredService<App>().RequestedTheme) ? LightColor1 : DarkColor1)}"">{rawContent}</body>";
return $@"<body style=""{(isDarkMode ? LightColor1 : DarkColor1)}"">{rawContent}</body>";
}
private async Task LoadAnnouncementAsync(INavigationData data)