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

后端 未结 3 446
感情败类
感情败类 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:29

    I don't think there is ready solution for this, but there is a colorsys module in standard library, it can help.

    I think you first need to convert RGB into HSV or HSL, then "rotate" hue, and convert back to RGB, if you need. For example (I'm not sure about proper rotating):

    from colorsys import rgb_to_hsv, hsv_to_rgb
    
    def complementary(r, g, b):
       """returns RGB components of complementary color"""
       hsv = rgb_to_hsv(r, g, b)
       return hsv_to_rgb((hsv[0] + 0.5) % 1, hsv[1], hsv[2])
    

提交回复
热议问题