Skip to content

Add ColorMatrix class and ColorMatrixEffect shader effect#1405

Merged
obiot merged 2 commits intomasterfrom
feat/color-matrix-effect
Apr 19, 2026
Merged

Add ColorMatrix class and ColorMatrixEffect shader effect#1405
obiot merged 2 commits intomasterfrom
feat/color-matrix-effect

Conversation

@obiot
Copy link
Copy Markdown
Member

@obiot obiot commented Apr 19, 2026

Summary

Closes #1384 (Phase 1)

ColorMatrix utility class

  • Extends Matrix3d with chainable color adjustment methods
  • brightness(), contrast(), saturate(), hueRotate(), sepia(), invertColors()
  • Zero-allocation: uses Matrix3d.transform() internally — no temporary matrices created
  • Composable: new ColorMatrix().brightness(1.3).contrast(1.5).saturate(0.8)

ColorMatrixEffect shader effect

  • Applies a ColorMatrix to any renderable or camera via the existing shader system
  • Exposes the same chainable color methods that auto-push the GPU uniform
  • reset() to clear back to identity
  • multiply() and transform() for advanced use
// simple usage
sprite.shader = new ColorMatrixEffect(renderer).saturate(0.0);

// combined transforms
camera.shader = new ColorMatrixEffect(renderer)
    .brightness(1.3)
    .contrast(1.5)
    .sepia(0.3);

// update dynamically
effect.reset().hueRotate(angle);

Refactored effects

  • SepiaEffect, InvertEffect, DesaturateEffect now extend ColorMatrixEffect
  • Share a single color matrix shader (mat4 * color) instead of individual GLSL programs
  • setIntensity() uses reset() + parent color method — no matrix allocation

Matrix3d.transform()

  • New method accepting 16 values (full 4x4) or 6 values (2D affine, promoted to 4x4)
  • Mirrors Matrix2d.transform() API with aligned JSDoc
  • Used by ColorMatrix for allocation-free matrix composition

Test plan

  • 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)
  • 2597 total tests pass
  • Existing SepiaEffect/InvertEffect/DesaturateEffect behavior unchanged
  • ColorMatrixEffect works on sprites and cameras

🤖 Generated with Claude Code

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>
Copilot AI review requested due to automatic review settings April 19, 2026 02:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 (extends Matrix3d) with chainable color adjustment methods built on allocation-free Matrix3d.transform().
  • Adds ColorMatrixEffect (ShaderEffect) that uploads a mat4 uniform and exposes the same chainable color operations + reset/multiply/transform.
  • Refactors SepiaEffect, InvertEffect, and DesaturateEffect to extend ColorMatrixEffect, and adds/expands tests for Matrix2d.transform() and Matrix3d.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.

Comment thread packages/melonjs/src/math/matrix3d.ts Outdated
Comment thread packages/melonjs/src/video/webgl/effects/colorMatrix.js
Comment thread packages/melonjs/src/math/matrix3d.ts Outdated
- 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>
@obiot obiot merged commit d07bf3d into master Apr 19, 2026
6 checks passed
@obiot obiot deleted the feat/color-matrix-effect branch April 19, 2026 06:19
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.

Add ColorMatrix ShaderEffect for global color transforms

2 participants