Gradient.h 1.4 KB

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