Add ColorMatrix class and ColorMatrixEffect shader effect#1405
Merged
Conversation
ColorMatrix: - New class extending Matrix3d with chainable color adjustment methods - brightness(), contrast(), saturate(), hueRotate(), sepia(), invertColors() - Zero-allocation: uses Matrix3d.transform() internally (no temp matrices) Matrix3d.transform(): - New method accepting 16 values (full 4x4) or 6 values (2D affine) - Mirrors Matrix2d.transform() API with aligned JSDoc - Used by ColorMatrix for allocation-free matrix composition ColorMatrixEffect: - ShaderEffect that applies a ColorMatrix to any renderable or camera - Exposes chainable color methods that auto-push the GPU uniform - reset(), brightness(), contrast(), saturate(), hueRotate(), sepia(), invertColors(), multiply(), transform() Refactored effects: - SepiaEffect, InvertEffect, DesaturateEffect now extend ColorMatrixEffect - Share a single color matrix shader (mat4 * color) instead of individual GLSL - setIntensity() uses reset() + parent color method (no matrix allocation) Tests: - 23 ColorMatrix tests (all methods, chaining, reset, edge cases) - 15 Matrix3d.transform() tests (6-arg and 16-arg forms) - 10 Matrix2d.transform() tests (aligned coverage) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a reusable color-matrix pipeline to melonJS (math + shader effect) and refactors existing single-purpose WebGL color effects to share it, while expanding matrix transform APIs and test coverage.
Changes:
- Introduces
ColorMatrix(extendsMatrix3d) with chainable color adjustment methods built on allocation-freeMatrix3d.transform(). - Adds
ColorMatrixEffect(ShaderEffect) that uploads amat4uniform and exposes the same chainable color operations + reset/multiply/transform. - Refactors
SepiaEffect,InvertEffect, andDesaturateEffectto extendColorMatrixEffect, and adds/expands tests forMatrix2d.transform()andMatrix3d.transform().
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/melonjs/src/math/matrix3d.ts | Adds transform() overloads (6-arg affine + 16-arg mat4) used for allocation-free composition. |
| packages/melonjs/src/math/matrix2d.ts | Aligns transform() JSDoc with clarified parameter semantics. |
| packages/melonjs/src/math/color_matrix.ts | New ColorMatrix utility (brightness/contrast/saturate/hueRotate/sepia/invertColors). |
| packages/melonjs/src/video/webgl/effects/colorMatrix.js | New ColorMatrixEffect shader effect using a single color-matrix uniform. |
| packages/melonjs/src/video/webgl/effects/sepia.js | Refactors sepia effect to reuse ColorMatrixEffect. |
| packages/melonjs/src/video/webgl/effects/invert.js | Refactors invert effect to reuse ColorMatrixEffect. |
| packages/melonjs/src/video/webgl/effects/desaturate.js | Refactors desaturate effect to reuse ColorMatrixEffect. |
| packages/melonjs/src/index.ts | Exports ColorMatrix and ColorMatrixEffect from the public entrypoint. |
| packages/melonjs/tests/matrix3d_transform.spec.ts | New test suite for both Matrix3d.transform() arities + chaining/edge cases. |
| packages/melonjs/tests/mat2d.spec.ts | Adds coverage for Matrix2d.transform() behavior and equivalence to multiply(). |
| packages/melonjs/tests/colorMatrix.spec.ts | New test suite for ColorMatrix methods, chaining, and reset behavior. |
| packages/melonjs/CHANGELOG.md | Documents new APIs and effect refactor. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Matrix3d.transform(): validate arity (throw on invalid arg count) - Matrix3d.transform(): merge duplicate JSDoc blocks into single block - ColorMatrixEffect.transform(): document both 6-arg and 16-arg forms Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1384 (Phase 1)
ColorMatrix utility class
Matrix3dwith chainable color adjustment methodsbrightness(),contrast(),saturate(),hueRotate(),sepia(),invertColors()Matrix3d.transform()internally — no temporary matrices creatednew ColorMatrix().brightness(1.3).contrast(1.5).saturate(0.8)ColorMatrixEffect shader effect
ColorMatrixto any renderable or camera via the existing shader systemreset()to clear back to identitymultiply()andtransform()for advanced useRefactored effects
SepiaEffect,InvertEffect,DesaturateEffectnow extendColorMatrixEffectmat4 * color) instead of individual GLSL programssetIntensity()usesreset()+ parent color method — no matrix allocationMatrix3d.transform()
Matrix2d.transform()API with aligned JSDocColorMatrixfor allocation-free matrix compositionTest plan
🤖 Generated with Claude Code