Results 1 to 3 of 3

Thread: Creating hsl colors

  1. #1
    Join Date
    Oct 2015
    Posts
    33
    Thanks
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Creating hsl colors

    Hi guys,

    i'm trying to make a color map for plotting and want to use hsl colors. was trying to figure out how to properly use it but been stuck on it for a while now

    Qt Code:
    1. //make color
    2. QColor testColor;
    3.  
    4. //check conversion
    5. testColor = testColor.toHsl();
    6. testColor = testColor.fromHsl(120,50,50,255);
    7.  
    8. qDebug() << testColor.isValid();
    9. qDebug() << testColor.spec();
    10.  
    11. //set lable colors
    12. ui->label_1->setText("hi");
    13. QPalette *palette = new QPalette;
    14. //palette->setColor(QPalette::WindowText, Qt::blue); <- works
    15. palette->setColor(QPalette::WindowText, testColor); <- see no color, shows up black
    16. ui->label_1->setPalette(*palette);
    To copy to clipboard, switch view to plain text mode 

    i made a quick gui with a lable which i want to apply the hsl color to. but it seems to be not working. am i making a basic syntax/logic mistake here? the output from the debug statments is:

    true
    4 //= HSL

    thx for the help!


    PS: this is what i actually want to achieve. A list of colors to save in a vector and use it for different data types
    Qt Code:
    1. QVector<QColor> colorvec;
    2. colorvec.resize(particlemass_len);
    3.  
    4. for (int i=0;i<particlemass_len;i++)
    5. {
    6. int hue = i * 359/particlemass_len;
    7. int saturation = 80;
    8. int lightness = 50;
    9. int alpha = 255;
    10. colorvec[i] = QColor::fromHsl(hue,saturation,lightness,alpha);
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating hsl colors

    The better scenario for setting a QPalette is this:

    Qt Code:
    1. QPalette pal = myWidget->palette();
    2. pal.setColor( QPalette::WindowText, newCOlor );
    3. myWidget->setPalette( pal );
    To copy to clipboard, switch view to plain text mode 

    By doing so, you retain all of the colors that are already set in the widget instead of reverting to the default set you get when you construct a new QPalette. If you don't delete the QPalette instance you make on the heap in your implementation, that's a memory leak.

    I don't know if that is the source of your problem. It could also be that because you create an empty QColor (testColor) this is causing problems in your testing. What if you create it with a default color (Qt:: blue for example)?

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

    Lumbricus (3rd March 2016)

  4. #3
    Join Date
    Oct 2015
    Posts
    33
    Thanks
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Creating hsl colors

    Thx for the reply stranz! Of course you are right allocating on heap was my bad, but in this case not the problem. I found the solution was in chosing a bad combination of hue, saturation and lightness, the color was so near black i could not see the difference. Code above works if you use something like these values:

    testColor.fromHsl(100,255,110,255); //light green

    sry for the fuss about nothing :/

Similar Threads

  1. Help on QTabBar colors
    By smacchia in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2012, 17:48
  2. How to set colors in arraylist?
    By naufalahad in forum Qt Programming
    Replies: 9
    Last Post: 28th November 2011, 15:56
  3. MouseEvent and colors
    By arubagirl in forum Newbie
    Replies: 1
    Last Post: 15th April 2011, 10:24
  4. QToolBox Colors
    By rooney in forum Qt Programming
    Replies: 3
    Last Post: 3rd September 2010, 15:37
  5. row colors
    By nategoofs in forum Qt Programming
    Replies: 3
    Last Post: 17th August 2007, 20:16

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.