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.

Keep Material You accent close to the chosen color

adnan-bhaldarc96cd123a8ac
diff --git a/src/Filee.App/Services/MaterialYouPalette.cs b/src/Filee.App/Services/MaterialYouPalette.cs
index 1a949c4e..0579a5b3 100644
--- a/src/Filee.App/Services/MaterialYouPalette.cs
+++ b/src/Filee.App/Services/MaterialYouPalette.cs
@@ -55,6 +55,41 @@ public sealed class MaterialYouPalette
["SystemAccentColorDark1"] = SoftPalette.Shade(Accent, -0.15),
["SystemAccentColorDark2"] = SoftPalette.Shade(Accent, -0.30),
["SystemAccentColorDark3"] = SoftPalette.Shade(Accent, -0.45),
+
+ // Fluent's own templates read these keys for filled buttons, sliders, checkboxes and focused text boxes.
+ // Pointing them at the Material You accent keeps those controls on the chosen colour, in every state.
+ ["AccentButtonBackground"] = new SolidColorBrush(Accent),
+ ["AccentButtonBackgroundPointerOver"] = new SolidColorBrush(SoftPalette.Shade(Accent, -0.08)),
+ ["AccentButtonBackgroundPressed"] = new SolidColorBrush(SoftPalette.Shade(Accent, -0.16)),
+ ["AccentButtonForeground"] = new SolidColorBrush(OnAccent),
+ ["AccentButtonForegroundPointerOver"] = new SolidColorBrush(OnAccent),
+ ["AccentButtonForegroundPressed"] = new SolidColorBrush(OnAccent),
+ // The slider thumb is a slightly darker shade of the accent so it stays visible against the accent-filled track.
+ ["AccentThumbBrush"] = new SolidColorBrush(SoftPalette.Shade(Accent, -0.2)),
+ ["SliderThumbBackground"] = new SolidColorBrush(SoftPalette.Shade(Accent, -0.2)),
+ ["SliderThumbBackgroundPointerOver"] = new SolidColorBrush(SoftPalette.Shade(Accent, -0.26)),
+ ["SliderThumbBackgroundPressed"] = new SolidColorBrush(SoftPalette.Shade(Accent, -0.32)),
+ ["SliderTrackValueFill"] = new SolidColorBrush(Accent),
+ ["SliderTrackValueFillPointerOver"] = new SolidColorBrush(Accent),
+ ["SliderTrackValueFillPressed"] = new SolidColorBrush(Accent),
+ ["CheckBoxCheckBackgroundFillChecked"] = new SolidColorBrush(Accent),
+ ["CheckBoxCheckBackgroundFillCheckedPointerOver"] = new SolidColorBrush(SoftPalette.Shade(Accent, -0.08)),
+ ["CheckBoxCheckBackgroundFillCheckedPressed"] = new SolidColorBrush(SoftPalette.Shade(Accent, -0.16)),
+ ["CheckBoxCheckBackgroundStrokeChecked"] = new SolidColorBrush(Accent),
+ ["CheckBoxCheckBackgroundStrokeCheckedPointerOver"] = new SolidColorBrush(SoftPalette.Shade(Accent, -0.08)),
+ ["CheckBoxCheckBackgroundStrokeCheckedPressed"] = new SolidColorBrush(SoftPalette.Shade(Accent, -0.16)),
+ ["CheckBoxCheckGlyphForegroundChecked"] = new SolidColorBrush(OnAccent),
+ ["CheckBoxCheckGlyphForegroundCheckedPointerOver"] = new SolidColorBrush(OnAccent),
+ ["CheckBoxCheckGlyphForegroundCheckedPressed"] = new SolidColorBrush(OnAccent),
+ ["TextControlBorderBrushFocused"] = new SolidColorBrush(Accent),
+
+ // The cross in search boxes turns the accent colour on hover and press, with no background behind it.
+ ["TextControlButtonBackgroundPointerOver"] = new SolidColorBrush(Colors.Transparent),
+ ["TextControlButtonBackgroundPressed"] = new SolidColorBrush(Colors.Transparent),
+ ["TextControlButtonBorderBrushPointerOver"] = new SolidColorBrush(Colors.Transparent),
+ ["TextControlButtonBorderBrushPressed"] = new SolidColorBrush(Colors.Transparent),
+ ["TextControlButtonForegroundPointerOver"] = new SolidColorBrush(Accent),
+ ["TextControlButtonForegroundPressed"] = new SolidColorBrush(Accent),
};
/// <summary>True if the colour is monochrome (low saturation or black/white/charcoal).</summary>
@@ -69,6 +104,62 @@ public sealed class MaterialYouPalette
/// <summary>Calculates standard sRGB relative luminance.</summary>
public static double Luminance(Color c) => (0.2126 * c.R + 0.7152 * c.G + 0.0722 * c.B) / 255.0;
+ /// <summary>WCAG 2.x relative luminance (gamma-corrected), used for contrast ratios.</summary>
+ public static double RelativeLuminance(Color c)
+ {
+ static double Channel(byte v)
+ {
+ var x = v / 255.0;
+ return x <= 0.03928 ? x / 12.92 : Math.Pow((x + 0.055) / 1.055, 2.4);
+ }
+
+ return 0.2126 * Channel(c.R) + 0.7152 * Channel(c.G) + 0.0722 * Channel(c.B);
+ }
+
+ /// <summary>WCAG 2.x contrast ratio between two colours (1 to 21).</summary>
+ public static double ContrastRatio(Color a, Color b)
+ {
+ var la = RelativeLuminance(a);
+ var lb = RelativeLuminance(b);
+ return (Math.Max(la, lb) + 0.05) / (Math.Min(la, lb) + 0.05);
+ }
+
+ /// <summary>Minimum contrast between filled accent controls (buttons, checkboxes) and the text or glyph on them.</summary>
+ public const double MinAccentContrast = 4.5;
+
+ private static readonly Color DarkOnAccent = Color.Parse("#1F1E1D");
+
+ /// <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
+ /// (#7F77DD) into a dark blue (#392EB8). Now the seed is used as is when white text already reads on it, nudged
+ /// only slightly darker when that is enough, and light pastels keep their colour and get dark text instead.
+ /// </summary>
+ internal static (Color Primary, Color OnPrimary) LightPrimary(Color seed, double h, double sat, double l)
+ {
+ if (ContrastRatio(seed, Colors.White) >= MinAccentContrast)
+ return (seed, Colors.White);
+
+ // Darken in small steps, keeping hue and saturation, until white text is readable.
+ var candidate = seed;
+ var lightness = l;
+ while (lightness > 0.20 && ContrastRatio(candidate, Colors.White) < MinAccentContrast)
+ {
+ lightness -= 0.005;
+ candidate = HslToRgb(h, sat, lightness);
+ }
+
+ // A small nudge keeps the accent recognisable; a big one would change its character.
+ if (l - lightness <= 0.08)
+ return (candidate, Colors.White);
+
+ // Pastels (peach, rose, amber ...): keep the colour itself and put dark text on it.
+ if (ContrastRatio(seed, DarkOnAccent) >= MinAccentContrast)
+ return (seed, DarkOnAccent);
+
+ return (candidate, Colors.White);
+ }
+
/// <summary>Generates Material You tonal resources for the specified seed and variant.</summary>
public static MaterialYouPalette Generate(Color seed, bool isDark)
{
@@ -80,8 +171,7 @@ public sealed class MaterialYouPalette
if (!isDark)
{
- var primary = l > 0.60 ? HslToRgb(h, sat, 0.45) : seed;
- var onPrimary = Luminance(primary) > 0.55 ? Color.Parse("#1F1E1D") : Colors.White;
+ var (primary, onPrimary) = LightPrimary(seed, h, sat, l);
var primaryContainer = HslToRgb(h, Math.Min(sat * 0.5, 0.40), 0.90);
var onPrimaryContainer = HslToRgb(h, Math.Min(sat * 0.7, 0.60), 0.22);
diff --git a/src/Filee.App/Styles/Soft.axaml b/src/Filee.App/Styles/Soft.axaml
index 5b5d6467..99694d89 100644
--- a/src/Filee.App/Styles/Soft.axaml
+++ b/src/Filee.App/Styles/Soft.axaml
@@ -366,7 +366,7 @@
<Setter Property="Width" Value="8" />
<Setter Property="Height" Value="28" />
<Setter Property="CornerRadius" Value="{DynamicResource SoftRadiusPill}" />
- <Setter Property="Background" Value="{DynamicResource AccentBrush}" />
+ <Setter Property="Background" Value="{DynamicResource AccentThumbBrush}" />
<Setter Property="BorderBrush" Value="{DynamicResource SurfaceBrush}" />
<Setter Property="BorderThickness" Value="1.5" />
<Setter Property="VerticalAlignment" Value="Center" />
@@ -383,6 +383,13 @@
<Setter Property="RenderTransform" Value="scale(1.4, 1.15)" />
</Style>
+ <!-- Search box cross: turns the accent colour on hover and press (no background) instead of Fluent's default blue -->
+ <Style Selector="Window.material-you TextBox /template/ Button#PART_ClearButton:pointerover /template/ ContentPresenter#PART_ContentPresenter, Window.material-you TextBox /template/ Button#PART_ClearButton:pressed /template/ ContentPresenter#PART_ContentPresenter">
+ <Setter Property="Background" Value="Transparent" />
+ <Setter Property="BorderBrush" Value="Transparent" />
+ <Setter Property="Foreground" Value="{DynamicResource AccentBrush}" />
+ </Style>
+
<!-- ToggleButtons (Filter chips / Modifier keys) -->
<Style Selector="Window.material-you ToggleButton">
<Setter Property="CornerRadius" Value="{DynamicResource SoftRadiusSmall}" />
diff --git a/src/Filee.App/Views/Pages/AboutPage.axaml.cs b/src/Filee.App/Views/Pages/AboutPage.axaml.cs
index 53a9e3fa..91cd77c9 100644
--- a/src/Filee.App/Views/Pages/AboutPage.axaml.cs
+++ b/src/Filee.App/Views/Pages/AboutPage.axaml.cs
@@ -37,4 +37,4 @@ public partial class AboutPage : UserControl
if (TopLevel.GetTopLevel(this) is { } top)
await top.Launcher.LaunchUriAsync(new Uri(Services.UpdateService.NewIssueUrl));
}
-}
\ No newline at end of file
+}
diff --git a/tests/Filee.App.Tests/MaterialYouThemeTests.cs b/tests/Filee.App.Tests/MaterialYouThemeTests.cs
index 22aba023..1be4efd9 100644
--- a/tests/Filee.App.Tests/MaterialYouThemeTests.cs
+++ b/tests/Filee.App.Tests/MaterialYouThemeTests.cs
@@ -430,6 +430,91 @@ public class MaterialYouThemeTests
}
}
+ [Fact]
+ public void Light_lavender_accent_stays_lavender_instead_of_turning_dark_blue()
+ {
+ var seed = Color.Parse("#7F77DD");
+ var light = MaterialYouPalette.Generate(seed, isDark: false);
+
+ // Regression for #835: the accent used to be forced to 45% lightness, which gave #392EB8.
+ Assert.NotEqual(Color.Parse("#392EB8"), light.Accent);
+
+ var (seedHue, _, seedLightness) = MaterialYouPalette.RgbToHsl(seed);
+ var (hue, _, lightness) = MaterialYouPalette.RgbToHsl(light.Accent);
+ Assert.InRange(Math.Abs(hue - seedHue), 0, 0.02);
+ Assert.InRange(seedLightness - lightness, 0, 0.08);
+ }
+
+ [Fact]
+ public void Light_accent_text_is_readable_for_every_preset_and_swatch()
+ {
+ var seeds = ThemeService.Presets.Select(p => p.LightColor)
+ .Concat(new[] { "#7F77DD", "#1D9E75", "#D85A30", "#378ADD", "#D4537E", "#BA7517", "#639922", "#5F5E5A" }.Select(hex => Color.Parse(hex)));
+
+ foreach (var seed in seeds)
+ {
+ var light = MaterialYouPalette.Generate(seed, isDark: false);
+ var ratio = MaterialYouPalette.ContrastRatio(light.Accent, light.OnAccent);
+ Assert.True(ratio >= MaterialYouPalette.MinAccentContrast,
+ $"{seed}: {light.OnAccent} on {light.Accent} has contrast {ratio:F2}");
+ }
+ }
+
+ [AvaloniaFact]
+ public void Buttons_slider_checkbox_and_focused_text_box_follow_the_accent_in_light_and_dark()
+ {
+ TestServices.EnsureInitialized("en");
+ var themeService = AppHost.Get<ThemeService>();
+
+ try
+ {
+ themeService.Apply(new ThemeSettings { Style = ThemeStyle.MaterialYou, Accent = "#7F77DD", PresetKey = "lavender" }, "en");
+
+ foreach (var variant in new[] { ThemeVariant.Light, ThemeVariant.Dark })
+ {
+ var accent = ResolveBrush("AccentBrush", variant).Color;
+ var onAccent = ResolveBrush("OnAccentBrush", variant).Color;
+
+ foreach (var key in new[]
+ {
+ "AccentButtonBackground", "SliderTrackValueFill",
+ "CheckBoxCheckBackgroundFillChecked", "CheckBoxCheckBackgroundStrokeChecked",
+ "TextControlBorderBrushFocused", "SystemAccentColor",
+ })
+ {
+ var color = key == "SystemAccentColor" ? ResolveColor(key, variant) : ResolveBrush(key, variant).Color;
+ Assert.True(accent == color, $"{variant}: {key} is {color}, expected the accent {accent}");
+ }
+
+ // The slider thumb is a slightly darker shade of the accent, so it can be told apart from the filled track.
+ var thumb = ResolveBrush("AccentThumbBrush", variant).Color;
+ Assert.NotEqual(accent, thumb);
+ Assert.Equal(thumb, ResolveBrush("SliderThumbBackground", variant).Color);
+ Assert.True(MaterialYouPalette.Luminance(thumb) < MaterialYouPalette.Luminance(accent),
+ $"{variant}: slider thumb {thumb} should be darker than the track {accent}");
+
+ // The cross in search boxes must not fall back to Fluent's default blue on hover or press.
+ Assert.Equal(accent, ResolveBrush("TextControlButtonForegroundPointerOver", variant).Color);
+ Assert.Equal(accent, ResolveBrush("TextControlButtonForegroundPressed", variant).Color);
+ Assert.Equal(Colors.Transparent, ResolveBrush("TextControlButtonBackgroundPointerOver", variant).Color);
+
+ Assert.Equal(onAccent, ResolveBrush("AccentButtonForeground", variant).Color);
+ Assert.Equal(onAccent, ResolveBrush("CheckBoxCheckGlyphForegroundChecked", variant).Color);
+ }
+ }
+ finally
+ {
+ themeService.Apply(new ThemeSettings(), "en");
+ }
+ }
+
+ private static Color ResolveColor(string key, ThemeVariant variant)
+ {
+ var window = new Window();
+ Assert.True(window.TryFindResource(key, variant, out var value), $"Failed to resolve resource {key} for variant {variant}");
+ return Assert.IsType<Color>(value);
+ }
+
private static SolidColorBrush ResolveBrush(string key, ThemeVariant variant)
{
var window = new Window();