Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Help with an equation
-
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 atableview
viascrollview_did_scroll
. I want to move from 1.0 to a target value as thescrollview.content_offset[1]
increases, when the offset hits 70 I will change to another action. The target value being the respective values inmyrgb
.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)
-
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: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[0] + p * to_color[0] g = (1.0 - p) * from_color[1] + p * to_color[1] b = (1.0 - p) * from_color[2] + p * to_color[2]
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 multipliedto_color
and the value of(1.0 - p)
. I also appreciate the use ofmin
andmax
as I would have had to debug that when I would've caught it when reaching the end of thescrollview
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.