PDA

View Full Version : How to set new Color every time draw() loop is executed?



Krish
28th April 2008, 08:18
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.


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.

wysota
28th April 2008, 08:37
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:

r = (r+2) % 256;

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.

Krish
28th April 2008, 09:46
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.

wysota
28th April 2008, 10:07
How about adding a const QColor &color parameter to the draw function? Where are you calling draw() from?

Krish
28th April 2008, 10:57
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

curve[i]->setPen(QPen(color));
Am calling the draw function from main file i.e. curves.cpp!!

Thanks again.

wysota
28th April 2008, 11:44
void draw(const QColor &color){
setColor(color);
doTheDrawing();
}

Krish
28th April 2008, 12:02
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.

wysota
28th April 2008, 12:55
There are 16.7 millions of colours available in Qt's colour space... A simple random will do just fine.


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

Krish
28th April 2008, 15:21
Hey! Wysota,
Thanks for helping me out. Its working now. Now am getting different color of curve each time.:)

Best Regards.

Nadia
24th December 2009, 10:05
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.

john_god
24th December 2009, 11:29
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.

Nadia
25th December 2009, 09:41
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 :



srand( time(0) );//somewhere in main

//and then in function then generate randoms
QColor crand = QColor(rand()%256, rand()%256, rand()%256);


But I think there are should be standard solutions (algorithms) for good colours combinations.

john_god
25th December 2009, 15:07
I mean something like:


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