Skip to content

Darken rather than invert a color already darker than the background in AdjustForContrast [patch] - #509

Merged
matt-edmondson merged 3 commits into
mainfrom
fix/507-adjust-for-contrast-nearest-direction
Nov 4, 2026
Merged

matt-edmondson merged 3 commits into
mainfrom
fix/507-adjust-for-contrast-nearest-direction

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #507

What was wrong

AdjustForContrast chose its lightness search direction from the background alone, and kept that direction whenever it could reach the target. On a background just below luminance ~0.179, white has slightly more headroom than black, so lightening always won. That held even for a dark foreground that needed only a nudge darker:

Color.FromHex("#303030").AdjustForContrast(Color.FromHex("#737373"), AccessibilityLevel.AA, largeText: true)
// was #CECECE (Oklab distance ≈ 0.54); #2A2A2A already meets 3:1

Change

Both lightness directions are now searched. When both reach the target, the result closer to the original color by DistanceTo is returned. When only one does, it is used as before. When neither does, the mix fallback keeps its existing order, led by the extreme with more headroom. This is the fix the issue suggested.

Tests

  • New AdjustForContrast_DarkensADarkColorOnABackgroundJustBelowTheCrossover pins the issue's example. The result must meet large-text AA, be darker than #303030, and sit within Oklab distance 0.05 of it.
  • With the source change reverted, it fails with "adjusted to #CECECE". With it, the full Semantics.Test suite passes: 1963 passed, 14 skipped, 0 failed. That includes _DarkensOnMidToneBackground, _LightensOnDarkBackground, _KeepsTheHueOfABlueItDarkens and the saturated-color cases.

Generated by Claude Code

…kground in AdjustForContrast [patch]

The lightness search tried first whichever direction's extreme had more
headroom against the background, and kept it whenever it could reach the
target. On a background just below luminance 0.179 that meant lightening,
even for a dark foreground one step short of the target: #303030 on #737373
for large-text AA became #CECECE instead of #2A2A2A. Both lightness directions
are now searched, and when both reach the target the one closer to the
original color (by DistanceTo) is returned. The mix fallback keeps its order.

Fixes #507

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5Cza8WxR4s8ByZ2GX
…under the complexity limit

SonarCloud S3776 flagged AdjustForContrast at cognitive complexity 19 (limit
15) after the both-directions comparison was added inline. The comparison now
lives in NearerOf. The new test's two comparisons use Assert.IsLessThan
(MSTEST0037).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5Cza8WxR4s8ByZ2GX
SonarCloud S3358 flagged the nested conditional in NearerOf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5Cza8WxR4s8ByZ2GX
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit f5ac830 into main Nov 4, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/507-adjust-for-contrast-nearest-direction branch November 4, 2026 00:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Color.AdjustForContrast inverts a dark foreground to light when a tiny darkening would pass (#303030 on #737373 → #CECECE)

2 participants