Talk:Courses/Computer Science/CPSC 203/CPSC 203 Template/Labs Template/Problem Solving Week 1 - Lab 2

Though the "Loop Example" is taken from "Introduction to Programming with JES by Titus Winters and Josef Spjut (October 6, 2005)", the color.getRGB function did not seem to work in the latest version of JES. So here goes an alternative version of the "Loop Example" which works in JES 3.1:

def colorSwap(pic): # Do the following to every pixel in the picture for pixel in getPixels(pic): # Get the red, green, and blue values of the pixel r = getRed(pixel) g = getGreen(pixel) b = getBlue(pixel) # Make a new color that has swapped (r, g, b) for (g, b, r)       newColor = makeColor(g, b, r)        # Make the pixel the new color pixel.setColor(newColor) newPic = makePicture(pickAFile) colorSwap(newPic) show(newPic)
 * 1) This is how we make our own new commands (called "functions"), using
 * 2) the "def" keyword. Now if we call colorSwap(newPic), it’ll make
 * 3) "pic" another name for newPic, and do everything listed in this
 * 4) new function.
 * 5) NOTE: Indentation (spaces at the beginning of the line) are
 * 6) important here!