facelessuser / coloraide

A library to aid in using colors
https://facelessuser.github.io/coloraide
MIT License
194 stars 12 forks source link

Adjustment to spline interpolation and extrapolation #235

Closed facelessuser closed 2 years ago

facelessuser commented 2 years ago

When extrapolating past the end of a clamped spline, the intention is for the interpolation to continue, but in a linear fashion. While this currently works, sort of, the results are somewhat stunted. This is because we are still using the spline logic, but forcing the interpolation by stacking multiple endpoints. This is great for guiding a spline through its endpoint, but not necessarily to extend it past the end.

We can see here our stunted extrapolation:

bad_extrapolate

But the fix is simple. For any initial t (not counting what happens after an easing function) that is beyond the [0, 1] range, we can simply use linear interpolation between the last 2 control points. This will extend the line in a linear fashion. And due to the way the endpoints are clamped, this should have the slopes matching just fine.

good-extrapolate

facelessuser commented 2 years ago

Correction, even in linear, the extrapolation is considered an extension of the first or last piece in a piecewise interpolation, so yes, even an easing function that extends past the entire interpolation range on the first or last segment will trigger linear interpolation when working with splines. This is less jarring than having the end mirror in the last segment and then turn into linear interpolation when absolute t is outside that segment.

I had forgotten we had that check in the code already.