PyQt has a whole class (QtGui.QColor) for handling colors in several formats. This class includes a setNamedColor() method which receives a named color as defined by the SVG color keyword names provided by the World Wide Web Consortium and returns a color as an RGB value.
So, given the (pseudo)code:
my_color = QtGui.QColor()
my_color.setNamedColor("red")
one gets something like "#0000ff"
But what I want is precisely the opposite: a "getNamedColor()" function which, given a valide RGB value would return the name of the color according to the above mentioned SVG color keyword names.
A satisfactory solution would be to use the Matplotlib colors table (see here) in a handy function like that:
import matplotlib.colors as colors
def get_named_color(hex):
return colors.cnames.keys()[colors.cnames.values().index(hex)]
but just to makes things difficult, it seems that the hex codes generated by QColor() are not compatible with those in the Matplotlib color dictionary!
Is there any solution for this out there?