fix(scale): clamp out-of-domain input before applying gamma - #395
Open
apoorva-01 wants to merge 1 commit into
Open
fix(scale): clamp out-of-domain input before applying gamma#395apoorva-01 wants to merge 1 commit into
apoorva-01 wants to merge 1 commit into
Conversation
pow(t, gamma) returns NaN for a negative t with a fractional gamma, so an out-of-domain value threw instead of clamping to the endpoint.
|
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.
Fixes #331.
chroma.scale('YlGn').domain([5,15]).gamma(1.2)(4)throws instead of clamping the out-of-domain value. With a fractional gamma,pow(t, _gamma)runs whiletis still negative (tis -0.1 for input 4), andMath.pow(-0.1, 1.2)is NaN, which falls through to an invalid color. Thelimit(t, 0, 1)a few lines down handles the no-gamma path, but the gamma branch has already produced NaN by then.The fix clamps
tinto [0,1] before the pow, reusing thelimithelper that's already imported. Out-of-domain input now pins to the nearest endpoint, same as it does without a gamma. This matches @regorxxx's fork fix you said you'd look at, just usinglimitinstead of an explicit if/else.One call worth your eye:
limitalso clamps the high side, so an above-max input sits at the padded endpoint rather than the hard edge. With default padding that's identical to before and the full suite stays green, but if you'd rather scope the change strictly to the negative side that broke,pow(t < 0 ? 0 : t, _gamma)does only that.Test in
test/scales.test.jscovers both ends. The below-domain case throws onmainand passes with the fix.