rgb(119, 119, 119)a11y - Accessibility
WCAG (Web Content Accessibility Guidelines) accessibility tools. Determine the readability of text against background colors, and find the best choice among multiple candidate colors.
Install
import xcolor from '@xpyjs/color'
import a11yPlugin from '@xpyjs/color/plugins/a11y'
xcolor.use(a11yPlugin)rgb(118, 118, 118)Target reached
import { adjustContrast } from '@xpyjs/color/plugins/a11y'
adjustContrast("#777777", "#ffffff", {"target":4.5})Target adjustment and theme audits
Named exports adjustContrast(foreground, background, options?) and auditContrastPairs(colors, pairs, options?) work without registration.
import { adjustContrast, auditContrastPairs } from '@xpyjs/color/plugins/a11y'
const adjusted = adjustContrast('#777', '#fff', { target: 4.5 })
adjusted.met // true; ratio is the actual unrounded output contrastAdjustment returns { color, ratio, target, met, changed } without mutating input. Target defaults to 4.5, range 1–21. Direction is auto (default), darker or lighter. It searches RGB paths toward black/white; auto picks the passing direction with the smaller RGB change, not a globally minimal perceptual change. Alpha and source-instance precision options are retained. Already-compliant inputs return independent copies. Unreachable targets return the best permitted endpoint with met: false; translucent backgrounds require an opaque backdrop.
Theme audits accept a flat concrete color map, such as xcolor.getThemeVars() after theme registration. Each pair is { foreground: key, background: key, target? }, with global/per-pair targets and optional backdrop. Results contain token names, unrounded ratio, target and passes. Missing keys, invalid colors, unresolved var() values and unresolved translucent backgrounds throw. Theme values are never changed.
auditContrastPairs(xcolor.getThemeVars(), [{ foreground: '--x-primary-text', background: '--x-primary-bg' }])These results evaluate the supplied colors and target; font size, weight and other accessibility requirements still need separate checks.
Methods
isReadable(other, options?)
Determine whether the combination of the current color and another color meets WCAG readability standards.
| Parameter | Type | Default | Description |
|---|---|---|---|
other | XColorInput | — | Target color to compare |
options.level | 'AA' | 'AAA' | 'AA' | WCAG level |
options.size | 'normal' | 'large' | 'normal' | Text size |
Return value: boolean
xcolor('#000000').isReadable('#ffffff') // true (contrast ratio 21:1)
xcolor('#000000').isReadable('#ffffff', { level: 'AAA' }) // true
xcolor('#888888').isReadable('#ffffff') // false (normal text contrast too low)
xcolor('#888888').isReadable('#ffffff', { size: 'large' }) // true (lower large-text threshold)mostReadable(candidates, options?)
Find the color with the highest contrast ratio against the current color from a list of candidate colors; falls back to black or white if none meets the threshold.
| Parameter | Type | Description |
|---|---|---|
candidates | XColorInput[] | List of candidate colors |
options.level | 'AA' | 'AAA' | WCAG level, default 'AA' |
options.size | 'normal' | 'large' | Text size, default 'normal' |
Return value: XColor
xcolor('#333333').mostReadable(['#ffffff', '#eeeeee', '#cccccc']).toHex()
// '#ffffff' — highest contrast ratiowcagLevel(other)
Return the WCAG levels met by the two colors for normal and large text.
| Parameter | Type | Description |
|---|---|---|
other | XColorInput | Target color to compare |
Return value: { level, largeText, ratio }, where each level is 'AAA' | 'AA' | 'FAIL'.
xcolor('#000000').wcagLevel('#ffffff')
// { level: 'AAA', largeText: 'AAA', ratio: 21 }
xcolor('#666666').wcagLevel('#999999')
// { level: 'FAIL', largeText: 'FAIL', ratio: 2.02 }Practical Scenarios
Ensure Text Readability
const bg = xcolor('#1a1a2e')
const textColor = bg.mostReadable(['#ffffff', '#e0e0e0', '#a0a0a0'])
// Automatically selects the text color with the best contrast ratioValidate Design Mockups
const pairs = [
['#333', '#fff'],
['#666', '#eee'],
['#999', '#ccc']
]
pairs.forEach(([text, bg]) => {
const readable = xcolor(text).isReadable(bg)
const level = xcolor(text).wcagLevel(bg)
console.log(`${text} on ${bg}: ${readable ? '✓' : '✗'} (${level})`)
})Invalid and translucent colors
isReadable() returns false for invalid colors. wcagLevel() throws for an invalid color and mostReadable() throws for an invalid base while skipping invalid candidates. The core contrast() method still returns a number, including when the parser supplied fallback channels.
isReadable(background, options?) and wcagLevel(background, options?) treat the current color as text. mostReadable(candidates, options?) treats the current color as the background and each candidate as text. They composite text over the resolved background without rounding intermediate channels. Fully transparent text has a contrast ratio of 1 and fails every level.
All three methods accept options.backdrop: XColorInput. A translucent background requires a valid, fully opaque backdrop. Without one, isReadable() returns false and the other methods throw. An opaque background ignores this option. Candidate results retain their original opacity; they are not replaced with the composited color. The black/white fallback chooses the higher contrast but does not guarantee the requested AAA threshold.
xcolor('#0000').isReadable('#fff') // false
xcolor('#0008').wcagLevel('#fff8', { backdrop: '#fff' })
xcolor('#0000').mostReadable(['#000', '#fff'], { backdrop: '#fff' }).toHex() // '#000000'You can also composite the layers explicitly before checking:
import xcolor from '@xpyjs/color'
import blend from '@xpyjs/color/plugins/blend'
import a11y from '@xpyjs/color/plugins/a11y'
xcolor.use(blend).use(a11y)
const background = xcolor('#fff').blend('rgb(0 0 255 / 20%)', 'normal')
const foreground = background.blend('rgb(0 0 0 / 80%)', 'normal')
foreground.isReadable(background)Composite from the opaque canvas through the background to the text. Compare the resulting text color against the resulting background.
