PDA

View Full Version : Creating hsl colors



Lumbricus
3rd March 2016, 12:53
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




//make color
QColor testColor;

//check conversion
testColor = testColor.toHsl();
testColor = testColor.fromHsl(120,50,50,255);

qDebug() << testColor.isValid();
qDebug() << testColor.spec();

//set lable colors
ui->label_1->setText("hi");
QPalette *palette = new QPalette;
//palette->setColor(QPalette::WindowText, Qt::blue); <- works
palette->setColor(QPalette::WindowText, testColor); <- see no color, shows up black
ui->label_1->setPalette(*palette);



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


QVector<QColor> colorvec;
colorvec.resize(particlemass_len);

for (int i=0;i<particlemass_len;i++)
{
int hue = i * 359/particlemass_len;
int saturation = 80;
int lightness = 50;
int alpha = 255;
colorvec[i] = QColor::fromHsl(hue,saturation,lightness,alpha);
}

d_stranz
3rd March 2016, 16:27
The better scenario for setting a QPalette is this:



QPalette pal = myWidget->palette();
pal.setColor( QPalette::WindowText, newCOlor );
myWidget->setPalette( pal );


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)?

Lumbricus
3rd March 2016, 17:58
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 :/