Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 1 Review Spring 2004: Generalized changeColor #2

(Return to Sp2004 Midterm 1 Review)


Amy Howard
def changeColor(picture, amount, number):
    for p in getPixels(picture):
        if number == 1:
            setRed(p, getRed(p)*(amount + 1))
        if number == 2:
            setGreen(p, getGreen(p)*(amount + 1))
        if number == 3:
            setBlue(p, getBlue(p)* (amount + 1))

This code looks pretty good to me! Ashley Coker



Amelia Cipolla
def changeColor(picture, amount, channel)
    for p in getPixels(picture)
       red=getRed(p)
       green=getGreen(p)
       blue=getBlue(p)
         if channel==1:
            setRed(p, red*(1+amount))
         if channel==2:  
            setGreen(p, green*(1+amount))
         if channel==3:
            setBlue(p, blue(1+amount)) 


Is it necessary to use all of the red=getRed(p), etc???
If you don't do red=getRed(p), green=getGreen(p), blue=getBlue(p), then you must change the other times you use the variables "red","green", and "blue". It is just easier to read and understand (and debug) if you do it. But to answer your question, no, it is not necessary. Ashley Coker


def changeColor(picture, value, number):
  for p in getPixels(picture):
    red=getRed(p)
    setRed(p, -.5, 1)
    green=getGreen(p)
    setRed(p, .8, 2)
    blue=getBlue(p)
    setBlue(p, .5, 3)

I know I am missing a lot here, but for starters, what is the best way to tell the computer that R, G, & B equal 1, 2, & 3?

(see answer below) Ashley Coker

Catherine Covington


def changeColor(picture, value, number):
for p in getPixels(picture):
red=getRed(p)
setRed(p, -.5, 1)
green=getGreen(p)
setRed(p, .8, 2)
blue=getBlue(p)
setBlue(p, .5, 3)



Still learning how to enter code onto CoWeb. Same question as above :)
what about some if statements? Like "if number == 1:" Ashley Coker

def changeColor(picture,amount,number):
  for p in getPixels(picture):
    if number == 1:
      setRed(p,getRed(p)*amount)
    if number == 2:
      setGreen(p,getGreen(p)*amount)
    if number == 3:
      setBlue(p,getBlue(p)*amount)

Look at this again. The inputs you will be using are between -.99 and .99. However, if the amount is .50, you want to increase the value by 50%–your code will make the value 50% of its original value. Something needs to be added so that will work correctly, but otherwise you have the right idea... Summer McWilliams


For the code written by Amy Howard, would you need to multiply the r/g/b values by the 'amount' plus 1? Nate Lemoine
Yes you would need to multiply by the 'amount' plus 1. Look at Summer's answer right above your question because she explains it very well there. Ashley Coker

Remember, too, that you want your programs to work for ANY input amounts (between -.99 and .99). If you put in explicitly numbers like .5, then it will only work for those values. Mark Guzdial


def changeColor(picture,amount,number):
  for p in getPixels(picture):
    if number == 1:
      setRed(p,getRed(p)*amount + getRed(p))
    if number == 2:
      setGreen(p,getGreen(p)*amount + getGreen(p))
    if number == 3:
      setBlue(p,getBlue(p)*amount + getBlue(p))


Rae Smith

Rae, your program is actually equivalent to the other ones. Work out the math: getRed(p)*(1+amount) is the same (algebraically) as getRed(p)+(getRed(p)*amount). Mark Guzdial



I was wondering why this won't work??

def changeColor(picture, amount, color):
for p in getPixels(picture):
if color==1:
value=getRed(p)
if color==2:
value=getGreen(p)
if color==3:
value=getBlue(p)
setColor(p, value (amount + 1))

(the == is two equal signs)
(and there is an asterik after value)





I still don't understand why you have to add 1 in this problem and the next
Try working out the math. Mark Guzdial


1 represents 100%; for example, if the amount is .55, and you add 1 to it, it is like increasing the color component to 155% of its current value.
Jalencia Adams

def changeColor(picture, amount, number):
for p in getPixels(picture):
if number == 1:
setRed(p, getRed(p)(amount + 1))
elif number == 2:
setGreen(p, getGreen(p)(amount + 1))
else:
setBlue(p, getBlue(p) (amount + 1))
Rachel King

Why do we use the double equal signs?

Yes, I was wondering that too because some places in the book show double equal signs and others don't. Melanie Nelson

We use single equal signs "=" when we're doing assignments. We do double equal signs "==" when testing to see if one value is equal to another. Typically, you'll use only "==" in IF statements. If you see someplace in the book where we're using single "=" in an IF statement, please let me know! That's a bug! Mark Guzdial


I know some profs/tas have said to check the math, but if you're decreasing the red by 10 %, why do you still add 1 to the new amount? Wouldn't that be increasing the amount by 10%?? Karin Bowman
Because if you want to decrease by 10%, the input would be negative 10% (-.10), so when you add 1, it becomes 0.9, or decreased by 10%. Brienne Thorusen

Like Brienne said. Remember that the input is going to be negative for decreasing and positive for increasing amounts. If you multiply getRed (for example) by a negative value (like -.10), you're going to get a negative value – you can't set red to a negative value. What you really want to do is multiply getRed by a positive value that will give you right result. To decrease by 10%, you actually want to multiply by .90. How do you get to .90 when you have an input of -.10? Add one to the input. That generally works for all the positive/negative values to get the increment/discount. Mark Guzdial


It makes sense that we add 1 to the amount, but will JES think that this refers to the color red since 1 is the number we are inputting to refer to the red value (at least in the first IF statement)? Will JES know that this should be a math operation?


JES doesn't know anything about what the value "1" means. There is no meaning other than what your program does with the values. Mark Guzdial



Link to this Page