Results 1 to 13 of 13

Thread: How to set new Color every time draw() loop is executed?

  1. #1
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question How to set new Color every time draw() loop is executed?

    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.
    Qt Code:
    1. draw()
    2. { // r = 10; g = 255; & b = 180; are static & set previously
    3. color.setRgb(r,g,b);
    4. ..
    5. ... // using the color
    6. ....
    7. r += 2;
    8. g -= 5;
    9. b -= 8;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Can anyone please give me a hint how to solve this.
    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to set new Color every time draw() loop is executed?

    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:
    Qt Code:
    1. r = (r+2) % 256;
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set new Color every time draw() loop is executed?

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to set new Color every time draw() loop is executed?

    How about adding a const QColor &color parameter to the draw function? Where are you calling draw() from?

  5. #5
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set new Color every time draw() loop is executed?

    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
    Qt Code:
    1. curve[i]->setPen(QPen(color));
    To copy to clipboard, switch view to plain text mode 
    Am calling the draw function from main file i.e. curves.cpp!!

    Thanks again.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to set new Color every time draw() loop is executed?

    Qt Code:
    1. void draw(const QColor &color){
    2. setColor(color);
    3. doTheDrawing();
    4. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set new Color every time draw() loop is executed?

    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.
    Last edited by Krish; 28th April 2008 at 12:08.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to set new Color every time draw() loop is executed?

    There are 16.7 millions of colours available in Qt's colour space... A simple random will do just fine.

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

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

    Krish (28th April 2008)

  10. #9
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up [SOLVED] How to set new Color every time draw() loop is executed?

    Hey! Wysota,
    Thanks for helping me out. Its working now. Now am getting different color of curve each time.

    Best Regards.

  11. #10
    Join Date
    Aug 2009
    Location
    Lviv
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set new Color every time draw() loop is executed?

    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.

  12. #11
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    287
    Thanks
    23
    Thanked 31 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: How to set new Color every time draw() loop is executed?

    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.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  13. #12
    Join Date
    Aug 2009
    Location
    Lviv
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set new Color every time draw() loop is executed?

    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 :

    Qt Code:
    1. srand( time(0) );//somewhere in main
    2.  
    3. //and then in function then generate randoms
    4. QColor crand = QColor(rand()%256, rand()%256, rand()%256);
    To copy to clipboard, switch view to plain text mode 

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

  14. #13
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    287
    Thanks
    23
    Thanked 31 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Wink Re: How to set new Color every time draw() loop is executed?

    I mean something like:

    Qt Code:
    1. int r;
    2. int g;
    3. int b;
    4.  
    5. //define the followin values for a range of grey defined by you
    6. int r_upper_grey = ...;
    7. int r_lower_grey = ...;
    8. int g_upper_grey = ...;
    9. int g_lower_grey = ...;
    10. int b_upper_grey = ...;
    11. int b_lower_grey = ...;
    12.  
    13. do
    14. {
    15. r= qrand() % 256;
    16. g= qrand() % 256;
    17. b= qrand() % 256;
    18.  
    19. }while ( ( r < r_upper_grey && r > r_lower_grey) &&
    20. ( g < g_upper_grey && g > g_lower_grey) &&
    21. ( b < b_upper_grey && b > b_lower_grey) );
    To copy to clipboard, switch view to plain text mode 

    Get the picture ? Define a range of grey to avoid and keep random the numbers
    until they out of the grey zone
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

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.