#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; }; 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