General Discussions · Python Codes

Complementary Colors – Python Code

Colors that are opposite to each other on the color wheel are called complementary colors. You can look out on web for more information.


The function below takes a color as a input (hex-value) and returns its complementary color (hex-value).

def get_complementary(color):
    # strip the # from the beginning
    color = color[1:]

    # convert the string into hex
    color = int(color, 16)

    # invert the three bytes
    # as good as substracting each of RGB component by 255(FF)
    comp_color = 0xFFFFFF ^ color

    # convert the color back to hex by prefixing a #
    comp_color = "#%06X" % comp_color

    # return the result
    return comp_color

One thought on “Complementary Colors – Python Code

Let me Know What you Think!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s