Gradient.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. inline bool isRepeat(void) const { return repeat; }
  22. static Gradient defaultGradient(void);
  23. static Gradient fromXml(const std::string& xml);
  24. std::string toXml(void) const;
  25. ///
  26. /// \brief get the maximum value this gradient accepts
  27. ///
  28. /// If \link Gradient::get(float) is called with a value
  29. /// greater than the one returned by this function, the
  30. /// value will either get clamped or wrapped around.
  31. ///
  32. float getMax(void) const;
  33. ///
  34. /// \brief get a color at a specific position in this gradient
  35. /// \param x the position
  36. /// \return the color in sRGB format
  37. ///
  38. RGBColor get(float x) const;
  39. private:
  40. static RGBColorf lerpColors(RGBColorf a, RGBColorf b, float val);
  41. static RGBColor lerpColors(RGBColor a, RGBColor b, float val);
  42. std::tuple<RGBColor, RGBColor, float> getNeighbors(float x) const;
  43. };
  44. #endif // GRADIENT_H