寻找给定颜色的补色/相反的颜色

2024-09-30 00:33:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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}$

Tags: 代码消息密码object颜色错误rgblist
1条回答
网友
1楼 · 发布于 2024-09-30 00:33:29

您的join格式需要修复。列表没有join方法,字符串有:

def complementaryColor(my_hex):
    """Returns complementary RGB color

    Example:
    >>>complementaryColor('FFFFFF')
    '000000'
    """
    if my_hex[0] == '#':
        my_hex = my_hex[1:]
    rgb = (my_hex[0:2], my_hex[2:4], my_hex[4:6])
    comp = ['%02X' % (255 - int(a, 16)) for a in rgb]
    return ''.join(comp)

对于两个十六进制字符,十六进制的格式应该是%02X,而不是{}。后者只在3个字符而不是6个字符的损坏输出中追加一个前导02。在


hex是内置函数,因此可以考虑将名称改为,比如my_hex,以避免隐藏原始的hex函数。在

相关问题 更多 >

    热门问题