Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sort HSL colors



  • Hello, I would like some help to write an algorithm to sort HSL colors from an 512 colors.

    I understood that I had to use the hue, saturation and lightness values to order the colors, but I can not get a gradient.

    Do you have an example or an indication to give me?

    Thanks in advance.



  • use std::sort with a custom Comparer that gives hsl as input
    https://en.cppreference.com/w/cpp/algorithm/sort



  • Hello thanks for your reply.

    I alreay use this function for sort the QVector<QColor> but the result is not very good.

    The best result is this comparaison :

    int compare(const QColor &a, const QColor &b)
    {
        return a.rgb() < b.rgb();
    }
    

    I wrote this code, but the colors the sort does not give a result does not give a gradient.

    int compare(const QColor &a, const QColor &b)
    {
        if(a.hue() == b.hue())
        {
            if(a.saturation() == b.saturation())
            {
                return (a.lightness() <= b.lightness());
            }
           return (a.saturation() <= b.saturation());
        }
        return (a.hue() < b.hue());
    }
    

Log in to reply