14.5. Location based Conditions

We can also use conditionals and the x and y coordinates of pixels to make decsisions about what pixels to process. One popular graphic desgin technique is to make parts of an image black and white and only leave color in 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 to 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 move a turtle there is no new answer to return. Changing the turtle’s location or direction in a function permanantly 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:

Write code to implement darken. It should reduce all three color values (red, green, blue) of the pixel by the parameter amount. But, if a color would end up with A value below 0, set it to 0 instead.

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 have attempted of activities on this page