Gradient.h 1.7 KB

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