Skip to content

fix(scale): clamp out-of-domain input before applying gamma - #395

Open
apoorva-01 wants to merge 1 commit into
gka:mainfrom
apoorva-01:fix/331-clamp-out-of-domain-gamma
Open

fix(scale): clamp out-of-domain input before applying gamma#395
apoorva-01 wants to merge 1 commit into
gka:mainfrom
apoorva-01:fix/331-clamp-out-of-domain-gamma

Conversation

@apoorva-01

Copy link
Copy Markdown

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 while t is still negative (t is -0.1 for input 4), and Math.pow(-0.1, 1.2) is NaN, which falls through to an invalid color. The limit(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 t into [0,1] before the pow, reusing the limit helper 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 using limit instead of an explicit if/else.

One call worth your eye: limit also 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.js covers both ends. The below-domain case throws on main and passes with the fix.

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.
@apoorva-01
apoorva-01 requested a review from gka as a code owner July 2, 2026 03:12
@changeset-bot

changeset-bot Bot commented Jul 2, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: e6360f6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

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.

Unhandled error with a decimal gamma value and input out of domain

1 participant