The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(shared): normalize invalid ensureContrast step values before the ladder loop
Konstantin Khlopkov1da0c50c9821
diff --git a/apps/shared/src/color.test.ts b/apps/shared/src/color.test.tsindex 98d4ada19f3..78dc2821692 100644--- a/apps/shared/src/color.test.ts+++ b/apps/shared/src/color.test.ts@@ -79,4 +79,37 @@ describe('contrast', () => {expect(ensureContrast('#3D2F13', '#ffffff', 3.9)).toBe('#3D2F13')expect(ensureContrast('ansi256(245)', '#ffffff', 3.9)).toBe('ansi256(245)')})++ it.each([+ ['zero', 0],+ ['negative', -0.2],+ ['NaN', Number.NaN],+ ['Infinity', Number.POSITIVE_INFINITY],+ ['denormal', Number.MIN_VALUE]+ ])('ensureContrast normalizes a %s step to the default ladder and still clears the ratio', (_label, badStep) => {+ const fixed = ensureContrast('#777777', '#ffffff', 4.5, badStep as number)++ expect(fixed).toBe(ensureContrast('#777777', '#ffffff', 4.5))+ expect(contrastRatio(fixed, '#ffffff')!).toBeGreaterThanOrEqual(4.5)+ })++ it('ensureContrast clamps an above-1 step to a single full blend toward the pole', () => {+ expect(ensureContrast('#777777', '#ffffff', 4.5, 25)).toBe('#000000')+ expect(contrastRatio(ensureContrast('#777777', '#ffffff', 4.5, 25), '#ffffff')!).toBeGreaterThanOrEqual(4.5)+ })++ it('ensureContrast accepts a sub-default rung down to 0.001 without renormalizing', () => {+ const fixed = ensureContrast('#777777', '#ffffff', 4.5, 0.001)++ expect(fixed).not.toBe(ensureContrast('#777777', '#ffffff', 4.5))+ expect(contrastRatio(fixed, '#ffffff')!).toBeGreaterThanOrEqual(4.5)+ })++ it('ensureContrast keeps the documented ladders byte-identical', () => {+ // Desktop default rungs and the TUI's 0.05 chain rely on the exact float+ // sequence of the accumulating loop; normalization must not touch them.+ expect(ensureContrast('#cba6f7', '#ffffff', 7)).toBe('#514263')+ expect(ensureContrast('#cba6f7', '#ffffff', 7, 0.2)).toBe('#514263')+ expect(ensureContrast('#3a3a5c', '#101014', 4.5, 0.05)).toBe('#7f7f95')+ })})diff --git a/apps/shared/src/color.ts b/apps/shared/src/color.tsindex b532799a3af..e4c9f4e6abb 100644--- a/apps/shared/src/color.ts+++ b/apps/shared/src/color.ts@@ -135,6 +135,12 @@ export function readableOn(bg: string, inks: readonly [string, ...string[]] = DE* `#3f70d8`). The TUI's chainable form opts into 0.05 for less hue loss.* The accumulating loop (rather than `i * step`) is deliberate — it is the* exact float sequence the old desktop ladder produced.+ *+ * Invalid rungs are normalized instead of trusted: a finite `step` in+ * (0.001, 1] is used as-is (clamped to 1 above that), everything else —+ * non-finite, non-positive, or too small to reach the bound in a reasonable+ * number of rungs (including denormals like `Number.MIN_VALUE`) — falls back+ * to the default ladder. The helper terminates for every JavaScript number.*/export function ensureContrast(color: string, bg: string, min: number, step = 0.2): string {const bgLuminance = relativeLuminance(bg)@@ -152,7 +158,9 @@ export function ensureContrast(color: string, bg: string, min: number, step = 0.const pole = bgLuminance < 0.5 ? '#ffffff' : '#000000'let best = color- for (let amount = step; amount <= 1.0001; amount += step) {+ const rung = Number.isFinite(step) && step >= 0.001 ? Math.min(step, 1) : 0.2++ for (let amount = rung; amount <= 1.0001; amount += rung) {best = mix(color, pole, Math.min(amount, 1))const stepRatio = contrastRatio(best, bg)