Skip to content

FIX: Color.setStyle has parser gaps and silent failures for valid CSS color forms#33197

Open
MITHRAN-BALACHANDER wants to merge 2 commits intomrdoob:devfrom
MITHRAN-BALACHANDER:Color.setStyle
Open

FIX: Color.setStyle has parser gaps and silent failures for valid CSS color forms#33197
MITHRAN-BALACHANDER wants to merge 2 commits intomrdoob:devfrom
MITHRAN-BALACHANDER:Color.setStyle

Conversation

@MITHRAN-BALACHANDER
Copy link
Copy Markdown

Summary

Color.setStyle() silently left colors unchanged for several valid CSS inputs.
This PR fixes the parser gaps, adds case-insensitive model name matching, and adds explicit warnings instead of silent no-ops.

Closes #33195


Problem

Input Before After
rgb(255 0 0) 123456 (no-op) ff0000
rgb(255 0 0 / 0.5) 123456 (no-op) ff0000 + alpha warning ✓
hsl(120deg 100% 50%) 123456 (no-op) 00ff00
rgb(100%,50%,10%,50%) 123456 (no-op) ff801a + alpha warning ✓
RGB(255,0,0) "Unknown color model" warning, no-op ff0000
HSL(120,100%,50%) "Unknown color model" warning, no-op 00ff00
rgb(255,0,oops) silent no-op explicit invalid-components warning ✓

Root causes

1. Separate rigid regexes per token format.
The original parser used two distinct hardcoded regexes for rgb (integer form, percent form) and one for hsl. Any input that did not match both patterns fell through the switch without warning, returning this silently.

2. Case-sensitive switch on the model name.
RGB, RGBA, HSL, HSLA fell into the default branch and emitted "Unknown color model", leaving the color unchanged.

3. No CSS Color 4 space/slash syntax support.
rgb(255 0 0) and rgb(255 0 0 / 0.5) / hsl(120deg 50% 50%) are valid CSS Color 4 forms. The parser had no path for space-separated values or slash-delimited alpha.

4. Percentage alpha was rejected.
rgb(100%,50%,10%,50%) — the alpha 50% failed the original \d*\.?\d+ numeric-only regex.


Changes

src/math/Color.js

  • handleAlpha — refactored to accept a pre-parsed number instead of a raw string, removing redundant parseFloat.
  • parseAlpha (new) — parses an optional alpha token that may be a decimal (0.5) or percentage (50%); returns undefined for absent, null for malformed, and a clamped [0,1] value otherwise.
  • parseFunctionComponents (new) — single entry point for tokenizing the argument list of any CSS color function. Handles:
    • Comma-separated form: 255, 0, 0 / 255, 0, 0, 0.5
    • Space-separated form: 255 0 0
    • Space/slash alpha form: 255 0 0 / 0.5
  • parseRGBValue (new) — converts a single RGB channel token (integer 0–255 or percentage 0–100%) to a [0,1] float.
  • parseHue (new) — parses a hue token that may be a bare number or include the optional deg unit suffix.
  • parsePercent (new) — parses a n% token and returns n/100.
  • Model matchingm[1].toLowerCase() is applied before the switch, making RGB, Rgb, etc. all resolve correctly.
  • Outer regex tightened — changed from /^(\w+)\(([^\)]*)\)/ to /^\s*([A-Za-z]+)\(([^\)]*)\)\s*$/ to anchor the full string and avoid partial matches.
  • Explicit warnings — both rgb/rgba and hsl/hsla branches now emit Color: Invalid color components <style> when the model was recognised but components could not be parsed, eliminating the silent no-op path.

test/unit/src/math/Color.tests.js

Seven new test cases added after the existing setStyle* block:

Test name Input
setStyleRGBSpaceSeparated rgb(255 0 0)
setStyleRGBSpaceSeparatedWithAlpha rgb(255 0 0 / 0.5)
setStyleRGBPercentAlphaPercent rgb(100%,50%,10%,50%)
setStyleHSLSpaceSeparatedWithDegrees hsl(120deg 100% 50%)
setStyleUpperCaseRGBModel RGB(255,0,0)
setStyleUpperCaseHSLModel HSL(120,100%,50%)
setStyleInvalidKnownModelDoesNotApply rgb(255,0,oops) — asserts color is not mutated

Test results

npm run test-unit -- --filter "Color"

TAP version 13
...
ok 48 Maths > Color > setStyleRGBSpaceSeparated
ok 49 Maths > Color > setStyleRGBSpaceSeparatedWithAlpha
ok 50 Maths > Color > setStyleRGBPercentAlphaPercent
ok 57 Maths > Color > setStyleHSLSpaceSeparatedWithDegrees
ok 58 Maths > Color > setStyleUpperCaseRGBModel
ok 59 Maths > Color > setStyleUpperCaseHSLModel
ok 60 Maths > Color > setStyleInvalidKnownModelDoesNotApply
...
1..67
# pass 67
# skip 0
# todo 0
# fail 0

All 67 Color tests pass. No regressions.


Scope / non-goals

  • This PR does not change hsl to accept rad, turn, or grad angle units — only deg (the CSS Color 4 default) is added. Additional unit support can follow in a separate PR.
  • none keyword values (CSS Color 4 missing-component syntax) are not handled — out of scope.
  • Alpha is still ignored at render time; handleAlpha continues to emit the existing warning when alpha < 1.

Files changed

  • src/math/Color.js
  • test/unit/src/math/Color.tests.js

Enhance Color.setStyle to robustly parse modern CSS color function syntaxes: accepts space-separated components, slash-delimited alpha, percent and numeric RGB values, hue with optional 'deg', and case-insensitive model names. Introduces helper parsers (parseAlpha, parseFunctionComponents, parseRGBValue, parseHue, parsePercent), stricter validation/clamping of components, and preserves behavior for alpha warnings. Adds unit tests covering space-separated RGB/HSL, alpha variants, percent inputs, uppercase model names, and invalid-component handling to prevent color mutation.
@github-actions
Copy link
Copy Markdown

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 359.97
85.54
359.97
85.54
+0 B
+0 B
WebGPU 631.34
175.16
631.34
175.16
+0 B
+0 B
WebGPU Nodes 629.46
174.87
629.46
174.87
+0 B
+0 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 492.17
120.12
492.83
120.38
+662 B
+264 B
WebGPU 703.77
190
704.43
190.24
+662 B
+233 B
WebGPU Nodes 653
177.4
653.66
177.63
+662 B
+236 B

@mrdoob
Copy link
Copy Markdown
Owner

mrdoob commented Mar 17, 2026

Nice!

Did you use any AI for this?

@MITHRAN-BALACHANDER
Copy link
Copy Markdown
Author

Yes, I used AI only to help refine the documentation and wording.

The issue identification, debugging, refactoring, and implementation were done by me, and I added the unit tests to ensure correctness and avoid regressions.

@MITHRAN-BALACHANDER MITHRAN-BALACHANDER marked this pull request as draft March 17, 2026 12:06
@MITHRAN-BALACHANDER MITHRAN-BALACHANDER marked this pull request as ready for review March 17, 2026 12:06
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.

Bug: Color.setStyle has parser gaps and silent failures for valid CSS color forms

2 participants