Fabs
Fabs

Reputation: 171

Convert HTML color name to hex

Is it possible to convert the HTML color names (i.e. given on w3cschools) to hex?

For example I want something like:

name_to_hex("Orange")   # would return: #FFA500

I found this to be possible with matplotlib in this question.

Upvotes: 3

Views: 450

Answers (1)

Mureinik
Mureinik

Reputation: 312404

The webcolors module can handle this.

First, install it, e.g.:

$ pip install webcolors

Once you have it installed, it's pretty straight-forward:

>>> import webcolors
>>> webcolors.name_to_hex('Orange')
'#ffa500'

Upvotes: 4

Related Questions