# In-App Contrast Checker

## Objective

Measure contrast against what actually rendered, not against what the stylesheet claimed.

A development-mode checker that samples real rendered foreground and background pairs and reports the ones that fail.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Read the computed style of each element and the effective colour behind it, walking up through ancestors until an opaque background is found. Source values in the stylesheet are not what the user sees once inheritance, overrides, and alpha have been applied.
2. Evaluate text and non-text separately against their own thresholds — body copy, large headings, icons, focus rings, and control borders do not share one number, and applying a single threshold produces both false failures and false passes.
3. Handle the cases where there is no single background colour: partial opacity, gradients, photographic images, and video. Sample the actual composited pixels behind the text and report the worst region rather than the average.
4. Report the failure with the offending element, its two colours, the measured ratio, and the nearest token that would pass. Do not mutate the theme to fix it — correcting the ramp belongs to Brand Palette Generator, and correcting the mapping belongs to Semantic Color Tokens.
5. Keep the checker out of the production bundle and out of the normal render path. A contrast pass that runs on every paint in front of customers is a performance regression sold as an accessibility feature.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Text sitting on a gradient, a photograph, or a partially transparent panel has no single background colour — sample the composited result across the text's own bounds and report the weakest point, not a convenient average.
- Text and non-text elements are held to different thresholds, and large text to a different threshold again, so the checker must classify each element by rendered size and weight before judging it.
- Computed styles are the only honest source. A component whose colour comes from an inherited variable, a utility class, or an inline override will report a passing source value and a failing rendered one.
- The checker suggests; it never edits. Silently rewriting a theme to satisfy a ratio produces a palette nobody chose and hides the original decision that caused the failure.
- Elements that are hidden, clipped to zero size, or positioned off-screen must be skipped, or the report fills with failures nobody can see or act on.
- Disabled controls and purely decorative text are exempt by specification — flagging them trains people to ignore the whole report.
- Running the check while a transition is mid-flight samples an intermediate colour; wait for the element to settle before measuring.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] The checker reads computed and composited values, not stylesheet source values.
- [ ] Text and non-text elements are judged against separate, correct thresholds.
- [ ] Text over gradients, images, and translucent surfaces is measured against the pixels actually behind it.
- [ ] Each failure names the element, both colours, the measured ratio, and a passing alternative.
- [ ] No theme value is changed by the checker itself.
- [ ] Hidden, off-screen, disabled, and decorative elements are excluded from the report.
- [ ] The checker is absent from production builds.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
