facelessuser / coloraide

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

Other Spline interpoloations? #222

Closed facelessuser closed 2 years ago

facelessuser commented 2 years ago

Okay, so we implemented B-spline, which is pretty cool. As I've dug deeper, I realize that all methods have strengths and weaknesses.

There are, of course, other approaches as well, such as Catmull-Rom Curves, and other various algorithms. I may dig into some of these for fun and see how they compare to some other approaches.

There is no strong need to implement any or all of these, and definitely no rush. I'd like to take my time and explore and evaluate these. I think, right now, that B-spline provides a nice alternative to piecewise. We'll finish cleaning up the processing of undefined values in a sane way, and I think we'll have a pretty decent alternative. It will, of course, have more overhead than linear, but mainly that is upfront, pre-calculation work.

facelessuser commented 2 years ago

For fun, here is a more natural B-spline: bspline-natural

Though, as seen in the image above, it passes right through the points, but it does have an overshoot. This can be seen below as well. The white tries to transition to yellow but overshoots out of the gamut. Unfortunately, gamut mapping doesn't cope too well in this particular spot, so this creates a bit of a break.

Screen Shot 2022-07-26 at 5 14 43 PM
facelessuser commented 2 years ago

Monotonic spline from SAO/NASA: https://adsabs.harvard.edu/full/1990A%26A...239..443S. Seems surprisingly easy to understand. Should be nice to experiment with.

facelessuser commented 2 years ago

Instead of coercing a b-spline into a natural curve, by recalculating the control points, we can probably do a Catmull-Rom curve. Matrix approach here: http://www2.cs.uregina.ca/~anima/408/Notes/Interpolation/Parameterized-Curves-Summary.htm.

Catrom seems to do just as well.

facelessuser commented 2 years ago

As far as colors go, I can't see a reason not to clamp the colors, or to allow otherwise. The only exception would be if we ever allowed "closed" interpolation, but that would just connect the end back to the beginning.

We were able to get extrapolation to handled in a sane way. Here is Catmull-Rom extrapolating when its boundaries are reached. Ignore the title, it is indeed Catmull-Rom.

catmull-rom-extrapolate

Of course, this would require a user to specifically enable it:

Color.interpolate(['red', 'blue', 'green'], method='catrom', extrapolate=True)

This says nothing as to whether extrapolating such an interpolation is helpful or not, but I wanted to make sure when we allow extrapolation with mixing and interpolating that we could make it work with splines.

facelessuser commented 2 years ago

I'll look into monotonic splines more when I have time. I want to get out what I already have. I need to focus on some other projects.

facelessuser commented 2 years ago

I think the issue is the monotonic spline monotonic to? In all the examples I've seen, that is to the Y axis...but we are dealing with many colors in 3-D (x, y, z) space (or more). Is there a generalized way to do this? Or are we chasing our tail? I'm okay not bothering with monotonic if we cannot generalize an approach, or waiting to see if someone else knows how to do this properly. I think it would be neat to have, but not a necessity to have. To be honest, only linear interpolation is needed, the rest of the spline implementations are nice to have.

facelessuser commented 2 years ago

Closing this one in favor of a monotonic specific issue as we've addressed other splines of interest already.