我在这个脚本中的某处应该生成一个渐变。当深度等于差值时,梯度是完美的,但是浮点增量似乎会把它搞砸。我一直盯着这段代码很久,我看不到答案。生成梯度时出错
这段代码有什么问题?
def interpolate(s, e, n):
start = list(s)
end = list(e)
incrementlst = []
for i in range(0,len(start)):
diff = int(end[i]) - int(start[i])
if diff == 0:
increment = 0.0
else:
increment =diff/n
incrementlst.append(increment)
return incrementlst
def incrementedValue(s, i, n):
start = list(s)
increment = list(i)
n = n-1
finallst = [0,0,0,0]
if n < 1:
return start
for i in range(0,len(start)):
finallst[i] = start[i] + (n*(increment[i]))
return finallst
def formatIncrementedValue(l):
cmykList = list(l)
formattedString = str(int(round(cmykList[0], 0))) + " " + str(int(round(cmykList[1], 0))) + " " + str(int(round(cmykList[2], 0))) + " " + str(int(round(cmykList[3], 0)))
return formattedString
# Get user inputs.
depth = int(ca_getcustomvalue ("depth", "0"))
start = ca_getcustomvalue("start", "0")
end = ca_getcustomvalue("end", "0")
startlst = start.split(" ")
startlst = [int(i) for i in startlst]
endlst = end.split(" ")
endlst = [int(i) for i in endlst]
# draw a line and incrementally change the pen colour towards the end colour
colorlst = interpolate(startlst, endlst, depth)
for i in range(1,depth-1):
color = formatIncrementedValue(incrementedValue(startlst, colorlst, i))
#Draw line at correct offset in colour "color"
你能提供'depth','start'和'end'值?当你说“_浮点增量似乎把它搞砸了”,你的意思是“出现错误信息”,或者“它运行,但结果看起来不像我期望的那样”?请发布错误信息和/或搞砸的结果图片。 – Kevin
你的缩进被搞砸了。 'interpolate'的所有行都处于错误的缩进级别。 –