compute a gradient with "minimal" colorstops from a ordered set of RGB colors
What you want to do is fit a piecewise linear function to your data.

No noise

If the data is perfect and has no noise, this is easy:


Form a line from the first points.  Remember those two points as the endpoints of the line.
If the third point lies on this line, discard the right endpoint and replace it with the third point.  Otherwise, start a new line from the second point and third point.
Repeat.


That can be done with a simple linear scan.

Noisy data

If the data is imperfect and noisy, you want to do a best fit of a piecewise linear function to your data.

There's no universal answer, as there are multiple ways to fit a piecewise linear function.  If you have $n$ points, you can always get a perfect fit by drawing $n-1$ lines (one line between each consecutive pair of points), but the resulting function might be very wiggly and not what you are looking for.  In practice, we probably have the idea that a solution with few pieces (few lines) is probably more likely than one with more pieces, all being equal.  On the other hand, with too few pieces, you might get a very poor fit.

Thus, you need a parameter to balance the number of pieces against the amount of error in your fit, and then you can try to find an optimal solution.  So, the first step will be to define a loss function that quantifies how good/bad each candidate solution is.  Once you have that, there are various algorithms for finding the best fit.  One approach is to use dynamic programming.  See https://en.wikipedia.org/wiki/Segmented_regression, https://dsp.stackexchange.com/q/1227/5874, https://stackoverflow.com/q/22028529/781723, and probably many other sources.

My guess is that if there is any noise/error in  your data, it will be very small, so any reasonable procedure will work well.