Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

fix(ui): keep white labels on dark Material You accent buttons

adnan-bhaldar8ebd56f8a33a
diff --git a/src/Filee.App/Services/MaterialYouPalette.cs b/src/Filee.App/Services/MaterialYouPalette.cs
index f49b8171..26fd0ce8 100644
--- a/src/Filee.App/Services/MaterialYouPalette.cs
+++ b/src/Filee.App/Services/MaterialYouPalette.cs
@@ -150,6 +150,19 @@ public sealed class MaterialYouPalette
private static readonly Color DarkOnAccent = Color.Parse("#1F1E1D");
+ /// <summary>Lowest contrast at which a white label is still kept on a dark-mode accent fill (WCAG's 3:1 for UI components).</summary>
+ public const double MinWhiteLabelContrast = 3.0;
+
+ private static readonly Color DarkModeDarkLabel = Color.Parse("#16141A");
+
+ /// <summary>
+ /// The label colour on a dark-mode accent fill. White stays white whenever it reads (3:1 or better), which keeps the
+ /// buttons the way they looked before: Lavender (#7F77DD) is 3.76:1 with white, but the old luminance cut-off (0.45)
+ /// turned its labels black. Only fills that are too pale for white (pastels, lightened accents) get the dark label.
+ /// </summary>
+ internal static Color DarkModeOnAccent(Color fill) =>
+ ContrastRatio(fill, Colors.White) >= MinWhiteLabelContrast ? Colors.White : DarkModeDarkLabel;
+
/// <summary>
/// Picks the light-mode accent fill and the colour used on top of it. The fill stays as close to the chosen accent
/// as readability allows: the old rule jumped any light accent to a fixed 45% lightness, which turned lavender
@@ -219,7 +232,7 @@ public sealed class MaterialYouPalette
else
{
var darkPrimary = l < 0.65 ? HslToRgb(h, Math.Min(sat * 0.75, 0.65), 0.75) : seed;
- var onPrimary = Luminance(darkPrimary) > 0.45 ? Color.Parse("#16141A") : Colors.White;
+ var onPrimary = DarkModeOnAccent(darkPrimary);
var primaryContainer = HslToRgb(h, Math.Min(sat * 0.5, 0.38), 0.26);
var onPrimaryContainer = HslToRgb(h, Math.Min(sat * 0.7, 0.55), 0.88);
diff --git a/tests/Filee.App.Tests/MaterialYouThemeTests.cs b/tests/Filee.App.Tests/MaterialYouThemeTests.cs
index a40e371d..6c70f051 100644
--- a/tests/Filee.App.Tests/MaterialYouThemeTests.cs
+++ b/tests/Filee.App.Tests/MaterialYouThemeTests.cs
@@ -534,6 +534,31 @@ public class MaterialYouThemeTests
}
}
+ [Fact]
+ public void Dark_accent_labels_stay_white_when_white_reads_and_turn_dark_only_on_pale_fills()
+ {
+ // Lavender in dark mode is #7F77DD, which is 3.76:1 with white. Its Save / New labels must stay white, not black.
+ var lavender = MaterialYouPalette.Generate(Color.Parse("#7F77DD"), isDark: true);
+ Assert.Equal(Color.Parse("#7F77DD"), lavender.Accent);
+ Assert.Equal(Colors.White, lavender.OnAccent);
+
+ // Pale fills cannot carry white: they get the dark label.
+ foreach (var pale in new[] { "#F5C26B", "#8FD6C0" })
+ {
+ var dark = MaterialYouPalette.Generate(Color.Parse(pale), isDark: true);
+ Assert.NotEqual(Colors.White, dark.OnAccent);
+ }
+
+ // Whatever the label is, it always reads on its fill.
+ foreach (var hex in new[] { "#7F77DD", "#1D9E75", "#D85A30", "#378ADD", "#D4537E", "#BA7517", "#639922", "#5F5E5A", "#F5C26B", "#8FD6C0" })
+ {
+ var dark = MaterialYouPalette.Generate(Color.Parse(hex), isDark: true);
+ var ratio = MaterialYouPalette.ContrastRatio(dark.Accent, dark.OnAccent);
+ Assert.True(ratio >= MaterialYouPalette.MinWhiteLabelContrast,
+ $"{hex}: {dark.OnAccent} on {dark.Accent} has contrast {ratio:F2}");
+ }
+ }
+
private static Color ResolveColor(string key, ThemeVariant variant)
{
var window = new Window();