Python from color name to RGB

为君一笑 提交于 2020-08-19 06:26:02

问题


If I have a function which takes color as an input to be edited first (by RGB numbers), and then used in matplotlib.pyplot. How can I convert color name to RGB?

For example:

def function(color):
     color[3] = 0.5
     plt.plot([1,2],[2,4], color = color)

then function((0,0,1,1)) works, but function('blue') will only work on the plt.plot.

How can I convert color name to RGB, (such as blue to (0,0,1,1))?

Thanks.


回答1:


You can use with matplotlib.colors

from matplotlib import colors

print(colors.to_rgba('blue'))

Result:

(0.0, 0.0, 1.0, 1.0)



回答2:


Found the solution, by

from matplotlib import colors
orange_rgb = colors.hex2color(colors.cnames['orange'])


来源:https://stackoverflow.com/questions/51350872/python-from-color-name-to-rgb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!