Gradient.h 1.5 KB

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