Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Color converter: the contrast ratio shows `4.50 : 1` while Normal AA says FAIL

已关闭
#235 5 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@bercedev 已经在做这个了。

开始于 2026年10月21日。

  • #245 来自 @bercedev —— 未关闭

评估

这个 Issue 还没有评估数据。

描述

bug good first issue hacktoberfest

In the color converter's WCAG Contrast Checker, set Text / Foreground to #FFFFFF and Surface / Background to #EB1717. The ratio reads 4.50 : 1, and the Normal AA badge (which needs 4.5:1) says FAIL.

Both come from the same number, 4.499327326513352. The FAIL is right and the display isn't: formatRatio (app/(tools)/color-converter/ColorConverterClient.tsx, lines 33 to 35) uses toFixed(2), which rounds it up to 4.50. W3C's Understanding SC 1.4.3 says the computed ratio should not be rounded ("4.499:1 would not meet the 4.5:1 threshold"), so a checker that shows 4.50 next to FAIL leaves people unsure which of the two to believe.

It happens at the other thresholds too:

Text Background Ratio Shown now Expected Threshold it misses
#FFFFFF #EB1717 4.499327326513352 4.50 : 1 4.49 : 1 4.5:1 (Normal AA, Large AAA)
#EE0707 #FFFFFF 4.497858855042646 4.50 : 1 4.49 : 1 4.5:1 (Normal AA, Large AAA)
#575757 #CCCCCC 4.499612814225531 4.50 : 1 4.49 : 1 4.5:1 (Normal AA, Large AAA)
#959595 #FFFFFF 2.9953461357088114 3.00 : 1 2.99 : 1 3:1 (Large AA)
#176363 #FFFFFF 6.996878801802648 7.00 : 1 6.99 : 1 7:1 (Normal AAA)

The badges come from wcagRating (lib/color-utils.ts, line 473), which compares the unrounded ratio, so they're already correct.

What to do

  • Truncate the shown ratio to two decimals instead of rounding it, so it never shows more contrast than the colors have: 4.4993 shows 4.49, and exactly 4.5 still shows 4.50.
  • Watch out for floating point: Math.floor(4.56 * 100) / 100 is 4.55, because 4.56 * 100 is 455.99999999999994. A ratio of exactly 4.56 should still show 4.56.
  • Move the helper to lib/color-utils.ts (for example formatContrastRatio) so it can have unit tests, and use it at line 601.
  • The default pair (#FFFFFF on #0A0B0F, 19.668948449427404) will show 19.66 instead of 19.67. That's expected.
  • Leave wcagRating and the colored bar (lines 609 to 613) as they are.

Where to look

  • app/(tools)/color-converter/ColorConverterClient.tsx: formatRatio (lines 33 to 35) and where it's shown (line 601)
  • lib/color-utils.ts: contrastRatio (line 444) and wcagRating (line 473)
  • lib/__tests__/color-utils.test.ts: the contrastRatio tests (line 142) and the wcagRating tests (line 195)

Acceptance criteria

  • Every row in the table shows the expected ratio, and the PASS/FAIL badges don't change
  • Ratios of 21, 1, 4.5 and 4.56 show 21.00, 1.00, 4.50 and 4.56
  • Unit tests for the helper with the table rows and these values
  • npm run lint, npm run test and npm run build pass

Want to work on this? Comment below and I'll assign it to you. Please take one issue at a time, branch off develop, and open your PR against develop with Closes #235 in the description.

主要语言
TypeScript
星标
1
派生
9
平均合并
7 小时 34 分钟
30 天内合并 PR
146

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

codercops/toolbelt 的其他 Issue

查看 codercops/toolbelt 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。