Fall 2006 Midterm 1 Review: Generalized changeColor
Questions?
Do we have to write four different functoins, all of them in one function or should one function do everything? The directions are too clear on if it should be one or multiple.
Brittany Walsh
| Brittany, it should be one function that does it all. Stacy, where did you define the variable amount at? Amanda Bennett |
def changeColor(pic):
for p in getPixels(pic):
redvalue=getRed(p)
setRed(p, redvalue*amount)
greenvalue=getGreen(p)
setGreen(p, greenvalue*amount)
bluevalue=getBlue(p)
setBlue(p, bluevalue*amount)
show(pic)
return pic
Stacy Schwaiger
So, it should be {changeRed} instead of {decreaseRed} or {increaseRed} basically so that it can be reusable.....
| One thing, Stacy, where are you going to define by what amount you change the different values? Look at the examples of what the code should be able to do, and what inputs the examples are using. Toni Walden | |
Where it says amount is where you would put in the -10, .30, and 0...I thought I was just going to do it so that it can be decreased or increased.......I don't think I understand you too much :)
Stacy Schwaiger
| so, maybe you want to have "amount" as an input value? You have to define every variable you use in your code! Amanda Bennett |
when I try to put 'red' into my code it says I have an error, how can I use these "''" around my colors?
def changeColor(pic, amount, color):
if(amount <> 0):
if(color == red):
for p in getPixels(pic):
setRed(p, getRed(p)*amount)
if(color == green):
for p in getPixels(pic):
setGreen(p, getGreen(p)*amount)
if(color == blue):
for p in getPixels(pic):
setBlue(p, getBlue(p)*amount)
if(amount == 0):
if(color == red):
for p in getPixels(pic):
setRed(p, getRed(p)*1)
if(color == green):
for p in getPixels(pic):
setGreen(p, getGreen(p)*1)
if(color == blue):
for p in getPixels(pic):
setBlue(p, getBlue(p)*1)
show(pic)
return pic
I'm sure it can be shorter but it seems to work well for me.
def changeColor(pic, amount,color):
for p in getPixels(pic):
redvalue=getRed(p)
setRed(p, redvalue*amount)
greenvalue=getGreen(p)
setGreen(p, greenvalue*amount)
bluevalue=getBlue(p)
setBlue(p, bluevalue*amount)
show(pic)
return pic
Stacy Schwaiger
Is this correct now?
| Better, but you are adjusting every color by the same amount. So this function will lighten or darken the image, not change its color balance. You need to adjust only the color specified in the 'color' input. How can you tell if (hint, hint) it is the right color to change? Colin Potts |
def changeColor(pic, amount, color):
for p in getPixels(pic):
if color == "red":
r = getRed(p)
setRed (p, r * amount)
if color == “green”:
g = getGreen (p)
setGreen (p, g * amount)
if color == “blue”:
b = getBlue (p)
setBlue (p, b* amount)
show(pic)
return pic
Is this Correct?
| Almost! You're adjusting the right color, but are you adjusting it correctly? Read the question again to check what 'amount' really means. Colin Potts |
def changeColor(pic, amount, color):
for p in getPixels(pic):
if color == "red":
r = getRed(p)
setRed (p, r + (r * amount))
if color == “green”:
g = getGreen (p)
setGreen (p, g + (g * amount))
if color == “blue”:
b = getBlue (p)
setBlue (p, b + (b* amount))
show(pic)
return pic
To pick up where Stacy left off...is this adjusting the colors correctly? Is the above code correct?
Megan Bowen
why is it "r + (r amount" ?
Let's say you wanted to increase the red in the picture by 50%; if you used "r*amount"
it would end up setting the new red to half of its original value instead
of increasing it Toni Walden |
Why do you have to add green to the (red or) green (or blue) times some amount? why can't you just put (g amt))?
would this work as well?
def changeColor(pic,amtChange,color):
for p in getPixels(pic):
if color=="red":
r=getRed(p)
setRed(p,(1+amtChange)*r)
if color=="blue":
b=getBlue(p)
setBlue(p,(1+amtChange)*b)
if color=="green":
g=getGreen(p)
setGreen(p,(1+amtChange)*g)
show(pic)
charles blackmon
| Yes, that would work as well. :) It's just the distributive property, so yes, it works :) Sweta Vajjhala |
You have to do:
setRed(p, redvalue+(redvalue x amount)) instead of just amount b/c if you have red as a value of 100 and you want to increase it by 10% you must multiply 100 by .1 and then add it to the original amount to get the correct increase amount...hope this helps in some way. Stacy Schwaiger
why would you add the one to amtChange?
Link to this Page