0

我在这个脚本的某个地方有一个错误,应该生成一个渐变。当深度等于差值时,梯度是完美的,但浮点增量似乎把它搞砸了。我一直盯着这段代码很久了,我看不到答案。

这段代码有什么问题?

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"
4

1 回答 1

2

这个:

increment =diff/n

正在做整数除法,例如如果diff是 3 和n2,你得到 1,而不是 1.5。

使表达式浮动,以解决此问题:

increment = float(diff) / n
于 2013-08-21T14:33:37.087 回答