Skip to main content

Section 26.6 Location Based Conditions Continued

Let’s use location-based decisions to do something more complex. One popular graphic design technique is to make parts of an image black and white, only leaving color in the parts we want to highlight. We will do this to make the graduates’ faces stand out from the background in this image:
First, we need to make some procedures that we will use to make a pixel gray and darken it. These procedures will accept a pixel as their parameter (or one of their parameters) and modify them. Because we are modifying an existing object, there will be no new value to return. We will just update the colors of the pixel and those changes will persist when the procedure is done. (Similar to how when we write a procedure that moves a turtle there is no new answer to return. Changing the turtle’s location or direction in a function permanently changes it.)
Below, the code for makeGray is already written. Your task is to write the code for darken that will be used to make pixels darker:

Checkpoint 26.6.1.

Write code to implement darken. It should reduce all three color values (red, green, blue) of the pixel by the parameter amount.
Now we can use a conditional to apply those only to particular pixels in the image. Start by copy/pasting darken and makeGray from the program above into this program. Then examine the code inside the loop. Notice that we use a conditional to apply the functions only to pixels that have a Y value of 110 or more.
Try modifying the condition so it only leaves a rectangle of color around the students’ heads and turns the rest to gray. You want to say something like “if y is less than 110 or x is less than 70 or x is more than 200”.
You have attempted of activities on this page.