我试图用Python找出给定颜色的互补色。这是我的密码。代码返回错误消息,告知“AttributeError:'list'object has no attribute'join'”我需要提示。另外,可能有一个更健壮的代码来计算相反/互补的颜色,这是我基本上正在寻找的。你的建议会有帮助的。在
from PIL import Image
def complementaryColor(hex):
"""Returns complementary RGB color
Example:
>>>complementaryColor('FFFFFF')
'000000'
"""
if hex[0] == '#':
hex = hex[1:]
rgb = (hex[0:2], hex[2:4], hex[4:6])
comp = ['02%X' % (255 - int(a, 16)) for a in rgb]
return comp.join()
另一个类似的函数
^{pr2}$
您的
join
和格式需要修复。列表没有join
方法,字符串有:对于两个十六进制字符,十六进制的格式应该是}。后者只在3个字符而不是6个字符的损坏输出中追加一个前导
%02X
,而不是{02
。在hex
是内置函数,因此可以考虑将名称改为,比如my_hex
,以避免隐藏原始的hex
函数。在相关问题 更多 >
编程相关推荐