solve_for_contrast
Find a foreground color meeting WCAG 2.1 contrast targets for a given background. Returns nearest-compliant hex and achieved contrast ratio, supporting single or multiple targets.
Instructions
Find a foreground color that meets one or more WCAG 2.1 contrast targets against a background. Binary-searches OKLCH lightness (holding hue/chroma fixed) and returns the nearest-compliant hex plus the achieved ratio. Pass target for one target or targets for several; prefer selects lighter/darker/either; hue/chroma pin the foreground chromaticity. If BOTH target and targets are provided, targets takes precedence. The response shape differs by mode: a single target returns { met, color, ratio } (with an optional nearMiss flag); targets returns { results: [{ met, color, ratio, nearMiss? }, ...] }. The output schema is an all-optional superset of both shapes (SDK 1.29 single-shape limitation). An unparseable background returns the PARSE_FAILED error; a translucent background (alpha < 1, e.g. rgba()/hsla()/#rgba/#rrggbbaa) returns ALPHA_UNSUPPORTED — composite it over its backdrop first.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hue | No | Fixed OKLCH hue (degrees, cyclic) held constant during the lightness search. Giving hue WITHOUT chroma does not add saturation: chroma then defaults to the background's own chroma, which is 0 for achromatic backgrounds (white/grey/black), so the result is an achromatic gray and the hue has no visible effect — pass chroma explicitly to keep saturation when fixing hue. | |
| chroma | No | Fixed OKLCH chroma (>= 0) held constant during the lightness search (reduced along the fixed hue when the sRGB gamut requires it). Defaults to the background's own chroma when omitted; pass chroma explicitly to keep saturation when fixing hue. Giving chroma WITHOUT hue fixes the hue to the background's hue, or to 0 when the background is achromatic. | |
| prefer | No | Search direction relative to the background lightness. Default "either". | |
| target | No | Single WCAG 2.1 contrast target (e.g. 4.5, 7). Use this OR `targets` (targets wins if both given). | |
| targets | No | Multiple WCAG 2.1 contrast targets (1-50 entries; an empty array is rejected). Takes precedence over `target`. Returns one result per target. | |
| background | Yes | Background CSS color string, e.g. "#FFFFFF", "oklch(0.6 0.1 240)" (max 256 chars). |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| met | No | SINGLE-target: whether the target contrast was achievable | |
| color | No | SINGLE-target: nearest-compliant foreground hex, or null when unreachable | |
| ratio | No | SINGLE-target: achieved WCAG 2.1 ratio (display-rounded), or peak attainable when not met | |
| results | No | MULTI-target: one result per requested target (present only for the `targets` path) | |
| nearMiss | No | SINGLE-target: present/true when met was granted via the near-ceiling tolerance |