Skip to content

Color.AdjustForContrast lands exactly on the threshold, so about half its results fail the target once written as hex/bytes (#A7E058 on #8815C9 → AA comes back as #A7E058, still 4.486:1) #522

Open

Description

@matt-edmondson

What's wrong

Color.AdjustForContrast (Semantics.Color/Color.Operations.cs#L104-L171) binary-searches for the smallest movement that meets the required ratio and returns that double-precision color. The result therefore sits at the threshold, around 4.50000:1 for AA. Once it goes through any 8-bit output (ToHex(), ToBytes(), Conversions.cs#L84-L110), the per-channel rounding drops it below the threshold about half the time. Hex/bytes are how a theme color is stored, shown in CSS, and checked by WCAG tools.

Reproduced against the current main build (be8aa23):

Color fg = Color.FromHex("#A7E058");
Color bg = Color.FromHex("#8815C9");
fg.ContrastRatio(bg);                               // 4.4858  -> Fail
Color adj = fg.AdjustForContrast(bg, AccessibilityLevel.AA);
adj.ContrastRatio(bg);                              // 4.50000 -> AA
adj.ToHex();                                        // "#A7E058"  (the input, unchanged)
Color.FromHex(adj.ToHex()).AccessibilityLevelAgainst(bg); // Fail (4.4858)

A larger move fails the same way. #8D8896 on #E7E3F3 starts at 2.74:1, is adjusted to 4.50000:1, writes out as #6A6572, and reads back at 4.487:1, which is Fail.

Over 20,000 random opaque byte pairs (seeded), 17,657 needed an AA adjustment that succeeded in double precision. 8,779 of those (49.7%) fail AA after FromHex(adj.ToHex()). AAA behaves the same way.

Why it matters

The method's contract (XML doc at L86-L103, README "WCAG accessibility" section) is that the result meets the requested level. A caller that runs text.AdjustForContrast(background, AA) and then saves or emits the color as hex, which is the ThemeProvider/ImGui.Styler migration path the color spec describes, ships a color that an accessibility checker rejects. When the adjustment is under half a step per channel, the "fixed" color is byte-identical to the failing input.

Suggested fix

Have the search accept only candidates that still pass after 8-bit sRGB quantization:

  • In Search (L131-L158), test Quantize(candidate).ContrastRatio(background) >= required, where Quantize is FromBytes(ToBytes()) keeping alpha. Then return the quantized color, or step hi outward until the quantized form passes.
  • Alternatively, search against a slightly raised target (for example required + ε) and verify the quantized result, falling back to the next byte step in the chosen direction.

The extreme fallback (pure black/white) is already exact in 8-bit, so it needs no change.

Acceptance criteria

  • For every opaque byte foreground/background pair where black or white meets the target, Color.FromHex(fg.AdjustForContrast(bg, level, largeText).ToHex()).ContrastRatio(bg) >= RequiredContrast(level, largeText). A seeded random-sample test like the one above should report zero failures for AA, AAA, and large-text AA.
  • #A7E058 on #8815C9 (AA) returns a color whose hex differs from the input and passes AA.
  • The existing "minimal change" tests still pass. The quantized result should be at most one 8-bit step further than today's.

Activity

  1. added
    bug
    on Oct 31, 2026
  2. matt-edmondson commented on Oct 31, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions