Search code examples
c++qtqtguihsl

QColor hsl hue precision


In the code presented below the hsl hue component of the color has a diffrent value than expected.

For: r:255 g:168 b:177 it gives a hsl hue of 353 but on other web tools it yields 354

QColor rgb(c);
QColor hsl = rgb.toHsl();
QColor hsv = rgb.toHsv();

// RGB
int r = rgb.red();
int g = rgb.green();
int b = rgb.blue();

// HSB
int hslh = qMax(hsl.hslHue(), 0);
int hsls = hsl.hslSaturation();
int hsll = hsl.lightness();

Is this a known problem?


Solution

  • The issue seems to be simply how Qt rounds the colour component values when requesting an integer output. To illustrate on your example:

    QColor col(255,168,177);
    std::cout << "hue_float " << col.hslHueF()*360.0f << std::endl;
    std::cout << "hue_int " << col.hslHue() << std::endl;
    

    Outputs:

    hue_float 353.79
    hue_int 353