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

From wiki.ucalgary.ca
< Talk:Courses/Computer Science/CPSC 203/CPSC 203 Template
Revision as of 01:18, 7 November 2008 by Mhasan (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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:

# This is how we make our own new commands (called "functions"), using 
# the "def" keyword. Now if we call colorSwap(newPic), it’ll make 
# "pic" another name for newPic, and do everything listed in this 
# new function.
# NOTE: Indentation (spaces at the beginning of the line) are 
# important here! 
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)