Gradient.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "Gradient.h"
  2. #include "CubicSpline.h"
  3. #include "XmlException.h"
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <functional>
  7. using alm::Gradient;
  8. Gradient::Gradient(void) :
  9. max{ 1.0 }
  10. {
  11. }
  12. Gradient::Gradient(std::vector<std::pair<RGBColor, float>> colors, bool repeat) :
  13. repeat{ repeat }
  14. {
  15. if(colors.empty() || colors.size() < 2)
  16. return;
  17. std::sort(colors.begin(), colors.end(),
  18. [] (const auto& a, const auto& b) {
  19. return a.second < b.second;
  20. });
  21. for (auto& [col, at] : colors) {
  22. pointMap[at] = col;
  23. }
  24. points = colors;
  25. max = colors.at(colors.size() - 1).second;
  26. std::vector<std::pair<float, RGBColorf>> fs;
  27. for (const auto& [col, pos] : points) {
  28. fs.push_back(std::make_pair(pos, col));
  29. }
  30. colorSpline = ColorSpline{ fs, false, false };
  31. return;
  32. }
  33. Gradient::Gradient(std::vector<std::pair<RGBColor, float>> colors, float maxVal, bool repeat) :
  34. repeat{ repeat }
  35. {
  36. if(colors.empty() || colors.size() < 2)
  37. return;
  38. std::sort(colors.begin(), colors.end(),
  39. [] (const auto& a, const auto& b) {
  40. return a.second < b.second;
  41. });
  42. for (auto& [col, at] : colors) {
  43. pointMap[at] = col;
  44. }
  45. points = colors;
  46. max = maxVal;
  47. std::vector<std::pair<float, RGBColorf>> fs;
  48. for (const auto& [col, pos] : points) {
  49. fs.push_back(std::make_pair(pos, col));
  50. }
  51. colorSpline = ColorSpline{ fs, false, false };
  52. return;
  53. }
  54. const std::vector<std::pair<RGBColor, float>>& Gradient::getPoints(void) const
  55. {
  56. return points;
  57. }
  58. Gradient Gradient::defaultGradient(void)
  59. {
  60. return Gradient({
  61. { RGBColor{ 0, 0, 0 }, 0.0f },
  62. { RGBColor{ 0, 255, 255 }, 30.0f },
  63. { RGBColor{ 50, 100, 170 }, 60.0f },
  64. { RGBColor{ 180, 140, 20 }, 90.0f },
  65. { RGBColor{ 255, 255, 0 }, 120.0f },
  66. { RGBColor{ 143, 67, 0 }, 150.0f },
  67. { RGBColor{ 255, 255, 255 }, 180.0f },
  68. { RGBColor{ 20, 30, 180 }, 210.0f },
  69. { RGBColor{ 20, 190, 30 }, 240.0f },
  70. { RGBColor{ 120, 240, 120 }, 270.0f },
  71. { RGBColor{ 0, 0, 0 }, 300.0f },
  72. }, true);
  73. }
  74. bool Gradient::isRepeat(void) const
  75. {
  76. return repeat;
  77. }
  78. float Gradient::getMax(void) const
  79. {
  80. return this->max;
  81. }
  82. RGBColor Gradient::get(float x) const
  83. {
  84. return interpolate(x);
  85. if (colors.empty() || std::isnan(x) || std::isinf(x))
  86. return RGBColor();
  87. /*const auto [left, right, lerp] = getNeighbors(x);
  88. RGBColor lerped = lerpColors(left, right, lerp);
  89. return lerped;*/
  90. if (x < 0)
  91. return colors[0];
  92. if (x > this->max) {
  93. if (repeat)
  94. x = ::fmodf(x, this->max);
  95. else
  96. x = this->max;
  97. }
  98. float pos = x * colors.size() / max;
  99. if (pos < 0) {
  100. pos = 0;
  101. }
  102. if (pos > colors.size() - 1) {
  103. pos = colors.size() - 1;
  104. }
  105. int left = int(pos);
  106. int right = int(pos + 1);
  107. float lerp = pos - left;
  108. if (lerp < 1e-5f) {
  109. return colors[left];
  110. }
  111. else {
  112. return lerpColors(colors[left], colors[right], lerp);
  113. }
  114. }
  115. RGBColor Gradient::interpolate(float x) const
  116. {
  117. if (pointMap.empty()) {
  118. return RGBColor{ 0, 0, 0 };
  119. }
  120. if (x < 0)
  121. return pointMap.begin()->second;
  122. if (x > this->max) {
  123. if (repeat)
  124. x = ::fmodf(x, this->max);
  125. else
  126. return (pointMap.rbegin())->second;
  127. }
  128. return colorSpline.interpolateAt(x);
  129. auto firstLess = pointMap.lower_bound(x);
  130. if (firstLess == pointMap.begin())
  131. return firstLess->second;
  132. if (firstLess == pointMap.end())
  133. return (pointMap.rbegin())->second;
  134. auto oneBefore = firstLess; oneBefore--;
  135. float lo = oneBefore->first;
  136. float hi = firstLess->first;
  137. RGBColor loCol = oneBefore->second;
  138. RGBColor hiCol = firstLess->second;
  139. return lerpColors(loCol, hiCol, (x - lo) / (hi - lo));
  140. }
  141. RGBColorf Gradient::lerpColors(RGBColorf a, RGBColorf b, float val)
  142. {
  143. return RGBColorf {
  144. b.r * val + a.r * (1 - val),
  145. b.g * val + a.g * (1 - val),
  146. b.b * val + a.b * (1 - val)
  147. };
  148. }
  149. RGBColor Gradient::lerpColors(RGBColor a, RGBColor b, float val)
  150. {
  151. return RGBColor{ lerpColors(RGBColorf{ a }, RGBColorf{ b }, val) };
  152. }
  153. /*std::tuple<RGBColor, RGBColor, float> Gradient::getNeighbors(float x) const
  154. {
  155. for (auto it = colors.begin(); it != colors.end(); ++it) {
  156. if (it->second > x) {
  157. if (it == colors.begin()) {
  158. return { it->first, it->first, 0 };
  159. }
  160. else {
  161. float lerp = (x - (it - 1)->second) / (it->second - (it - 1)->second);
  162. return { (it - 1)->first, it->first, lerp };
  163. }
  164. }
  165. }
  166. return { (colors.end() - 1)->first, (colors.end() - 1)->first, 0 };
  167. }*/