How to set new Color every time draw() loop is executed?
Hello! Freinds,
I want to set different color whenever draw() loop is executed. Am using this newly created color to set color for the curves. I am trying to increment, decrement the rgb values
say by 1 or 2 but not that much noticeable change:( Also they shouldn't cross their limit i.e. 0-255.
Code:
draw()
{ // r = 10; g = 255; & b = 180; are static & set previously
color.setRgb(r,g,b);
..
... // using the color
....
r += 2;
g -= 5;
b -= 8;
}
Can anyone please give me a hint how to solve this.
Thanks in advance.
Re: How to set new Color every time draw() loop is executed?
What exactly do you have problems with? For keeping values inside (0,255) bounds you can use qBound() or calculate the values modulus 256, like so:
By the way - if your draw() functions actually does some drawing, it might not be a good idea to change the colours inside it - the function might be called when you don't expect it and the colour will change.
Re: How to set new Color every time draw() loop is executed?
Thanks Wysota for replying.
Yes am drawing curves in draw() function. And to distinguish every new curve drawn i need to set different colors for each curve when they are drawn.
Any idea how to do this!!
Thanks again in advance.
Re: How to set new Color every time draw() loop is executed?
How about adding a const QColor &color parameter to the draw function? Where are you calling draw() from?
Re: How to set new Color every time draw() loop is executed?
Sorry to say but i didn't get your idea about "const QColor &color parameter". I mean how can i change their colors to distinguish them on the plot? I am using the color parameter to set curve' s color as
Code:
curve
[i
]->setPen
(QPen(color
));
Am calling the draw function from main file i.e. curves.cpp!!
Thanks again.
Re: How to set new Color every time draw() loop is executed?
Code:
void draw
(const QColor &color
){ setColor(color);
doTheDrawing();
}
Re: How to set new Color every time draw() loop is executed?
Thanks Wysota,
But even here i will have to pass different color parameter to the draw() function, so from where to get/generate these different colors everytime dynamically.
Frankly i have to draw more than 60 - 80 curves so how can i get/generate different colors everytime this draw() function is called/executed.
Even there are only 20 predefined QColors!!
Thanks again.
Re: How to set new Color every time draw() loop is executed?
There are 16.7 millions of colours available in Qt's colour space... A simple random will do just fine.
Code:
draw
( QColor( qrand
() % 256, qrand
() % 256, qrand
() % 256 ) );
[SOLVED] How to set new Color every time draw() loop is executed?
Hey! Wysota,
Thanks for helping me out. Its working now. Now am getting different color of curve each time.:)
Best Regards.
Re: How to set new Color every time draw() loop is executed?
Hi,
And maybe someone have any idea how to mix two QColors(adding them give wrong result). Or any idea how to avoid gray color.I also need generate random colours, except gray.
Thanks in advance.
Re: How to set new Color every time draw() loop is executed?
Just assign a qrand() to the r, g, b variables and perform a while cicle, until the randow values are diferent from the range values of the grey color.
In the case of Krish, and if you would have a maxim limit of curves to draw, like 100, another aproach would be to create a array/list of color's. That way you could give the user the change to personalize each curve color.
Re: How to set new Color every time draw() loop is executed?
Now, I just make simple check whether r, g an b values not equal (instead of while). But there are the colours that are not actually grey but very similar and lays bad on gray. And what does you mean "assign a qrand() to the r, g, b variables? I just do :
Code:
srand( time(0) );//somewhere in main
//and then in function then generate randoms
But I think there are should be standard solutions (algorithms) for good colours combinations.
Re: How to set new Color every time draw() loop is executed?
I mean something like:
Code:
int r;
int g;
int b;
//define the followin values for a range of grey defined by you
int r_upper_grey = ...;
int r_lower_grey = ...;
int g_upper_grey = ...;
int g_lower_grey = ...;
int b_upper_grey = ...;
int b_lower_grey = ...;
do
{
r= qrand() % 256;
g= qrand() % 256;
b= qrand() % 256;
}while ( ( r < r_upper_grey && r > r_lower_grey) &&
( g < g_upper_grey && g > g_lower_grey) &&
( b < b_upper_grey && b > b_lower_grey) );
Get the picture ? Define a range of grey to avoid and keep random the numbers
until they out of the grey zone