Skip to content

fix: return fallback color for malformed hex instead of NaN#274

Open
codergon wants to merge 1 commit into
paper-design:mainfrom
codergon:fix-invalid-hex-color-returns-nan
Open

fix: return fallback color for malformed hex instead of NaN#274
codergon wants to merge 1 commit into
paper-design:mainfrom
codergon:fix-invalid-hex-color-returns-nan

Conversation

@codergon

@codergon codergon commented Jul 5, 2026

Copy link
Copy Markdown

Summary

getShaderColorFromString returns NaN color channels for malformed hex strings instead of falling back to a valid color. Those NaNs get uploaded as GPU color uniforms, which produces broken/undefined rendering for the affected shader.

The function already has a fallback contract for unparseable input — the existing test asserts getShaderColorFromString('not-a-color')[0, 0, 0, 1]. Hex strings just weren't held to it: any hex with invalid characters or an invalid length slipped through to parseInt, yielding NaN.

getShaderColorFromString('#gggggg'); // [NaN, NaN, NaN, 1]
getShaderColorFromString('#12345');  // [0.07, 0.20, 0.01, NaN]
getShaderColorFromString('#ff');     // [1, NaN, NaN, NaN]

Changes

  • Validate hex after expansion. If the normalized value isn't exactly 8 hex digits, return the fallback color ([0, 0, 0, 1]) — matching how other unparseable input is already handled — instead of emitting NaN.
  • Support 4-digit #rgba shorthand. This is valid CSS that the parser previously mangled into NaN (it only expanded 3- and 6-digit forms). Expanding it alongside the 3-digit case also keeps the new validation from wrongly rejecting a valid color.
getShaderColorFromString('#gggggg'); // [0, 0, 0, 1]  ← fallback
getShaderColorFromString('#12345');  // [0, 0, 0, 1]  ← fallback
getShaderColorFromString('#ff');     // [0, 0, 0, 1]  ← fallback
getShaderColorFromString('#ff0c');   // [1, 1, 0, 0.8] ← 4-digit shorthand now supported

Test plan

  • Added cases to get-shader-color-from-string.test.ts:
    • 4-digit hex with alpha (#ff0c[1, 1, 0, 0.8])
    • hex with invalid characters returns fallback (#gggggg)
    • hex with invalid length returns fallback (#12345, #ff)
  • bun test passes (new cases green, no regressions).
  • bun run build and type-check pass; Prettier clean.

@vercel

vercel Bot commented Jul 5, 2026

Copy link
Copy Markdown

@codergon is attempting to deploy a commit to the Paper Design Team on Vercel.

A member of the Team first needs to authorize it.

@codergon codergon force-pushed the fix-invalid-hex-color-returns-nan branch from 0c80340 to c71f4a1 Compare July 6, 2026 01:00
@vercel

vercel Bot commented Jul 6, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
shaders-docs Ready Ready Preview, Comment Jul 6, 2026 5:05pm

Request Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant