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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#ifndef COLORSCALE_H
#define COLORSCALE_H
#include <initializer_list>
#include <vector>
class Color
{
public:
Color();
Color(int r, int g, int b);
Color(int r, int g, int b, int a);
Color(float r, float g, float b);
Color(float r, float g, float b, float a);
void getRgbF(float *r, float *g, float *b) const;
void getRgbF(float *r, float *g, float *b, float *a) const;
void setRgb(int r, int g, int b);
void setRgb(int r, int g, int b, int a);
void setRgbF(float r, float g, float b);
void setRgbF(float r, float g, float b, float a);
float r, g, b, a;
};
using QColor = Color;
class ColorScale
{
public:
ColorScale(const Color &firstColor, const Color &lastColor);
ColorScale(std::initializer_list<Color> colors);
ColorScale(const std::vector<Color> &colors);
virtual ~ColorScale();
Color operator ()(float t) const { return color(t); }
virtual Color 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(std::size_t samples, OutputIterator it) const;
static Color lerp(const Color &c1, const Color &c2, float _t);
protected:
float m_min, m_max;
std::vector<Color> m_colors;
};
template<typename OutputIterator>
void ColorScale::sample(std::size_t samples, OutputIterator it) const
{
if (samples < 1) {
return;
}
float step = (max() - min()) / samples;
float 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
|