rgb(119, 119, 119)a11y - 无障碍
WCAG (Web Content Accessibility Guidelines) 无障碍工具。判断文本与背景色的可读性,并在多个候选色中找到最佳选择。
安装
import xcolor from '@xpyjs/color'
import a11yPlugin from '@xpyjs/color/plugins/a11y'
xcolor.use(a11yPlugin)rgb(118, 118, 118)达到目标
import { adjustContrast } from '@xpyjs/color/plugins/a11y'
adjustContrast("#777777", "#ffffff", {"target":4.5})方法
adjustContrast(foreground, background, options?)
命名导出,无需注册插件。返回 { color, ratio, target, met, changed },不修改输入。target 默认 4.5,范围 1–21;direction 为 auto(默认)、darker 或 lighter。沿 RGB 到黑/白的路径搜索,auto 在达标方向中选择 RGB 改变量更小者,并非全色域最小感知差解。
import { adjustContrast, auditContrastPairs } from '@xpyjs/color/plugins/a11y'
const adjusted = adjustContrast('#777', '#fff', { target: 4.5 })
adjusted.met // true;ratio 是实际输出颜色的未舍入对比度保留 alpha 和输入实例精度选项;已达标返回独立副本。受透明度、背景或方向限制而无法达到目标时,返回该搜索范围内更好的黑/白端点并设置 met: false,不会把失败标为成功。背景半透明时必须传 backdrop。
auditContrastPairs(colors, pairs, options?)
对扁平主题颜色表逐对检查,pairs 每项为 { foreground: key, background: key, target? }。默认目标 4.5,可全局或逐对覆盖。返回每对的名称、实际 ratio、target 和 passes,不修改主题。
const vars = xcolor.getThemeVars() // 需要先注册 theme 插件
auditContrastPairs(vars, [{ foreground: '--x-primary-text', background: '--x-primary-bg' }])颜色表必须提供具体可解析的值;缺少键、无效颜色、未解析 var() 或没有底色的半透明背景会抛错。检查使用未舍入对比度,结果只表示所选颜色/目标是否满足,不代替字号、字重等完整无障碍检查。
isReadable(other, options?)
判断当前颜色与另一个颜色的组合是否满足 WCAG 可读性标准。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
other | XColorInput | — | 比较目标颜色 |
options.level | 'AA' | 'AAA' | 'AA' | WCAG 等级 |
options.size | 'normal' | 'large' | 'normal' | 文本尺寸 |
options.backdrop | XColorInput | — | 半透明背景背后的不透明底色 |
返回值:boolean
xcolor('#000000').isReadable('#ffffff') // true(对比度 21:1)
xcolor('#000000').isReadable('#ffffff', { level: 'AAA' }) // true
xcolor('#888888').isReadable('#ffffff') // false(正文对比度不足)
xcolor('#888888').isReadable('#ffffff', { size: 'large' }) // true(大字阈值较低)mostReadable(candidates, options?)
在候选颜色列表中,找到与当前颜色对比度最高的颜色;若均未达到阈值,则回退到黑色或白色。
| 参数 | 类型 | 说明 |
|---|---|---|
candidates | XColorInput[] | 候选颜色列表 |
options.level | 'AA' | 'AAA' | WCAG 等级,默认 'AA' |
options.size | 'normal' | 'large' | 文本尺寸,默认 'normal' |
options.backdrop | XColorInput | 当前背景半透明时需要提供的不透明底色 |
返回值:XColor
xcolor('#333333').mostReadable(['#ffffff', '#eeeeee', '#cccccc']).toHex()
// '#ffffff' — 对比度最高wcagLevel(other, options?)
返回两个颜色在正文和大字场景下分别满足的 WCAG 等级。
| 参数 | 类型 | 说明 |
|---|---|---|
other | XColorInput | 比较目标颜色 |
options.backdrop | XColorInput | 半透明背景背后的不透明底色 |
返回值:{ level, largeText, ratio },其中等级为 'AAA' | 'AA' | 'FAIL'。
xcolor('#000000').wcagLevel('#ffffff')
// { level: 'AAA', largeText: 'AAA', ratio: 21 }
xcolor('#666666').wcagLevel('#999999')
// { level: 'FAIL', largeText: 'FAIL', ratio: 2.02 }实际场景
确保文本可读
const bg = xcolor('#1a1a2e')
const textColor = bg.mostReadable(['#ffffff', '#e0e0e0', '#a0a0a0'])
// 自动选择对比度最佳的文本颜色检查设计稿
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})`)
})无效颜色与半透明颜色
isReadable() 遇到无效颜色返回 false。wcagLevel() 遇到无效颜色会抛错。mostReadable() 会跳过无效候选,基色无效时抛错。核心 contrast() 仍返回数值,解析失败时也会使用回退通道计算。
isReadable() 和 wcagLevel() 将当前实例作为文字色,参数作为背景色;mostReadable() 将当前实例作为背景色,候选作为文字色。计算时先将背景合成到底色,再将文字合成到背景,保留中间小数精度。完全透明的文字对比度为 1,不会通过评级。
背景不透明时不需要底色,backdrop 被忽略。背景半透明时必须提供有效且完全不透明的 backdrop;缺失或不满足条件时,isReadable() 返回 false,另外两个方法抛错。候选返回值保留自身透明度,不会返回合成后的颜色。黑白回退仅选择两者中更高的对比度,不保证一定达到指定的 AAA 阈值。
xcolor('#0000').isReadable('#fff') // false
xcolor('#0008').wcagLevel('#fff8', { backdrop: '#fff' })
xcolor('#0000').mostReadable(['#000', '#fff'], { backdrop: '#fff' }).toHex() // '#000000'也可以显式合成后再调用这些方法:
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)先把背景合成到不透明底色,再把文字合成到所得背景。用合成后的文字颜色和背景颜色检查对比度。
