finding out complementary/opposite color of a given color

python

Solution

Your `join` and formatting needed a fix. Lists do not have a `join` method, strings do:

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)

The formatting for hex shoud be `%02X` for two hex characters and not `'02%X'`. The later only appends a leading `02` to a mangled output of 3 characters instead of 6.

`hex` is builtin function, so you may consider changing the name to, say `my_hex` to avoid shadowing the original `hex` function.

Problem

I am trying to find out the complementary color of a given color using Python. here is my code. the code returns error message telling "AttributeError: 'list' object has no attribute 'join'" I need a hint. In addition, there might be a more robust code which calculates the opposite/complementary color, which I am basically looking for. your suggestions will be helpful. ``` 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() ``` another similar function ``` def blackwhite(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 = ['%X' % (0 if (15 - int(a, 16)) <= 7 else 15) for a in rgb] return ''.join(comp) print blackwhite('#36190D') ```

Original source