Results 1 to 3 of 3

Thread: How to get the specified position's QColor in QLinearGradient

  1. #1
    Join Date
    May 2008
    Posts
    58
    Thanks
    2

    Default How to get the specified position's QColor in QLinearGradient

    I am using Qt4.3 on Windows XP

    my question as below:
    Qt Code:
    1. QLinearGradient linearGrad(startPoint,endPoint);
    2. linearGrad.setColorAt(0,QColor(12,23,24));
    3. linearGrad.setColorAt(1,QColor(122,233,224));
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by marcel; 20th June 2008 at 18:44. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.
    Qt Code:
    1. double segmentLength = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
    2. double pdist = sqrt((xp-x1)*(xp-x1) + (yp-y1)*(yp-y1));
    3. double ratio = pdist/segmentLength;
    4. int red = (int)(ratio*startRedVal + (1-ratio)*endRedValue); //in your case, the values are 12 and 122
    5. int green = (int)(ratio*startGreenVal + (1-ratio)*endGreenValue); //in your case, the values are 23 and 233
    6. int blue = (int)(ratio*startBlueVal + (1-ratio)*endBlueValue); //in your case, the values are 24 and 244
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2008
    Posts
    58
    Thanks
    2

    Default Re: How to get the specified position's QColor in QLinearGradient

    Thank you very much!
    It works very well!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.