# Evaporation II

Last week, we began exploring the piece Evaporation.  In particular, we looked at two aspects of the piece — randomness of both the colors and the sizes of the circles — and experimented with these features in Python.  Look at last week’s post for details! Today, we’ll examine the third significant aspect of the piece — the color gradient.  The piece is a pure sky blue at the top, but becomes more random toward the bottom.  How do we accomplish this?

Essentially, we need to find a way to introduce “0” randomness to each color at the top, and more randomness as we move toward the bottom.  To understand the concept, though, we’ll be introducing 0 randomness at the bottom, and more as we move up.  You’ll see why in a moment.

Let’s first look at a linear gradient.  Imagine that we’re working with a $1\times1$ square — we can always scale later.  Here’s what it looks like: The “linear” part means we’re looking at randomness as a function of $y^1=y.$  So when $y=0,$ we subtract $y=0$ randomness to each color.  But when $y=1/2,$ we subtract a random number between $0$ and $y=1/2$ from each of the RGB values.  Finally, at the very top, we’re subtracting a random number between $0$ and $1$ from each RGB value.  Recall that if an RGB value would ever fall below $0$ as a result of subtraction, we’d simply treat the value as $0.$

Why do we subtract as we go up?  Recall that black has RGB values $(0,0,0),$ so subtracting the randomly generated number pushes the sky blue toward black.  If we added instead, this would push the sky blue toward white.  In fact, you can push the sky blue toward any color you want, but that’s a little too involved for today’s post.

The piece Evaporation was actually produced with a quadratic gradient.  Let’s look at a picture first: That the gradient is quadratic means that the randomness introduced is proportional to $y^2$ for each value of $y.$  In other words, at a level of $y=1/2$ on our square, we subtract a random number between $0$ and $(1/2)^2=1/4.$

You can visually see this as follows.  Look at the gradient of color change from $0$ to $1/2$ for the quadratic gradient.  This is approximately the same color change you see in the linear gradient between $0$ and $1/4.$  Why does this happen?  Because when you square numbers less than $1,$ they get smaller.  So smaller numbers will introduce less randomness in a quadratic gradient than they will in a linear gradient.

We can go the other way, we well.  If we use a quadratic gradient (exponent of $2>1$), the color changes more gradually at the bottom.  But if we use an exponent less than $1$ (such as in taking a root, like a square root or cube root), we get the opposite effect:  color changes more rapidly at the bottom.  This is because taking a root of a number less than $1$ increases the number.  It’s easiest to see this with an example: In this case, the exponent used is $0.4,$ so that for a particular $y$ value, a random number between $0$ and $y^{0.4}$ is subtracted from each RGB value.  Note how quickly the color changes from the sky blue at the bottom toward very dark colors at the top.

Of course this is just one way to vary colors.  But I find it a very interesting application of power and root functions usually learned in precalculus — using computer graphics, we can directly translate an algebraic, functional relationship geometrically into a visual gradient of color.  Another example of why it is a good idea to enlarge your mathematical toolbox — you just never know when something will come in handy!  If I didn’t really understand how power and root functions worked, I wouldn’t have been able to create this visual effect so easily.

Now it’s your turn!  You can visit the Evaporation worksheet to try creating images on your own.  If you’ve been trying out the Python worksheets all along, the code should start to look familiar.  But a few comments are in order.

First, we just looked at a $1\times 1$ square.  It’s actually easier to think in terms of integer variables “width” and “height” (after all, there is no reason our image needs to be square).  In this case, we use “j” as the height parameter, since it is more usual to use variables like “i” and “j” for integers.  So “j/height” would correspond to $y.$  This would produce a color gradient of light to dark form bottom to top.

To make the gradient go from top to bottom, we use “(height-j)/height” instead (see the Python code).  This makes values near the top of the image correspond to $0,$ and values near the bottom of the image correspond to $1.$  I’ll leave it to you to explore all the other details of the Python code.

Please feel free to comment with images you create using the Sage worksheet!

As mentioned in the previous post as well, each parameter you change — each number which appears anywhere in your code — affects the final image.  Some choices seem to give more appealing results than others.  This is where are meets technology.

As a final word — the work on creating fractals is still ongoing.  I’ve learned to make movies now using Processing:

You’ll notice how three copies of one fractal image morph into one of another.  You can find more examples on Twitter: @cre8math.  Once I feel I’ve had enough experience using Processing, I’ll post about how you can use it yourself.  The great thing about Processing is that you can use Python, so all your hard work learning Python will yield even further dividends!

## Published by ### Vince Matsko

Mathematician, educator, consultant, artist, puzzle designer, programmer, blogger, etc., etc. @cre8math

## 2 thoughts on “Evaporation II”

1. corydepinte says:

Just so wonderful to see your work come alive

Like