PDA

View Full Version : How to get the specified position's QColor in QLinearGradient



yxmaomao
20th June 2008, 07:54
I am using Qt4.3 on Windows XP

my question as below:


QLinearGradient linearGrad(startPoint,endPoint);
linearGrad.setColorAt(0,QColor(12,23,24));
linearGrad.setColorAt(1,QColor(122,233,224));

Now if I want to get the QColor of one specified position which is between startPoint and endPoint.
what should I do?

Thanks in advance!

marcel
20th June 2008, 18:52
You can do a linear interpolation and build the QColor. Let's assume you want the color at the point P, which is on the line between startP and endP.


double segmentLength = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
double pdist = sqrt((xp-x1)*(xp-x1) + (yp-y1)*(yp-y1));
double ratio = pdist/segmentLength;
int red = (int)(ratio*startRedVal + (1-ratio)*endRedValue); //in your case, the values are 12 and 122
int green = (int)(ratio*startGreenVal + (1-ratio)*endGreenValue); //in your case, the values are 23 and 233
int blue = (int)(ratio*startBlueVal + (1-ratio)*endBlueValue); //in your case, the values are 24 and 244

yxmaomao
23rd June 2008, 01:26
Thank you very much!
It works very well!