Repository navigation
Keep achromatic HSL gradients finite - #132
Open
sylvesterkaczmarek wants to merge 2 commits into
Open
sylvesterkaczmarek wants to merge 2 commits into
sylvesterkaczmarek wants to merge 2 commits into
Conversation
claudiofantacci
requested changes
Oct 18, 2026
| import numpy as np | ||
|
|
||
|
|
||
| class HslGradientsTest(parameterized.TestCase): |
Collaborator
There was a problem hiding this comment.
Please move these tests under color_conversion_test.py. We already have a color transformation test, so you can put them there, thanks!
Contributor
Author
There was a problem hiding this comment.
Moved the tests into color_conversion_test.py. All 9 new cases and 20 adjacent tests pass on current master.
| np.testing.assert_array_equal(grey(x), np.array([0.0, 0.0, level])) | ||
| np.testing.assert_allclose(reverse(x), [0.0, 0.0, 1.0], atol=1e-3) | ||
| np.testing.assert_allclose(forward(x), [0.0, 0.0, 1.0], atol=1e-3) | ||
| jacobian = jax.jacrev(color_conversion.rgb_to_hsl)(jnp.repeat(x[None], 3)) |
Collaborator
There was a problem hiding this comment.
Pre-prepare this outside the loop, so we can clearly see an arrange, act, and assert pattern in each test. Check this also for other tests too, thanks!
Contributor
Author
There was a problem hiding this comment.
The RGB Jacobian and its JIT wrapper are now prepared once before the greyscale-level loop.
Signed-off-by: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com>
sylvesterkaczmarek
force-pushed
the
fix/achromatic-hsl-gradients
branch
from
October 19, 2026 15:50
378e83c to
52a5c62
Compare
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.
Problem
rgb_to_hslmasks achromatic hue/saturation outputs after dividing by the zero colour range. The forward value for a grey pixel is correct, but reverse differentiation produces NaNs, including in the lightness component. Low-precision black/white pixels also need a safe denominator for the inactive saturation branch.Change
Replace inactive denominators before division, preserving the existing achromatic output branches and chromatic formula. This follows the JAX guidance on masked undefined operations.
Validation
dm_pix/_src/color_conversion_test.py. Unchanged production code fails eight cases and passes the chromatic finite-difference control.colorsysfinite differences.git diff --checkpass on the current head.Hue is mathematically undefined on the achromatic axis. The tests preserve the existing constant hue/saturation branches and verify the lightness derivative along the greyscale path; they do not assert a unique full colour-space derivative there.
Python 3.12.14 on macOS arm64 CPU. GPU/TPU and the complete image-processing suite were not run. Channel-axis handling in #125 is separate and unchanged. No epsilon, colour convention, dependency, or public API changes.
Reproduced against
ffb65074b39b67e1a686d1348f2d461c7ff3ab45.