Help with an equationbrowsing

I didn't know if this fell under share code or questions, as this isn't a question about Python or Pythonista per se.

I know this is a simple equation. I remember doing something like this in highschool. For the life of me I can't think of the solution or how to search for the solution. Essentially I am changing the .background_color of a tableview via scrollview_did_scroll. I want to move from 1.0 to a target value as the scrollview.content_offset[1] increases, when the offset hits 70 I will change to another action. The target value being the respective values in myrgb.

This is a snippet of example code. I know we hate snippets, but the entire project looks rather gross as its still a scratchpad draft.

This sort of transition becomes a lot easier when the value that the output depends on (the y coordinate in your case) moves from 0.0 to 1.0 instead of 0 to 70 or some other number. This is easy to achieve here:

p = 1.0 / 70.0

Because in your case, y can potentially be less than 0 and more than 70, you want to 'clamp' it:

p = max(0.0, min(1.0, y / 70.0))

This ensures that p will always be between 0.0 and 1.0, which makes things a lot easier.

Now you basically just need to multiply your target value (the color) with p and your start value (white) with (1.0 - p), and you're done:

Since your start value is white, you could actually have left out from_color entirely (multiplying by 1.0 doesn't change the outcome), but if you decide later that you want to start from a different color, this makes it easier to change.

Fantastic explanation. Always above and beyond, thank you omz! I was not that far off. With the exception of the from_color, I was missing the + p from what I conjured up before heading to the forums. I had multiplied to_color and the value of (1.0 - p). I also appreciate the use of min and max as I would have had to debug that when I would've caught it when reaching the end of the scrollview height.

@Dann - You are basically doing a type of animation so you might enjoy looking at how @omz implemented it in scene layers. You can look at the source code for how he defined each of the layer animation functions. They each run on an "x" input value from 0.0 to 1.0 and the output value is designed to go from 0.0 to 1.0 but can over and undershoot by something like 0.5. He has logic to "animate" simple values, but also tuples like color. You can view the code using: http://github.com/dgelessus/pythonista-scripts/blob/master/importfinder.py and typing "scene" in the dialog box. Half way through you should see the Animation object and the update and interpolation functions with all the equations above.