aboutsummaryrefslogtreecommitdiff
path: root/colorscale.h
blob: 1eb8c47ff7dfdfb7d04731f53f5c5c6c1ec80c2e (about) (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
#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;

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