You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
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):
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.
Priority: High. The method's documented contract is that the result meets the requested WCAG level, but about half of all adjusted colors fail once they're written out as hex or bytes. Hex and bytes are how theme colors are stored, emitted to CSS, and checked by accessibility tools. In the reported example the "fixed" color is byte-identical to the failing input. Nothing errors, so callers ship colors that don't meet the level.
Area:Semantics.Color/Color.Operations.cs, specifically the Search helper inside AdjustForContrast. The 8-bit conversions are in Conversions.cs.
Suggested assignment: Color / accessibility area owner. Downstream consumers are ThemeProvider and ImGui.Styler.
Activity
matt-edmondson commentedon Oct 31, 2026
Triage
Semantics.Color/Color.Operations.cs, specifically theSearchhelper insideAdjustForContrast. The 8-bit conversions are inConversions.cs.AdjustForContrastdefect (choosing the wrong search direction). Open PR Darken rather than invert a color already darker than the background in AdjustForContrast [patch] #509 fixes Color.AdjustForContrast inverts a dark foreground to light when a tiny darkening would pass (#303030 on #737373 → #CECECE) #507 by searching both directions. That PR touches the same search code, so landing it first and putting the quantization check on top should avoid a conflict. ClosedColor.AdjustForContrastreturns the original failing colour for saturated foregrounds (blue on #777777 stays at 1.92:1 though black reaches 4.69:1) #351, Color.AdjustForContrast gives up on mid-tone backgrounds (luminance ~0.18-0.5) because it always lightens there, even when darkening would pass #279 and Color contrast ignores alpha: NamedColors.Transparent on white rates 21:1 / AAA, and AdjustForContrast returns a 5%-opaque foreground unchanged #393 cover earlier, separate fixes to the same method.Generated by Claude Code