user9885031
user9885031

Reputation:

Python from color name to RGB

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))?

Upvotes: 27

Views: 26083

Answers (2)

Yakir Tsuberi
Yakir Tsuberi

Reputation: 1503

You can use with matplotlib.colors

from matplotlib import colors

print(colors.to_rgba('blue'))

Result:

(0.0, 0.0, 1.0, 1.0)

Upvotes: 49

user9885031
user9885031

Reputation:

Found the solution, by

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

Upvotes: 3

Related Questions