Skip to content

Let AdjustForContrast darken on mid-tone backgrounds [patch] - #292

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/adjust-for-contrast-mid-tones
Sep 28, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/adjust-for-contrast-mid-tones

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #279

What was wrong

Color.AdjustForContrast picked its search direction with background.RelativeLuminance < 0.5. White and black actually give equal contrast near luminance 0.179. On any background between 0.18 and 0.5, the method only searched toward white. That direction cannot reach AA there (or large-text AA above about 0.30), so the method returned the failing foreground unchanged.

Change

  • The first search now goes toward the extreme that gives more contrast against the background (1.05/(L+0.05) >= (L+0.05)/0.05).
  • If that search can't meet the target, the other direction is tried before the input is returned unchanged. The doc comment's "no adjustment can reach the target" is now true.
  • The binary search itself is unchanged. It moved into a local Search(goLighter) function.

Tests

  • AdjustForContrast_DarkensOnMidToneBackground uses the issue's example: background sRGB 0.58 grey (L ≈ 0.296), AA.
  • AdjustForContrast_ReachesLargeTextAAOnUpperMidToneBackground: background sRGB 0.7 grey (L ≈ 0.45), large-text AA.
  • AdjustForContrast_LightensOnDarkBackground is a guard that dark backgrounds still lighten.

The first two fail on main and pass with this change. The full Semantics.Test suite passes: 1336 passed, 8 skipped.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PKmokmgnpkeYgp439Qbthh


Generated by Claude Code

AdjustForContrast chose its direction with a 0.5 luminance threshold, but
white and black give equal contrast near luminance 0.179. On any background
between those two it searched only the lightening direction, which cannot
reach AA, and returned the failing colour unchanged.

Search first toward the extreme with more contrast headroom, and fall back
to the other direction before giving up.

Fixes #279

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 503aa4e into main Sep 28, 2026
13 checks passed
@matt-edmondson
matt-edmondson deleted the fix/adjust-for-contrast-mid-tones branch September 28, 2026 01:43
This was referenced Sep 28, 2026
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 gives up on mid-tone backgrounds (luminance ~0.18-0.5) because it always lightens there, even when darkening would pass

2 participants