2017-04-26 162 views
-1
def findClosestColor(newColor, colorList): 

    """Takes in a Color object and a list of Color objects, and it finds 
    which color in the list is most similar to newColor. It returns the 
    most similar color from the list.""" 

    currClosest = colorList[0] 
    currDist = distance(newColor, currClosest) 
    for col in colorList: 
     nextDist = distance(newColor, col) 
     if nextDist > currDist: 
      currClosest = col 
      currDist = nextDist 
    return currClosest 


colors1 = [red, green, blue, yellow, pink, white, black] 
c1 = findClosestColor(makeColor(240, 15, 30), colors1) 

print(c1) 

我得到功能不工作,因为它应该

(R = 0,G = 255,B = 0)而不是(R = 255,G = 0,B = 0 )。

回答

0

这条线是不正确的:

if nextDist > currDist: 

,你必须把它翻转到小于:

if nextDist < currDist: 
相关问题