Help with an equation
Dann239 last edited by
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
scrollview_did_scroll. I want to move from 1.0 to a target value as the
scrollview.content_offsetincreases, when the offset hits 70 I will change to another action. The target value being the respective values in
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.
def scrollview_did_scroll(self, scrollview): x, y = scrollview.content_offset myrgb = (26.0/256, 188.0/256, 156.0/256) r = g = b = #At y's origin, r, g, b will be (1.0, 1.0, 1.0) scrollview.background_color = (r, g, b)
omz last edited by
This sort of transition becomes a lot easier when the value that the output depends on (the
ycoordinate 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,
ycan 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
pwill 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
pand your start value (white) with
(1.0 - p), and you're done:
from_color = (1.0, 1.0, 1.0) to_color = (26.0/256, 188.0/256, 156.0/256) p = max(0.0, min(1.0, y / 70.0)) r = (1.0 - p) * from_color + p * to_color g = (1.0 - p) * from_color + p * to_color b = (1.0 - p) * from_color + p * to_color
Since your start value is white, you could actually have left out
from_colorentirely (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.
Dann239 last edited by
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
+ pfrom what I conjured up before heading to the forums. I had multiplied
to_colorand the value of
(1.0 - p). I also appreciate the use of
maxas I would have had to debug that when I would've caught it when reaching the end of the
wradcliffe last edited by
@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.