In this section, we'll look how to use expressions to greatly expand what we can do in a loop. With expressions, we'll be able to write real image manipulation code, and in particular solve some puzzles based on images.
We have seen code that "calls" a function where we pass in a value within the parenthesis, such as the value 42 passed in to the print function below.
Instead of a plain number like 42, an "expression" written in the code like 11 + 13 computes the value to use. For example you could write something like this:
print(11 + 31);
When that line runs, the computer first computes the expression 11 + 31, yielding 42. Then in effect it calls print(42), passing in the computed value. Anywhere in the code where we have used a fixed number like 0 or 255 of whatever, we can instead write an expression, letting the code compute a value when that line runs.
We have not used them until now, but there are three pixel functions that get the red, green or blue value out of a pixel. These will be very handy to use in expressions.
pixel.getRed()-- retrieves the red value from a pixel
pixel.getGreen()-- retrieves the green value
pixel.getBlue()-- retrieves the blue value
Here is an example of getRed() - get the red number out of a pixel, store it in a variable named "red".
red = pixel.getRed();
Notice that calling, say,
pixel.getRed() has the pair of parenthesis () after it . The syntax requires that calling a function includes the parenthesis, even if there's nothing inside the parenthesis. A good example of the inflexible nature of computers.
// Doubles the pixel's red value red = pixel.getRed(); pixel.setRed(red * 2);
pixel.getRed() can be combined with
pixel.setRed(number) to operate on a pixel. For example, the above code snippet doubles the pixel's red value. The first line retrieves the red value from the pixel and stores that value in an variable named red. Say in this case that the red value is 50. The second line computes
red * 2 (100 in this case), and sets that value back into the pixel as its new red value. The net effect is to double the red value of the pixel from 50 to 100.
pixel.setRed(pixel.getRed() * 2);
pixel.getRed() * 2expression, ...
pixel.getRed() * 2 is an expression, which is whatever the old red value was multiplied by 2. This expression is evaluated first, resulting a number such as 240. Then in effect pixel.setRed(240); is called. The setRed() etc. functions automatically limit the value set to the range 0..255. If setRed() is called with a value a greater than 255, it just uses 255, and likewise if a value less than 0 is passed in, it just uses the value 0.
pixel.setRed(pixel.getRed() * 2);
pixel.setRed(pixel.getRed() * 0.5);
pixel.setRed(pixel.getRed() * something);
Before we could only express ideas like "set the red value to 200". Now we can express what new value we want in terms of the old value, like "triple the red value", or "set the red value to 75% of what it was".
pixel.setRed(pixel.getRed() * 5);
In the solution image, you will see some "banding" of the yellow of the banana. This is because the red and green channels were scaled down to a range as small as just 0, 1, 2, .. 12. With so few values, the image only represent a few different shades of yellow, and those are the bands we see if we look carefully at the banana. We will talk about "banding" more when we talk about digital media formats.