Gradient.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifndef LIBALMOND_GRADIENT_H
  2. #define LIBALMOND_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. #include <memory>
  11. namespace alm
  12. {
  13. class Gradient;
  14. }
  15. class alm::Gradient
  16. {
  17. std::vector<std::pair<RGBColor, float>> points;
  18. std::map<float, RGBColor, std::greater<float>> pointMap;
  19. ColorSpline colorSpline;
  20. /// the colors of this gradient stored in linear RGB format
  21. /// so they can be easily interpolated
  22. std::vector<RGBColorf> colors;
  23. float max;
  24. bool repeat;
  25. public:
  26. Gradient(void);
  27. Gradient(std::vector<std::pair<RGBColor, float>> colors, bool repeat = true);
  28. Gradient(std::vector<std::pair<RGBColor, float>> colors, float maxValue, bool repeat = true);
  29. const std::vector<std::pair<RGBColor, float>>& getPoints(void) const;
  30. static Gradient defaultGradient(void);
  31. bool isRepeat(void) const;
  32. ///
  33. /// \brief get the maximum value this gradient accepts
  34. ///
  35. /// If \link Gradient::get(float) is called with a value
  36. /// greater than the one returned by this function, the
  37. /// value will either get clamped or wrapped around.
  38. ///
  39. float getMax(void) const;
  40. ///
  41. /// \brief get a color at a specific position in this gradient
  42. /// \param x the position
  43. /// \return the color in sRGB format
  44. ///
  45. RGBColor get(float x) const;
  46. RGBColor interpolate(float x) const;
  47. private:
  48. static RGBColorf lerpColors(RGBColorf a, RGBColorf b, float val);
  49. static RGBColor lerpColors(RGBColor a, RGBColor b, float val);
  50. std::tuple<RGBColor, RGBColor, float> getNeighbors(float x) const;
  51. };
  52. #endif // LIBALMOND_GRADIENT_H