0

code:

QColor color = "#f0f0f0";
qDebug() << color;
qDebug() << color.toHsl();
qDebug() << color.convertTo(QColor::Hsl);

output:

QColor(ARGB 1, 0.941176, 0.941176, 0.941176)
QColor(AHSL 1, -1, 0, 0.941176) // hue == -1 here
QColor(AHSL 1, -1, 0, 0.941176) // hue == -1 here

from docs: QColor::fromHslF():

...

All the values must be in the range 0.0-1.0.

Docs for QML side color hsla():

Returns a color with the specified hue, saturation, lightness, and alpha components. All components should be in the range 0-1 (inclusive).

I need to get correct hue, saturation and lightness values on QML side. How to convert such RGB QColor to HSL QColor?

Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51
  • 1
    From the same docs: "Qt returns a hue value of -1 for achromatic colors". What if you try another color value? – vahancho Apr 06 '23 at 12:40
  • @vahancho yes, hue == -1 only for achromatic colors. I'd fixed that on QML side by forcing `if (color.hue < 0) color.hue = 0` – Vladimir Bershov Apr 07 '23 at 13:52

0 Answers0