Midterm Exam 1 Review Fall 2004: Generalized changeColor
Post your answers, questions, comments, comments on answers, suggestions, suggestions on questions, etc.
Back to Fall2004 Midterm 1 Review
Does this work as well? (I mean, it works, but is it as correct as what's below?)
def changeColor(picture,percentage,channel):
show(picture)
percentValue = percentage + 1
for px in getPixels(picture):
if channel == 1:
value = getRed(px)
setRed(px,(value*percentValue))
if channel == 2:
value = getGreen(px)
setGreen(px,(value*percentValue))
if channel == 3:
value = getBlue(px)
setBlue(px,(value*percentValue))
show(picture)
return picture
| See the FAQ on how to post code so that we can read it. Why show, then repaint, then show? Mark Guzdial |
def changeColor(pic,a,z):
for px in getPixels(pic):
red=getRed(px)
green=getGreen(px)
blue=getBlue(px)
if z = 1:
newred=reda
setColor(px,makeColor(newred,green,blue))
if z = 2:
newgreen=greena
setColor(px, makeColor(red,newgreen,blue))
if z = 3:
newblue=bluea
setColor(px, makeColor(red,green,newblue))
why don't the if statements here work in JES?
| Not sure if this is a question, or a suggestion from someone, but try looking up the difference between = and ==. That might help fix your problems. Kelly Lyons |
oh, and the bluea is bluea, and red and green too
blue times a
def changeColor(pic,ch,color):
px=getPixels(pic)
r=getRed(px)
b=getBlue(px)
g=getGreen(px)
if(color==1):
c1=makeColor(rch,b,g)
setColor(px,c1)
if(color==2):
c2=makeColor(r,bch,g)
setColor(px,c2)
if(color==3):
c3=makeColor(r,b,gch)
setColor(px,c3)
show(pic)
I just realized that the color order should be R, G, then B so the B and G should be changed in my code. Right?
how does this statement have a syntax error?
if color=1 and increasedecreaseamt > 0:
def changeColor(picture, a, n):
for px in getPixels(picture):
r = getRed(px)
g = getGreen(px)
b = getBlue(px)
if n == 1:
setRed(px, r a)
if n == 2:
setGreen(px, g a)
if n == 3:
setBlue(px, b a)
show(picture)
this is my code - however, i noticed that when i put .5 in for a, the amount of red decreased by 50 perccent - so how do i tell it that it should be times 1.5?
| Well, if you want it to increase by 50 percent, then your input is 0.5, and you want to multiply by 1.5 What about 60 percent increase? Input = 0.6, multiply by 1.6. 1.5 = (1 + .5) 1.6 = (1 + .6) But then of course wouldn't you need to do something different in the case of a negative input (i.e. color decrease), so then what would you do? Hint, if statement.. Kelly Lyons |
def changeColor2(picture, a, n):
for px in getPixels(picture):
r = getRed(px)
g = getGreen(px)
b = getBlue(px)
if n == 1 and a > 0:
setRed(px, r (1 + a))
if n == 1 and a 0:
setRed(px, r -(a))
if n == 2 and a > 0:
setGreen(px, r (1 + a))
if n == 2 and a 0:
setGreen(px, r -(a))
if n == 3 and a > 0:
setBlue(px, r (1 + a))
if n == 3 and a 0:
setBlue(px, r -(a))
show(picture)
oops - change the rs in n == 2 to gs, and in n==3 to bs
def changeColor (pict, amount, colorValue):
for px in getPixels(pict):
if colorValue==1:
r = getRed(px)
setRed(px,((ramount)+r))
if colorValue==2:
g = getGreen(px)
setGreen(px, ((gamount)+g))
if colorValue==3:
b = getBlue(px)
setBlue(px, ((bamount)+b))
show(pict)
def changeRGB(pic, changeRed, changeGreen, changeBlue):
for px in getPixels(pic):
r = getRed(px)
g = getGreen(px)
b = getBlue(px)
setRed(px, ((rchangeRed)+r))
setGreen(px, ((gchangeGreen)+g))
setBlue(px, ((bchangeBlue)+b))
show(pic)
def changeRGB(pic, changeRed, changeGreen, changeBlue):
for px in getPixels(pic):
r = getRed(px)
g = getGreen(px)
b = getBlue(px)
setRed(px, ((r changeRed)+r))
setGreen(px, ((g changeGreen)+g))
setBlue(px, ((b changeBlue)+b))
show(pic)
sorry, i posted the changeRGB to the work page, ignore that
def changeColor(picture,amount,n):
for p in getPixels(picture):
if (n==1):
setRed(p,getRed(p)+(getRed(p)*amount))
if (n==2):
setGreen(p,getGreen(p)+(getGreen(p)*amount))
if (n==3):
setBlue(p,getBlue(p)+(getBlue(p)*amount))
def changeColor2(picture, a, n):
for px in getPixels(picture):
r = getRed(px)
g = getGreen(px)
b = getBlue(px)
if n == 1 and a > 0:
setRed(px, r (1.0 + a))
if n == 1 and a 0:
setRed(px, r (1.0 - a))
if n == 2 and a > 0:
setGreen(px, g (1.0 + a))
if n == 2 and a 0:
setGreen(px, g (1.0 - a))
if n == 3 and a > 0:
setBlue(px, b (1.0 + a))
if n == 3 and a 0:
setBlue(px, b (1.0 - a))
show(picture)
def changeColor(picture,a,n):
for p in getPixels(picture):
if n==1 and a > 0:
setRed(p, getRed(p)*(1+a))
if n==1 and a <= 0:
setRed(p, getRed(p)*(1-a))
if n==2 and a > 0:
setGreen(p, getGreen(p)*(1+a))
if n==2 and a <= 0:
setGreen(p, getGreen(p)*(1-a))
if n==3 and a >0:
setBlue(p, getBlue(p)*(1+a))
if n==3 and a <= 0:
setBlue(p, getBlue(p)*(1-a))
| If you subtract a negative number, you get a positive number, so 1 - (-.5) is 1.5, and do you really want to multiply your value by 1.5 if you're trying to decrease the amount of color by 50 percent? Kelly Lyons |
def changeColor3(picture, a, n):
for px in getPixels(picture):
r = getRed(px)
g = getGreen(px)
b = getBlue(px)
if n == 1:
setRed(px, r (1.0 + a))
if n == 2:
setGreen(px, g (1.0 + a))
if n == 3:
setBlue(px, b (1.0 + a))
show(picture)
this worked! - i hope it is right - is it?
Link to this Page