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.

color-wheel

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

4 thoughts on “Complementary Colors – Python Code

  1. Hi, thanks for publishing this “Complementary Colors – Python Code”; are you proving it under an Open Source license such as the MIT license?

    Like

Let me Know What you Think!