feat: export getColorContrastContext for LLM-assisted color-contrast fixes Description:#714
Draft
feat: export getColorContrastContext for LLM-assisted color-contrast fixes Description:#714
Conversation
…ches color-contrast and color-contrast-enhanced violations with structured, mathematically-computed data for LLM consumption.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a new named export
getColorContrastContexttonpmIndexthat enrichescolor-contrastandcolor-contrast-enhancedviolations with structured, mathematically-computed data for LLM consumption.Why
LLMs cannot reliably fix color-contrast violations by guessing. Without the actual foreground/background
values and contrast ratio, models apply heuristic darkening that frequently undershoots the required
threshold or adjusts in the wrong direction. This was validated across 90 failures in eval runs — the
root cause in every case was the LLM not knowing the exact colors or the required ratio.
Axe already computes this data (
fgColor,bgColor,contrastRatio,fontSize,fontWeight) but itis discarded when violations are filtered into
ItemsInfo. This function recovers it by parsing thefailureSummarymessage that is preserved in the scan results.How
getColorContrastContext(scanResults)accepts the output ofscanPageorscanHTMLand returnsColorContrastFinding[]. For each violation it:failureSummarystring to extract the foreground, background, actual ratio, requiredratio, font size and weight
the required threshold
ratio is met, returning two pre-computed compliant hex alternatives
AAforcolor-contrast,AAAforcolor-contrast-enhanced)WCAG relative luminance and contrast ratio math is implemented inline with no new dependencies.
Usage
No breaking changes
scanPage and scanHTML return types are unchanged. This is purely additive.
This PR adds...
package[-lock].jsonnpm audit, portable installation on GitHub Actions