How to get the specified position's QColor in QLinearGradient
I am using Qt4.3 on Windows XP
my question as below:
Code:
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!
Re: How to get the specified position's QColor in QLinearGradient
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.
Code:
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
Re: How to get the specified position's QColor in QLinearGradient
Thank you very much!
It works very well!