I have a colour object in QML which I think is an instance of QColor, using the debugger I can see the colour object contains:
a 1
b 0
g 0
hslHue -1
hslLightness 0
hslSaturation 0
hsvHue -1
hsvSaturation 0
hsvValue 0
r 0
Is there a way to translate this into a human readable string, e.g Red ?
Colors are a difficult topic. (If you ever connected two screens to your computer and tried to configure them for identical color profiles you know what I mean.)
Nevertheless, I believe the intention of OP is reasonable. If Qt can process human-readable color names why shouldn't this reversible?
At first, I had a look into the manual – QColor:
The statement about
QColor::name()sounds precisely like what OP described. To convince myself completely, I made an MCVE but it didn't change anything.OK. So, it's not a bug – it's a feature.
If it's missing in Qt how this could be added? Qt seems to “know” all the names. So, it would be annoying if this cannot be exploited somehow.
I clicked a bit through the doc. and found e.g. in the doc. of QColor::setNamedColor a link for the table of the SVG Color Names.
So, I considered for a second just to copy it.
I also found the SVG Colors. Please, note, that it's remarked with
and still part of the
qt5-devdoc. (at the time of this writing).On
woboq.org, I stumbled into the responsible source code for the color names inqtbase/src/gui/painting/qcolor.cpp:…
And, finally, I ended up in QColor::colorNames():
With a list of all color names (which Qt can recognize) it's easy to build a reverse mapping table.
I made an MCVE
testQColorName.ccto demonstrate this:Output:
Console Output:
Note:
It seems that
QColordoes provide neither a less (to allow usage as key instd::map) nor a specialization ofstd::hash(to allow usage as key instd::unordered_map). So, I had to specializestd::hashmyself forQColor.→ SO: How to specialize std::hash::operator() for user-defined type in unordered containers?)