PDA

View Full Version : Generating distinguishes colors from QColor::colorNames()



time
9th November 2016, 22:07
Hi
I wrote a small loop in c++ for generating colors from QColor::colorNames(). But it generates it reaaaaly randomly, so together they look really really bad.
Is there any idea how to generate distinguishable colours(blue, red, green, yellow, orange, white etc.) first? I know it depence from i-value in loop, but no idea how to set it
Here is my code:



QStringList colorName = QColor::colorNames();
QVector<QColor> colorCat(numCat); // number of categories I have to color
for(int i =0; i < numCatt; i++)
colorCat[i] = colorName[i]; //I also tried something like colorName[2*i], or [i*i] to generate something normal, but it failed

anda_skoa
10th November 2016, 07:41
I guess the easiest way is to predefine a list of colors according to your criteria an take values from there when needed.

Cheers,
_

time
10th November 2016, 13:04
I guess the easiest way is to predefine a list of colors according to your criteria an take values from there when needed._

Hello, thank you for answer.
The idea is not bad, but unfortunatelly I don't know how many colors I will be need. So it can be 4 or 44, or maybe 100. So predefine 100 colors is not the 'clever' way to solve this problem, but obviously I can do that and it will be working :)
But I am looking for some universal way to make colors.
Cheers!

Killian
10th November 2016, 15:45
What you also could do (if you can avoid using colorNames()) is to create the colors yourself on-the-fly. Have a look at the standard color wheel (http://dba.med.sc.edu/price/irf/Adobe_tg/models/images/hsl_top.JPG), just subdivide the HUE value by the amount of colors you need. Finally sort your color list so that each color has a neighbor with it's very opposite color. This way you could create a list like: [red, cyan, yellow, blue ...] which is pretty much as distinguishable as it can get.

time
11th November 2016, 11:47
Thank you. But how I can do that without colorNames()? I don't understand the idea on-the-fly

Killian
11th November 2016, 13:08
On-the-fly just meant when required. QColor provides a method called fromHsv() (http://doc.qt.io/qt-5/qcolor.html#fromHsv). You calculate the designated hue and pass it to the function. The result is your color. Just calculate hue values in such way that neighboring colors in your list differ by 180 (degrees).