aboutsummaryrefslogtreecommitdiff
path: root/colorscale.h
blob: ffc3439c7bc65626f492bc85afe9636525942a59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef COLORSCALE_H
#define COLORSCALE_H

#include <initializer_list>

#include <QColor>
#include <QList>

class ColorScale
{
public:
    ColorScale(const QColor &firstColor, const QColor &lastColor);
    ColorScale(std::initializer_list<QColor> colors);
    ColorScale(const QList<QColor> &colors);
    virtual ~ColorScale();

    QColor operator ()(float t) const { return color(t); }
    virtual QColor color(float t) const;

    void setExtents(float min, float max);
    float min() const { return m_min; }
    float max() const { return m_max; }
    int numColors() const { return m_colors.size(); }

    template<typename OutputIterator>
    void sample(int samples, OutputIterator it) const;

    static QColor lerp(const QColor &c1, const QColor &c2, float _t);

protected:
    float m_min, m_max;
    QList<QColor> m_colors;
};

template<typename OutputIterator>
void ColorScale::sample(int samples, OutputIterator it) const
{
    if (samples < 1) {
        return;
    }

    float step = (max() - min()) / samples;
    qreal r, g, b;
    for (float t = min(); samples-- > 0; t += step) {
        color(t).getRgbF(&r, &g, &b);
        *it++ = r;
        *it++ = g;
        *it++ = b;
    }
}

#endif // COLORSCALE_H