Python- Is there a function or formula to find the complementary colour of a rgb code?

后端 未结 3 451
感情败类
感情败类 2021-01-05 15:08

I have tried to find a good formula in Python 3 to calculate the complementary colour of a rgb code eg. complementary of a = b. Is there any way to do this?

3条回答
  •  清歌不尽
    2021-01-05 15:33

    r,g,b = [25,25,25]
    
    def get_complementary(color):
        color = color[1:]
        color = int(color, 16)
        comp_color = 0xFFFFFF ^ color
        comp_color = "#%06X" % comp_color
        return comp_color
    hex_val = "#%02x%02x%02x" % (r,g,b)
    h = get_complementary(hex_val)
    print("Complementary color",h)
    h = h.lstrip('#')
    print('RGB =', tuple(int(h[i:i+2], 16) for i in (0, 2, 4)))
    

    output:

    Complementary color #E6E6E6

    RGB = (230, 230, 230)

提交回复
热议问题