Mail dark: pagina #080b10 / card #2b3648 met subtiele rand alleen in de dark-regels; licht zonder rand; conversiesimulatie en kleurconflict-test
Continuous Integration / config (pull_request) Successful in 10s
Continuous Integration / changes (pull_request) Successful in 23s
Continuous Integration / config-asf (pull_request) Successful in 9s
Continuous Integration / backend-build (pull_request) Successful in 5m11s
Continuous Integration / vulnerability-scan (pull_request) Successful in 4m22s
Continuous Integration / frontend-prepare (pull_request) Successful in 1m27s
Continuous Integration / backend-test (pull_request) Successful in 7m43s
Continuous Integration / frontend-build (pull_request) Successful in 2m3s
Continuous Integration / frontend-test (pull_request) Successful in 6m5s
Continuous Integration / frontend-lint (pull_request) Successful in 1m52s
Continuous Integration / publish-production (pull_request) Skipped
Continuous Integration / deploy-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / publish-test (pull_request) Successful in 6m7s
Continuous Integration / deploy-test (pull_request) Skipped

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 10:07:32 +01:00
co-authored by Claude Sonnet 5.5
parent d8d150e10d
commit 2818b74604
2 changed files with 82 additions and 15 deletions
@@ -197,15 +197,16 @@ public class MailTemplateRendererTests
} }
[Fact] [Fact]
public void InDarkMode_TheCardStandsOutFromThePage_AndHasABorder() public void InDarkMode_TheCardStandsOutFromThePage_AndHasASubtleBorderOnlyInTheDarkRules()
{ {
var html = _renderer.Render(MailKind.Invitation, "nl", Model()).Html; var html = _renderer.Render(MailKind.Invitation, "nl", Model()).Html;
MailTemplateRenderer.DarkCard.Should().NotBe(MailTemplateRenderer.DarkPage); MailTemplateRenderer.DarkCard.Should().NotBe(MailTemplateRenderer.DarkPage);
// clearly lighter than the page, not the near-identical shades it used to be // clearly lighter than the page, not the near-identical shades it used to be
MailColours.Contrast(MailTemplateRenderer.DarkCard, MailTemplateRenderer.DarkPage).Should().BeGreaterThanOrEqualTo(1.4); MailColours.Contrast(MailTemplateRenderer.DarkCard, MailTemplateRenderer.DarkPage).Should().BeGreaterThanOrEqualTo(1.6);
html.Should().Contain($".mail-page{{background:{MailTemplateRenderer.DarkPage} !important;}}") html.Should().Contain($".mail-page{{background:{MailTemplateRenderer.DarkPage} !important;}}")
.And.Contain($".mail-card{{background:{MailTemplateRenderer.DarkCard} !important;color:#f3f4f6 !important;border:1px solid #475569 !important;}}"); .And.Contain($".mail-card{{background:{MailTemplateRenderer.DarkCard} !important;color:#f3f4f6 !important;border:1px solid {MailTemplateRenderer.DarkCardBorder} !important;}}");
MailTemplateRenderer.DarkCardBorder.Should().Be("#394457");
// light mode is untouched: white card on the light grey page // light mode is untouched: white card on the light grey page
html.Should().Contain("bgcolor=\"#f3f4f6\"").And.Contain("bgcolor=\"#ffffff\""); html.Should().Contain("bgcolor=\"#f3f4f6\"").And.Contain("bgcolor=\"#ffffff\"");
} }
@@ -270,16 +271,17 @@ public class MailTemplateRendererTests
} }
[Fact] [Fact]
public void TheCard_HasAnInlineBorder_ThatSurvivesClientsThatStripTheStyleBlock_AndOutlookHasItsOwnRules() public void LightMode_HasNoBorder_AndOutlookHasItsOwnDarkRules()
{ {
var html = _renderer.Render(MailKind.Invitation, "nl", Model()).Html; var html = _renderer.Render(MailKind.Invitation, "nl", Model()).Html;
// inline, so it also exists where <style> is removed (Gmail app on non-Gmail accounts) and a client's own // the light card has no border of any kind (the border only appears in the dark rules)
// dark conversion still leaves an edge between card and page html.Should().NotContain("border:1px solid #d1d5db");
html.Should().MatchRegex("class=\"mail-card\"[^>]*border:1px solid #d1d5db;border-collapse:separate;"); var cardTag = System.Text.RegularExpressions.Regex.Match(html, "<table[^>]*class=\"mail-card\"[^>]*>").Value;
cardTag.Should().NotContain("border:").And.Contain("border-collapse:separate").And.Contain("border-radius:8px");
// Outlook.com / Outlook apps mark their dark mode with data-ogsb / data-ogsc, outside the media query // Outlook.com / Outlook apps mark their dark mode with data-ogsb / data-ogsc, outside the media query
html.Should().Contain($"[data-ogsb] .mail-page{{background-color:{MailTemplateRenderer.DarkPage} !important;}}") html.Should().Contain($"[data-ogsb] .mail-page{{background-color:{MailTemplateRenderer.DarkPage} !important;}}")
.And.Contain($"[data-ogsb] .mail-card{{background-color:{MailTemplateRenderer.DarkCard} !important;") .And.Contain($"[data-ogsb] .mail-card{{background-color:{MailTemplateRenderer.DarkCard} !important;border:1px solid {MailTemplateRenderer.DarkCardBorder} !important;}}")
.And.Contain("[data-ogsc] .mail-card{color:#f3f4f6 !important;}"); .And.Contain("[data-ogsc] .mail-card{color:#f3f4f6 !important;}");
// the media query stays for the clients that do support it // the media query stays for the clients that do support it
html.Should().Contain("@media (prefers-color-scheme: dark)"); html.Should().Contain("@media (prefers-color-scheme: dark)");
@@ -287,6 +289,70 @@ public class MailTemplateRendererTests
html.Should().Contain("bgcolor=\"#f3f4f6\"").And.Contain("bgcolor=\"#ffffff\""); html.Should().Contain("bgcolor=\"#f3f4f6\"").And.Contain("bgcolor=\"#ffffff\"");
} }
[Fact]
public void ForAClientThatAppliesTheDarkRules_ThereIsNoColourClash_EveryLightTextColourHasADarkOverride()
{
// Thunderbird (and Apple Mail) apply prefers-color-scheme: dark. Text that keeps a fixed dark inline colour on the dark
// card would be unreadable, so every element with an inline colour must carry a class the dark block overrides.
var html = _renderer.Render(MailKind.Invitation, "nl", Model(name: "Bryan")).Html;
var darkBlock = html[html.IndexOf("@media (prefers-color-scheme: dark){", StringComparison.Ordinal)..html.IndexOf("</style>", StringComparison.Ordinal)];
foreach (System.Text.RegularExpressions.Match tag in System.Text.RegularExpressions.Regex.Matches(html, "<(?<name>[a-z0-9]+)(?<attrs>[^>]*style=\"[^\"]*[^-]color:[^\"]*\"[^>]*)>"))
{
var attrs = tag.Groups["attrs"].Value;
var classes = System.Text.RegularExpressions.Regex.Match(attrs, "class=\"(?<c>[^\"]*)\"").Groups["c"].Value
.Split(' ', StringSplitOptions.RemoveEmptyEntries);
// the button link and the logo/site-name cells are handled by their own classes; the card itself by .mail-card
var overridden = classes.Any(c => darkBlock.Contains("." + c + "{") || darkBlock.Contains("." + c + " a{"))
|| (tag.Groups["name"].Value == "a" && darkBlock.Contains(".mail-btn a{")); // the button link, via its cell
overridden.Should().BeTrue($"<{tag.Groups["name"].Value}{attrs}> keeps a fixed colour in dark mode");
}
}
[Fact]
public void ClientConversionSimulation_DocumentsWhatSurvives_TheLightPairLosesItsDifferenceButTheDarkRulesKeepIt()
{
// A client that converts colours itself (Gmail app, non-Gmail account) ignores our dark rules. Model: invert the HSL
// lightness of the inline light colours. It is an approximation, written down so nobody expects more of the
// light pair; the real clients cannot be tested in code.
static string Invert(string hex)
{
int R(int i) => Convert.ToInt32(hex.Substring(i, 2), 16);
var (r, g, b) = (R(1) / 255.0, R(3) / 255.0, R(5) / 255.0);
double max = Math.Max(r, Math.Max(g, b)), min = Math.Min(r, Math.Min(g, b)), l = (max + min) / 2, d = max - min;
double h = 0, sat = 0;
if (d > 0)
{
sat = l > 0.5 ? d / (2 - max - min) : d / (max + min);
h = max == r ? ((g - b) / d + (g < b ? 6 : 0)) : max == g ? (b - r) / d + 2 : (r - g) / d + 4;
h /= 6;
}
var l2 = 1 - l;
double Hue(double p, double q, double t)
{
if (t < 0) t += 1; if (t > 1) t -= 1;
return t < 1.0 / 6 ? p + (q - p) * 6 * t : t < 0.5 ? q : t < 2.0 / 3 ? p + (q - p) * (2.0 / 3 - t) * 6 : p;
}
double r2, g2, b2;
if (sat == 0) r2 = g2 = b2 = l2;
else
{
var q = l2 < 0.5 ? l2 * (1 + sat) : l2 + sat - l2 * sat;
var p = 2 * l2 - q;
r2 = Hue(p, q, h + 1.0 / 3); g2 = Hue(p, q, h); b2 = Hue(p, q, h - 1.0 / 3);
}
return $"#{(int)Math.Round(r2 * 255):x2}{(int)Math.Round(g2 * 255):x2}{(int)Math.Round(b2 * 255):x2}";
}
var light = MailColours.Contrast("#f3f4f6", "#ffffff");
var converted = MailColours.Contrast(Invert("#f3f4f6"), Invert("#ffffff"));
var darkRules = MailColours.Contrast(MailTemplateRenderer.DarkCard, MailTemplateRenderer.DarkPage);
light.Should().BeApproximately(1.10, 0.02);
converted.Should().BeLessThan(light); // ~1.06: the conversion removes the difference
darkRules.Should().BeGreaterThan(light * 1.4); // 1.62: our own dark rules keep a clearly visible difference
}
[Fact] [Fact]
public void TheCard_HasAGutterOnNarrowScreens_InlineOnTheOuterCell_SoThePageColourShowsAtTheSides() public void TheCard_HasAGutterOnNarrowScreens_InlineOnTheOuterCell_SoThePageColourShowsAtTheSides()
{ {
@@ -159,11 +159,12 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
private static string E(string value) => WebUtility.HtmlEncode(value); private static string E(string value) => WebUtility.HtmlEncode(value);
// Dark mode mirrors the light layout: the outer page is the darker surface and the card stands out against it // Dark mode mirrors the light layout: the outer page is the darker surface and the card stands out against it
// (the earlier #111827 / #1f2937 pair was nearly the same shade, so the card vanished). A 1px border keeps the // (the earlier #111827 / #1f2937 pair was nearly the same shade, so the card vanished). The border exists ONLY in
// edge visible in clients that re-colour the mail themselves (Gmail app, Outlook) and flatten the two backgrounds. // the dark rules (subtle); light mode has no border. Clients that convert the colours themselves (Gmail app on a
public const string DarkPage = "#0a0e14"; // non-Gmail account) ignore these rules and show the card less clearly - accepted by Bryan.
public const string DarkCard = "#273244"; public const string DarkPage = "#080b10";
private const string DarkCardBorder = "#475569"; public const string DarkCard = "#2b3648";
public const string DarkCardBorder = "#394457";
private static string BuildHtml( private static string BuildHtml(
MailModel model, string site, string colour, string? greeting, string heading, string intro, MailModel model, string site, string colour, string? greeting, string heading, string intro,
@@ -197,12 +198,12 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
sb.Append("}"); sb.Append("}");
// Outlook.com / Outlook apps ignore the media query and mark their own dark mode with these attributes. // Outlook.com / Outlook apps ignore the media query and mark their own dark mode with these attributes.
sb.Append($"[data-ogsb] .mail-page{{background-color:{DarkPage} !important;}}"); sb.Append($"[data-ogsb] .mail-page{{background-color:{DarkPage} !important;}}");
sb.Append($"[data-ogsb] .mail-card{{background-color:{DarkCard} !important;border-color:{DarkCardBorder} !important;}}"); sb.Append($"[data-ogsb] .mail-card{{background-color:{DarkCard} !important;border:1px solid {DarkCardBorder} !important;}}");
sb.Append("[data-ogsc] .mail-card{color:#f3f4f6 !important;}[data-ogsc] .mail-muted{color:#9ca3af !important;}"); sb.Append("[data-ogsc] .mail-card{color:#f3f4f6 !important;}[data-ogsc] .mail-muted{color:#9ca3af !important;}");
sb.Append("</style></head>"); sb.Append("</style></head>");
sb.Append("<body class=\"mail-page\" style=\"margin:0;padding:0;background:#f3f4f6;\">"); sb.Append("<body class=\"mail-page\" style=\"margin:0;padding:0;background:#f3f4f6;\">");
sb.Append("<table role=\"presentation\" class=\"mail-page\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#f3f4f6\" style=\"background:#f3f4f6;\"><tr><td align=\"center\" style=\"padding:24px 16px;\">"); sb.Append("<table role=\"presentation\" class=\"mail-page\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#f3f4f6\" style=\"background:#f3f4f6;\"><tr><td align=\"center\" style=\"padding:24px 16px;\">");
sb.Append("<table role=\"presentation\" class=\"mail-card\" width=\"560\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#ffffff\" style=\"max-width:560px;width:100%;background:#ffffff;border:1px solid #d1d5db;border-collapse:separate;border-radius:8px;font-family:Arial,Helvetica,sans-serif;color:#111827;\">"); sb.Append("<table role=\"presentation\" class=\"mail-card\" width=\"560\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#ffffff\" style=\"max-width:560px;width:100%;background:#ffffff;border-collapse:separate;border-radius:8px;font-family:Arial,Helvetica,sans-serif;color:#111827;\">");
sb.Append($"<tr><td class=\"mail-accent\" style=\"padding:24px 32px;border-bottom:3px solid {colour};\">"); sb.Append($"<tr><td class=\"mail-accent\" style=\"padding:24px 32px;border-bottom:3px solid {colour};\">");
if (!string.IsNullOrEmpty(model.LogoUrl)) if (!string.IsNullOrEmpty(model.LogoUrl))
{ {