Gradient.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifndef GRADIENT_H
  2. #define GRADIENT_H
  3. #include <vector>
  4. #include <map>
  5. #include <string>
  6. #include "Color.h"
  7. #include "CubicSpline.h"
  8. #include <tuple>
  9. #include <cinttypes>
  10. class Gradient
  11. {
  12. std::vector<std::pair<RGBColor, float>> points;
  13. std::map<float, RGBColor, std::greater<float>> pointMap;
  14. ColorSpline colorSpline;
  15. /// the colors of this gradient stored in linear RGB format
  16. /// so they can be easily interpolated
  17. std::vector<RGBColorf> colors;
  18. float max;
  19. bool repeat;
  20. public:
  21. Gradient(void);
  22. Gradient(std::vector<std::pair<RGBColor, float>> colors, bool repeat = false, int precalcSteps = -1);
  23. Gradient(std::vector<std::pair<RGBColor, float>> colors, float maxValue, bool repeat = false, int precalcSteps = -1);
  24. const std::vector<std::pair<RGBColor, float>>& getPoints(void) const;
  25. inline bool isRepeat(void) const { return repeat; }
  26. static Gradient defaultGradient(void);
  27. static Gradient fromXml(const std::string& xml);
  28. std::string toXml(void) const;
  29. ///
  30. /// \brief get the maximum value this gradient accepts
  31. ///
  32. /// If \link Gradient::get(float) is called with a value
  33. /// greater than the one returned by this function, the
  34. /// value will either get clamped or wrapped around.
  35. ///
  36. float getMax(void) const;
  37. ///
  38. /// \brief get a color at a specific position in this gradient
  39. /// \param x the position
  40. /// \return the color in sRGB format
  41. ///
  42. RGBColor get(float x) const;
  43. RGBColor interpolate(float x) const;
  44. private:
  45. static RGBColorf lerpColors(RGBColorf a, RGBColorf b, float val);
  46. static RGBColor lerpColors(RGBColor a, RGBColor b, float val);
  47. std::tuple<RGBColor, RGBColor, float> getNeighbors(float x) const;
  48. };
  49. #endif // GRADIENT_H