PDA

View Full Version : Use QColor to set different color in loop



npatil15
19th February 2019, 13:46
Hello,

I have graph which has multiple curves.
For this curve I have sets some colors, where I dont know how many curve can be into the graph, so I'm setting different color up to 8 curves and on 9th onwards I set fixed blue color to rest of all curves,

I want to set different color for all the curves, like in the for loop I can change the value of RGB and will get different color, but I don't understand what pattern of loop I should use to get appropriate color, which should be at list visible.

I can use below logic but the problem is that every time I get different color for the same curve, that I want fixed whenever I start or reload the application, so the below logic will not worked for my application.

setCurveColor(QColor( qrand() % 256, qrand() % 256, qrand() % 256 ) );

Please let me know if any pattern or logic that I can implement for this, so that I can different color and which cannot change if I start or reload application again.

Thanks

Cruz
19th February 2019, 13:53
I don't fully understand what your question is, but in order to make your colors more visible, you could restrict yourself to the darker range of RGB, like so:


setCurveColor(QColor( qrand() % 126, qrand() % 126, qrand() % 126 ) );

You might want to consider sampling in HSV space instead of RGB, which gives you an better interface to colors of full saturation, say



int h = rand*359;
int s = 255;
int v = 128 + rand*127;
return QColor::fromHsv(h,s,v);


If you want to keep your colors past an exit of the application, you will have to write the colors to a file and load them when the application starts.
QSettings (https://doc.qt.io/qt-5/qsettings.html#details) seems to be best fitting for that.

npatil15
19th February 2019, 14:13
Thanks for the quick reply,

QSettings will work, but if I have open 2 application then this both application has different color for the same curve because we are using qrand/rand, here I want it to be fixed for all the instance of application.
So my intention is I can create loop and say example,

for(int i=0, j=255; i<255 && j>255; i+=25, j-=25)
{
QColor(i, j, i);
//QColor::fromHsv(h,s,v);
}
Yes that logic is not proper, but just to explain what I want to achieve, just to keep color fixed for any number of application or curves, so here I'm looking for pattern which may fit best but coudn't get that.
May be their is other better solution.

Cruz
19th February 2019, 14:39
Well then how about you use a fixed random seed, e.g. 0. Then you will get random colors for all curves, but the same random sequence each time.

npatil15
19th February 2019, 15:44
Sorry Cruz, I dont understand what do you mean by using fixed random seed ?

Lesiok
19th February 2019, 15:54
In main() add this line :
qsrand(0); After this every time rand will generate this same series.
Or don't use rand but use fixed colors for curve 1, 2, 3...

npatil15
20th February 2019, 06:32
Thank you all Experts :)
It works like charm, I have added qsrand(0) into constructor and whenever I'm using qrand it gets initialize with same color for all the number of curves

QColor(qrand()%256, qrand()%256, qrand()%256)