can someone show me how to use i am trying but i doesnt wowork!
you see i lost it again! i am trying to preserve the original format of the program. so can someone explain it better to me!
thanks
| At the top of the midterm review page, it says this: If you post code, please surround your code with <code> and </code> tags so that indentions will be preserved! This should help you. Liz Helms |
Also remember that if you want an asterisk to show up on the coweb (like the ones you were using for multiplication) you need to use ☆ instead of just typing *. Otherwise, the CoWeb thinks you're trying to make a link to a page. As far as your actual code goes, I'd plug in some sample numbers for colors and see what happens to them. Your code probably doesn't do exactly what you would think it would. Here's your original code formatted all pretty and stuff...def changeRGB(picture,r,g,b): for p in getPixels(picture): setRed(p, getRed(p)*r) setGreen(p, getGreen(p)*g) setBlue(p, getBlue(p)*b)
|
This one works
def changeRGB(picture,redValue,greenValue,blueValue):
for p in getPixels(picture):
r=getRed(p)
g=getGreen(p)
b=getBlue(p)
if(redValue<-.99 or redValue>.99):
return 0
if(greenValue<-.99 or greenValue>.99):
return 0
if(blueValue<-.99 or blueValue>.99):
return 0
newColor=makeColor(r*redValue,g*greenValue,b*blueValue)
setColor(p,newColor)
Paul
| Add 1 to your values or you'll get odd results. If you multiply by a negative number, you're going to make Jes roll over the color, if your redvalue input is .75 you want to increase the color by 75%, right now you'll be decreasing the red by 25% in actuality. Amanda Bennett |
def changeRGB(picture,rvalue, gvalue, bvalue):
for p in getPixels(picture):
r=getRed(p)
g=getGreen(p)
b=getBlue(p)
if rvalue > .99 or rvalue <-.99:
return 0
if gvalue > .99 or gvalue <-.99:
return 0
if bvalue > .99 or bvalue <-.99:
return 0
setRed(p, r*rvalue)
setGreen(p, g*gvalue)
setBlue(p, b*bvalue)
show(picture)
repaint(picture)
That has a different outcome compared to Paul's code, which one is correct?
| I don't know why these would look different in the end. From my quick look over they appear that they should look the same. But, neither of them is correct (for the same reason as the last question). The values taken in (rvalue, gvalue, bvalue) need to have 1 added to them, otherwise you're going to get very odd results. Amanda Bennett |
What does the "return 0" do in the function?
| This is intended as a sneaky way to exit from the function before changing the colors to what would be invalid values. But there are better ways to do this. You could test whether the values are valid (i.e. all are between -.99 and .99) before doing the loop. That way, the picture would only be changed inside an 'if' block. If any of the values were out of the -0.99..0.99 range, the loop would never be entered. Colin Potts |
Link to this Page