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

Spring 2006 Midterm 1 Review: Generalized changeColor

Spring 2006 Midterm 1 Review: Generalized changeColor


would this be an appropriate way to write the program?
def changeColor(picture):
increaseRed(picture)
decreaseRed(picture)
increaseBlue(picture)
decreaseBlue(picture)

def increaseRed(picture):
for p in getPixels(picture):
vlaue=getRed(p)
setRed(pic,.98,"red")

def decreaseRed(picture):
for p in getPixels(picture):
value=getRed(p)
setRed(pic,-.98,"red")

def increaseBlue(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(pic,.97,"blue")

def decreaseBlue(picture):
for p in getPixels(picture):
vlaue=getBlue(p)
setBlue(pic,-.97,"blue")

no, check out what your input values are supposed to be. Amanda Bennett

Does this "an amount to increase or decrease a color by (a number between -.99 and .99)" means that we can take any values in this range or we somehow have to set that range?
Assume that the user knows it can only be an amount between -.99 and .99 Amanda Bennett

does anyone knows if this is the right way to put it up:
def changeColor(picture,x):
for p in getPixels(picture):
redValue=getRed(p)
setRed(p, redValue(1+x))
greenValue=getGreen(p)
setGreen(p, greenValue(1+x))
blueValue=getBlue(p)
setBlue(p, blueValue(1+x))
???????
No, use code tags (there's a BIG note about it at the top of the review). Also, check out your input values. Amanda Bennett

I think they want the function to take in 3 different values. The picture, the color, AND the percentage (in decimal form). This seems to work for me :).

for p in getPixels(pic):
      new = getBlue(p) * (value + 1)
      if new > 255:
        setBlue(p, 255)
      else:
        setBlue(p, new)

where are you defining the function? What inputs are you taking in? This still is not correct. Amanda Bennett

I think they want the function to take in 3 different values. The picture, the color, AND the percentage (in decimal form). This seems to work for me :).

def changeColor(pic, value, color):
  test = value * value
  if value == 0:
    print("Are you TRYING to waste my time? Pick something other than 0!")
  elif value * value >= 1:
    print("Value must be between -.99 and .99")
  elif color == red:
    for p in getPixels(pic):
      new = getRed(p) * (value + 1)
      if new > 255:
        setRed(p, 255)
      else:
        setRed(p, new)
    show(pic)
  elif color == green:
    for p in getPixels(pic):
      new = getGreen(p) * (value + 1)
      if new > 255:
        setGreen(p, 255)
      else:
        setGreen(p, new)
    show(pic)
  elif color == blue:
    for p in getPixels(pic):
      new = getBlue(p) * (value + 1)
      if new > 255:
        setBlue(p, 255)
      else:

        setBlue(p, new)
    show(pic)
  else:
    print("An unknown error has occured.  Please check your inputs.")

Make sure you know what type of input your taking in. If you're taking in a STRING for your color (hint hint), you need to compare it to a STRING. This is very important. Amanda Bennett

so type "changeColor(pic, .5, red)" in python with this code loaded and the program increases all red values to 150% of the original in the picture "pic" with 255 being the largest possible value.
Correct, but what's the code that will do that? Amanda Bennett

i tried to use STRINGS and it seems like it works! yet i am still not sure if this is what is required?


def changeColor(picture,x,color):
color == "red"
for p in getPixels(picture):
new = getRed(p)(x+1)
if color.lower().find("red") >= 0:
setRed(p, new)
color == "green"
for p in getPixels(picture):
new = getGreen(p)(x+1)
if color.lower().find("green") >= 0:
setGreen(p, new)
color == "blue"
for p in getPixels(picture):
new = getBlue(p)(x+1)
if color.lower().find("blue") >= 0:
setBlue(p, new)
first, put CODE tags around your code. it's better to read that way. second, this won't work the way you want it to. When you use the == signs, you're going to want to give it a conditional statement, which you're missing right now. So, you'd want to say IF color == "red", etc. Make sure you then indent properly under this. See where you can go from there. Try writing this code in JES and see what happens and see if it does what you want it to do. Amanda Bennett


def changeColor(picture,x,"color"):
 color="red"
  for p in getPixels(picture):
    value=getRed(p)
    setRed(p,value*x)
 colr="green"
  for p in getPixels(picture):
    value=getGreen(p)
    setGreen(p,value*x)
 color="blue"
  for p in getPixels(picture):
    value=getBlue(p)
    setBlue(p,value*x)

no, you need some sort of if statements in there. Amanda Bennett


I believe this is the right code
def changeColor(picture,value,color):
   for p in getPixels(picture):
     r=getRed(p)
     g=getGreen(p)
     b=getBlue(p)
     if(value>.99 or value<-.99):
       return 0 
     if(color=='red'):
       setRed(p, r*value)
     if(color=='green'):
       setGreen(p, g*value)
     if(color=='blue'):
       setBlue(p, b*value)


Paul
This will work. One thing you could do so the code doesn't check value a bajillion times (since it will never change) is put it before the for loop. This is just help on your coding style, but this will work. Great job. Amanda Bennett
Okay, I'm wrong. See comment below (it was late when i first looked at this). You need to add one to the value taken in to get a value you can use. If you keep it the way you are right now, when you decrease the red, you are making the color negative and then it loops around and is all sorts of wrong. Keeping it positive still decreases the red. Sorry about that. Amanda Bennett


def changeColor(picture,amount,color):
  if (color=='red'):
    for p in getPixels(picture):
      value = getRed(p)
      setRed(p, value * (1.0+amount))
  if (color=='green'):
    for p in getPixels(picture):
      value = getGreen(p)
      setGreen(p, value * (1.0+amount))
  if (color=='blue'):
    for p in getPixels(picture):
      value = getBlue(p)
      setBlue(p, value * (1.0+amount))
  show(picture)
  return picture

this works

So, what do you do if the amount is >255 or <0? Do you want the color to loop around? Amanda Bennett

In Paul's code, if the value is .30 and if the color is 'red', then that means that it will be getRed(p).30
In the code below Paul's, if the value is .30 and if the color is 'red', then that means it will be getRed(p)(1.3)
how is it that this will return the same result even though in the first one the original is increasing by 30 percent while the second one the original red is increasing 130%?
It won't. Thanks. Pauls code needs to add 1 to the value as well...an easier way to think about it (to me at least) is if paul has a negative value as the input "value", then the color actually becomes negative and will loop around and do all sorts of things we don't want it to do. So, thanks for pointing this out :) Amanda Bennett

is this right?

 def changeColor(picture):
  for p in getPixels(picture):
    value=getRed(p)
    setRed(p,value*0.5)
    value=getGreen(p)
    setGreen(p,value*0.5)
    value=getBlue(p)
    setBlue(p,value*0.5)



No. You're dimming each color by 50%. The question asks you to change one color, as determined by the second input by a factor determined by the third input. Your code is equivalent to calling the correct function three times as follows...
changeColor(picture, 0.5, 'red')
changeColor(picture, 0.5, 'green')
changeColor(picture, 0.5, 'blue')
Colin Potts

def changeColor(pic, value, color):
  if value <= -1 or value >= 1:
    print"NEED DECIMAL VALUE"
  for p in getPixels(pic):
    redd=getRed(p)
    greenn=getGreen(p)
    bluee=getBlue(p)
    if(color==red):
      setRed(p, redd+(value*redd))
    if(color==green):
      setGreen(p, greenn+(value*greenn))
    if(color==blue):
      setBlue(p, bluee+(value*bluee))
  show(pic)

how do I make the code instantly stop and print "NEED DECIMAL VALUE"? It does print it but it still returns a modified picture. What command would I use and how would I use it?

The best way would be to use the 'else' command, which we will be covering shortly. Failing that, a common trick in situations like this – although you have to decide whether this is what you want in this situation – is to substitute a legal default value for one that is out of the legal range. So if the user asks for a value less than -1, change value to -1.0. If the value is greater than 1, change it to 1.0. You can do this without using the 'else' command. We did something like that in an earlier lecture example by changing a color value that would have been greater than 255 to 255. Colin Potts

def changeColor (picture, v, color):
  for p in getPixels(picture):
    r = getRed(p)
    g = getGreen(p)
    b = getBlue(p)
  if (color == 'red'):
    new = r*(1+v)
    setRed=(p, new)
    if (new > 255):
      new = 0
    if(new < -255):
      new = 0
  if (color == 'green'):
    new = g*(1+v)
    setGreen=(p, new)
    if (new > 255):
      new = 0
    if(new < -255):
      new = 0
  if (color == 'blue'):
    new = b*(1+v)
    setBlue=(p, new)
    if (new > 255):
      new = 0
    if(new < -255):
      new = 0
  repaint (picture)


I do not see any changes in my picture when I run this. Is It right?

Check your indentation. How much of this code SHOULD be executed inside the loop? How much of it actually is? Colin Potts


Why are you doing the if (new>255): and if(new-255): and where does it indicate that you are increasing or decreasing a color by a number between -.99 and .99?

Is this code right? It keeps returning 0 for me



def changeColor(pic,value,color):
  for p in getPixels(pic):
    r=getRed(p)
    g=getGreen(p)
    b=getBlue(p)
    if(value>.99 or value <.99):
      return 0
    if color == red:
      setRed(p,r*(1.0+value))
    if color == green:
      setGreen(p,g*(1.0+value))
    if color == blue:
      setBlue(p,b*(1.0+value))
  show(pic)



forgot the negative for value <-.99


Sorry guys; you are right about the adding 1 to the input value because we can't have a negative value for r,g or b.
def changeColor(picture,value,color):
   for p in getPixels(picture):
     r=getRed(p)
     g=getGreen(p)
     b=getBlue(p)
     if(value>.99 or value<-.99):
       return 0 
     if(color=='red'):
       setRed(p, r*(value+1))
     if(color=='green'):
       setGreen(p, g*(value+1))
     if(color=='blue'):
       setBlue(p, b*(value+1))

Paul

Sorry guys; you are right about the adding 1 to the input value because we can't have a negative value for r,g or b.
def changeColor(picture,value,color):
   for p in getPixels(picture):
     r=getRed(p)
     g=getGreen(p)
     b=getBlue(p)
     if(value>.99 or value<-.99):
       return 0 
     if(color=='red'):
       setRed(p, r*(value+1))
     if(color=='green'):
       setGreen(p, g*(value+1))
     if(color=='blue'):
       setBlue(p, b*(value+1))

Paul

this is the correct code as given in the review session. BUT, when i try to run it, it says i am trying to access a part of the object that does not exist, and to check line 4, the if color =='red': statement. anybody know why?
def changeColor(picture, amount, color):
  amount = amount + 1
  for p in getPixels(picture):
    if color == 'red':
      r = getRed(p)
      r= r * amount
      setRed(p, r)
    if color =='green':
      g = getGreen(p) * amount
      setGreen(p,g)
    if color =='blue':
      setBlue(p, getBlue(p) * amount)




Link to this Page