Skip to content

Scene: Color arithmetic and accessors, as three.js has them - #232

Closed
kvnloo wants to merge 1 commit into
meshy-dev:mainfrom
kvnloo:swarm/a15-parity-color-ops
Closed

kvnloo wants to merge 1 commit into
meshy-dev:mainfrom
kvnloo:swarm/a15-parity-color-ops

Conversation

@kvnloo

@kvnloo kvnloo commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

three.js's Color has a set of small arithmetic helpers and accessors (r187 Color.js) that packages/scene's Color lacks, so snippets such as color.getHexString(), color.addScalar(0.1) or [...color] throw.

Change: Color in math.ts gains the following, each following the r187 body:

  • isColor
  • setScalar, setFromVector3
  • applyMatrix3 (column-major; it takes any { elements }, since packages/scene has no Matrix3)
  • addColors, addScalar, sub (clamped at 0, as three.js)
  • getHexString, getRGB (linear, three.js's default working space)
  • fromBufferAttribute, toJSON (the sRGB hex)
  • [Symbol.iterator]

These all work on linear components, and three.js's default working space is linear sRGB, so they give the same numbers as three.js.

Not in this PR: offsetHSL and lerpHSL. They build on getHSL/setHSL, which here read HSL as sRGB-encoded, while three.js reads it in the linear working space. Adding them would carry that difference into two more methods.

Test: a new file, packages/scene/test/three-compat-color-ops.test.ts, with three node:test cases that cover each method, including the clamp in sub, addColors ignoring the receiver, zero-padding in getHexString, and JSON.stringify. Because the test is in its own file, it merges independently of other parity PRs.

Tier T (packages/scene TypeScript; no kernel, IR or export changes).

Merge gate

  • npm run typecheck: pass, on main 6e37e8a.
  • npm test: 203 tests, 202 pass, 0 fail, 1 skipped (the physics arena test, which needs a built physics_scene.wasm; main skips it too).
  • Red/green: with main's math.ts, the new file's 3 tests fail (TypeError: c.addColors is not a function, ...). At the head they pass.
  • The same test file, with its import pointed at real three@0.187.0, passes unchanged (3/3).
  • Merges cleanly (git merge-tree) with Scene: MathUtils scalar helpers, as three.js has them #223, which also edits math.ts.
  • npm run test:render: not run, because no GPU was available. No existing method changes; the new ones aren't called by any scene yet.

Drafted with AI assistance.

🤖 Generated with Claude Code

isColor, setScalar, setFromVector3, applyMatrix3, addColors, addScalar,
sub (clamped at 0), getHexString, getRGB, fromBufferAttribute, toJSON and
iteration, following three.js r187 src/math/Color.js.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kvnloo

kvnloo commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Folded into #249 so these review together; closing this one.

@kvnloo kvnloo closed this Nov 24, 2026
doodlewind added a commit that referenced this pull request Nov 24, 2026
… buffers, Euler in all six orders, and more (#256)

One commit for 21 small three.js-parity PRs by Kevin Rajan, each with its own test file:

- #210 Matrix4.scale, getMaxScaleOnAxis and multiplyScalar, as three.js has them
- #211 PerspectiveCamera.getFocalLength, getFilmWidth and getFilmHeight, as three.js has them
- #213 a normalized BufferAttribute's getters and setters work in 0..1, as three.js does
- #215 Euler.setFromQuaternion and setFromRotationMatrix in all six orders, as three.js has them
- #216 Light.dispose and LightShadow.dispose, as three.js has them
- #218 Box3.clampPoint, distanceToPoint, intersectsSphere and getBoundingSphere, as three.js has them
- #219 Matrix4.makeBasis, extractBasis, extractRotation and makeRotationAxis, as three.js has them
- #221 Int8, Uint8, Uint8Clamped, Int16 and Int32 BufferAttribute, as three.js has them
- #222 Quaternion identity, conjugate, length and slerpQuaternions, as three.js has them
- #223 MathUtils scalar helpers, as three.js has them
- #225 MeshPhysicalMaterial.reflectivity, as three.js has it
- #226 Plane, and Ray.intersectPlane / distanceToPlane / intersectsPlane, as three.js has them
- #228 Sphere.set, copy, isEmpty, containsPoint, intersectsSphere, applyMatrix4 and translate, as three.js has them
- #230 Object3D.copy and clone keep the source's rotation order, as three.js does
- #231 InterleavedBuffer and InterleavedBufferAttribute, as three.js has them
- #232 Color arithmetic and accessors, as three.js has them
- #234 Matrix3, Matrix4.setFromMatrix3 and Vector3.applyNormalMatrix, as three.js has them
- #235 Vector3's component-wise arithmetic, as three.js has it
- #236 Layers, Object3D.layers and Raycaster.layers, as three.js has them
- #238 Color sRGB conversions and the CSS color keywords, as three.js has them
- #239 Vector2's component-wise arithmetic, angle and rotateAround, as three.js has them

Co-authored-by: Kevin Rajan <7121943+kvnloo@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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