Results 1 to 7 of 7

Thread: Use QColor to set different color in loop

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2015
    Posts
    109
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Use QColor to set different color in loop

    Hello,

    I have graph which has multiple curves.
    For this curve I have sets some colors, where I dont know how many curve can be into the graph, so I'm setting different color up to 8 curves and on 9th onwards I set fixed blue color to rest of all curves,

    I want to set different color for all the curves, like in the for loop I can change the value of RGB and will get different color, but I don't understand what pattern of loop I should use to get appropriate color, which should be at list visible.

    I can use below logic but the problem is that every time I get different color for the same curve, that I want fixed whenever I start or reload the application, so the below logic will not worked for my application.
    Qt Code:
    1. setCurveColor(QColor( qrand() % 256, qrand() % 256, qrand() % 256 ) );
    To copy to clipboard, switch view to plain text mode 

    Please let me know if any pattern or logic that I can implement for this, so that I can different color and which cannot change if I start or reload application again.

    Thanks

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use QColor to set different color in loop

    I don't fully understand what your question is, but in order to make your colors more visible, you could restrict yourself to the darker range of RGB, like so:

    Qt Code:
    1. setCurveColor(QColor( qrand() % 126, qrand() % 126, qrand() % 126 ) );
    To copy to clipboard, switch view to plain text mode 

    You might want to consider sampling in HSV space instead of RGB, which gives you an better interface to colors of full saturation, say

    Qt Code:
    1. int h = rand*359;
    2. int s = 255;
    3. int v = 128 + rand*127;
    4. return QColor::fromHsv(h,s,v);
    To copy to clipboard, switch view to plain text mode 

    If you want to keep your colors past an exit of the application, you will have to write the colors to a file and load them when the application starts.
    QSettings seems to be best fitting for that.

  3. The following user says thank you to Cruz for this useful post:

    npatil15 (20th February 2019)

  4. #3
    Join Date
    Jun 2015
    Posts
    109
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use QColor to set different color in loop

    Thanks for the quick reply,

    QSettings will work, but if I have open 2 application then this both application has different color for the same curve because we are using qrand/rand, here I want it to be fixed for all the instance of application.
    So my intention is I can create loop and say example,
    Qt Code:
    1. for(int i=0, j=255; i<255 && j>255; i+=25, j-=25)
    2. {
    3. QColor(i, j, i);
    4. //QColor::fromHsv(h,s,v);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Yes that logic is not proper, but just to explain what I want to achieve, just to keep color fixed for any number of application or curves, so here I'm looking for pattern which may fit best but coudn't get that.
    May be their is other better solution.

  5. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use QColor to set different color in loop

    Well then how about you use a fixed random seed, e.g. 0. Then you will get random colors for all curves, but the same random sequence each time.

  6. The following user says thank you to Cruz for this useful post:

    npatil15 (20th February 2019)

  7. #5
    Join Date
    Jun 2015
    Posts
    109
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use QColor to set different color in loop

    Sorry Cruz, I dont understand what do you mean by using fixed random seed ?

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use QColor to set different color in loop

    In main() add this line :
    Qt Code:
    1. qsrand(0);
    To copy to clipboard, switch view to plain text mode 
    After this every time rand will generate this same series.
    Or don't use rand but use fixed colors for curve 1, 2, 3...

  9. The following user says thank you to Lesiok for this useful post:

    npatil15 (20th February 2019)

  10. #7
    Join Date
    Jun 2015
    Posts
    109
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use QColor to set different color in loop

    Thank you all Experts
    It works like charm, I have added qsrand(0) into constructor and whenever I'm using qrand it gets initialize with same color for all the number of curves
    Qt Code:
    1. QColor(qrand()%256, qrand()%256, qrand()%256)
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 6th August 2011, 02:40
  2. change color during a loop
    By 21did21 in forum Newbie
    Replies: 2
    Last Post: 2nd July 2011, 02:13
  3. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 17:31
  4. Replies: 12
    Last Post: 25th December 2009, 16:07
  5. Trouble: Updating color QColor and QBrush
    By SwedishPete in forum Newbie
    Replies: 3
    Last Post: 1st December 2009, 16:34

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.