Lab 10: Using recursion...

Due 11:59pm Thursday, November 29th, 2012

The goal of this program is to learn about recursion. We'll do this by writing a program that allows you to change the color of portions of an image file. We will be using the graphics library that we have used in previous labs.

To start, run update21 to create the cs21/labs/10 directory containing two starting files called colorfill.py and shapes.gif. The file shapes.gif is the image file that you'll be working with and you'll edit colorfill.py to create your solutions for this lab.

1. Colorfill operation

Most drawing programs have a color fill command which fills a region of the image with a chosen color. It is typically represented by a paint bucket icon:

The user clicks the icon, selects a color to fill with, and then selects an area to fill. The program will then change all the pixels of the same color in the area selected. In our program we will use a text entry box instead of an icon. First enter the name of a color in the text entry box, then select a region to color. The example below shows us changing the color of the Pacman to red:

Before clicking Pacman:

After clicking Pacman:

Notice how some of the edges around the boarder of Pacman are still yellow. This is because near the edges the image is colored in a slightly different shade of yellow and we are doing an exact match on the color of the pixel to see if it should be replaced with the new color.

If the user enters a word that isn't a color we understand, display a warning message to the user on the screen when the user clicks on the image. Be sure to remove the message when the user enters a valid color and clicks on the image again.

Invalid color:

Warning message cleared:

The user can keep clicking areas and changing colors. Every time the user clicks an area, that area is filled with whatever color is in the text entry box. To exit the program, type 'quit' in the text entry box and then click anywhere on the image.

Quitting:


2. Tips and Tricks

We have provided you with colorfill.py which has some code to get you started. The main() function creates your window object win and loads an image for you and gives you the image object im. The image class has two methods of interest to us: getPixel(x,y) and setPixel(x,y, color). The first function, getPixel(x,y), takes an x and y coordinate (Note: it's not a point object, but two integers) and returns a list with three elements [red, green, blue] representing the RGB values of the pixel. The second function, setPixel(x,y,color), sets the pixel at position (x,y) to the given color. Note that for this function, color is a string. You can use the function color_rgb(r,g,b) to convert red, green, and blue values to a string that the setPixel() function can use.

You will fill the area the user selected with a call to colorFill(). You will finish writing this function to create a recursive algorithm to fill an area. An area is defined as a group of pixels that are adjacent to one another and have the same color. When writing this function it is helpful to think of what is the base (non-recursive) case and what is the recursive case. For the base case, we want to stop when the pixel we are looking at is not the original color or when the x or y coordinate are out of the bound of the image. For the recursive case, you're going to want to fill in the pixels of your adjacent neighbors (left, right, up, down) with a recursive call to colorFill().

The user enters their choice of color as a string (e.g., 'blue', 'red', 'yellow') in the text entry box. You will need to convert the color string to the r,g,b values that color represents. There is a file '/usr/local/doc/tclTkColors.txt' which contains a list of colors and their corresponding r,g,b values. For example, the line for bisque looks like the following:

bisque                     255   228   196

You will need to read and parse this file to map colors to r,g,b values. To make the parsing of this file easier, you can only accept input from colors that are one word. In other words, when parsing the file you can ignore any lines that have colors that are made up of two or more words.

Optional: 3. Program enhancements

Note: This is not required and should only be attempted once your colorfill.py program is working correctly. The following are some enhancements you may want to add to your program.

Submit

Once you are satisfied with your programs you can hand them in by typing handin21 in a terminal window.