Gradient.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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, int precalcSteps) :
  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. std::vector<std::pair<RGBColorf, float>> linearColors;
  33. std::transform(colors.begin(), colors.end(), std::back_inserter(linearColors),
  34. [] (auto c) { return c; });
  35. std::vector<std::pair<float, float>> rs;
  36. std::vector<std::pair<float, float>> gs;
  37. std::vector<std::pair<float, float>> bs;
  38. std::transform(linearColors.begin(), linearColors.end(), std::back_inserter(rs),
  39. [] (auto p) { return std::pair{ p.second, p.first.r }; });
  40. std::transform(linearColors.begin(), linearColors.end(), std::back_inserter(gs),
  41. [] (auto p) { return std::pair{ p.second, p.first.g }; });
  42. std::transform(linearColors.begin(), linearColors.end(), std::back_inserter(bs),
  43. [] (auto p) { return std::pair{ p.second, p.first.b }; });
  44. CubicSpline rsp(rs, false, true);
  45. CubicSpline gsp(gs, false, true);
  46. CubicSpline bsp(bs, false, true);
  47. if(precalcSteps <= 0) {
  48. precalcSteps = int(max * 7) + 10;
  49. }
  50. if (precalcSteps > 12000) {
  51. precalcSteps = 12000;
  52. }
  53. for (int i = 0; i < precalcSteps; i++) {
  54. float position = i * max / precalcSteps;
  55. RGBColorf at = {
  56. rsp.interpolateAt(position),
  57. gsp.interpolateAt(position),
  58. bsp.interpolateAt(position)
  59. };
  60. this->colors.push_back(at);
  61. }
  62. }
  63. Gradient::Gradient(std::vector<std::pair<RGBColor, float>> colors, float maxVal, bool repeat, int precalcSteps) :
  64. repeat{ repeat }
  65. {
  66. if(colors.empty() || colors.size() < 2)
  67. return;
  68. std::sort(colors.begin(), colors.end(),
  69. [] (const auto& a, const auto& b) {
  70. return a.second < b.second;
  71. });
  72. for (auto& [col, at] : colors) {
  73. pointMap[at] = col;
  74. }
  75. points = colors;
  76. max = maxVal;
  77. std::vector<std::pair<float, RGBColorf>> fs;
  78. for (const auto& [col, pos] : points) {
  79. fs.push_back(std::make_pair(pos, col));
  80. }
  81. colorSpline = ColorSpline{ fs, false, false };
  82. return;
  83. std::vector<std::pair<RGBColorf, float>> linearColors;
  84. std::transform(colors.begin(), colors.end(), std::back_inserter(linearColors),
  85. [] (auto c) { return c; });
  86. std::vector<std::pair<float, float>> rs;
  87. std::vector<std::pair<float, float>> gs;
  88. std::vector<std::pair<float, float>> bs;
  89. std::transform(linearColors.begin(), linearColors.end(), std::back_inserter(rs),
  90. [] (auto p) { return std::pair{ p.second, p.first.r }; });
  91. std::transform(linearColors.begin(), linearColors.end(), std::back_inserter(gs),
  92. [] (auto p) { return std::pair{ p.second, p.first.g }; });
  93. std::transform(linearColors.begin(), linearColors.end(), std::back_inserter(bs),
  94. [] (auto p) { return std::pair{ p.second, p.first.b }; });
  95. CubicSpline rsp(rs, false, true);
  96. CubicSpline gsp(gs, false, true);
  97. CubicSpline bsp(bs, false, true);
  98. if(precalcSteps <= 0) {
  99. precalcSteps = int(max * 7) + 10;
  100. }
  101. if (precalcSteps > 12000) {
  102. precalcSteps = 12000;
  103. }
  104. for (int i = 0; i < precalcSteps; i++) {
  105. float position = i * max / precalcSteps;
  106. RGBColorf at = {
  107. rsp.interpolateAt(position),
  108. gsp.interpolateAt(position),
  109. bsp.interpolateAt(position)
  110. };
  111. this->colors.push_back(at);
  112. }
  113. }
  114. const std::vector<std::pair<RGBColor, float>>& Gradient::getPoints(void) const
  115. {
  116. return points;
  117. }
  118. Gradient Gradient::defaultGradient(void)
  119. {
  120. return Gradient({
  121. { RGBColor{ 0, 0, 0 }, 0.0f },
  122. { RGBColor{ 0, 255, 255 }, 30.0f },
  123. { RGBColor{ 50, 100, 170 }, 60.0f },
  124. { RGBColor{ 180, 140, 20 }, 90.0f },
  125. { RGBColor{ 255, 255, 0 }, 120.0f },
  126. { RGBColor{ 143, 67, 0 }, 150.0f },
  127. { RGBColor{ 255, 255, 255 }, 180.0f },
  128. { RGBColor{ 20, 30, 180 }, 210.0f },
  129. { RGBColor{ 20, 190, 30 }, 240.0f },
  130. { RGBColor{ 120, 240, 120 }, 270.0f },
  131. { RGBColor{ 0, 0, 0 }, 300.0f },
  132. }, true);
  133. }
  134. bool Gradient::isRepeat(void) const
  135. {
  136. return repeat;
  137. }
  138. float Gradient::getMax(void) const
  139. {
  140. return this->max;
  141. }
  142. RGBColor Gradient::get(float x) const
  143. {
  144. return interpolate(x);
  145. if (colors.empty() || std::isnan(x) || std::isinf(x))
  146. return RGBColor();
  147. /*const auto [left, right, lerp] = getNeighbors(x);
  148. RGBColor lerped = lerpColors(left, right, lerp);
  149. return lerped;*/
  150. if (x < 0)
  151. return colors[0];
  152. if (x > this->max) {
  153. if (repeat)
  154. x = ::fmodf(x, this->max);
  155. else
  156. x = this->max;
  157. }
  158. float pos = x * colors.size() / max;
  159. if (pos < 0) {
  160. pos = 0;
  161. }
  162. if (pos > colors.size() - 1) {
  163. pos = colors.size() - 1;
  164. }
  165. int left = int(pos);
  166. int right = int(pos + 1);
  167. float lerp = pos - left;
  168. if (lerp < 1e-5f) {
  169. return colors[left];
  170. }
  171. else {
  172. return lerpColors(colors[left], colors[right], lerp);
  173. }
  174. }
  175. RGBColor Gradient::interpolate(float x) const
  176. {
  177. return colorSpline.interpolateAt(x);
  178. if (pointMap.empty()) {
  179. return RGBColor{ 0, 0, 0 };
  180. }
  181. if (x < 0)
  182. return pointMap.begin()->second;
  183. if (x > this->max) {
  184. if (repeat)
  185. x = ::fmodf(x, this->max);
  186. else
  187. return (pointMap.rbegin())->second;
  188. }
  189. auto firstLess = pointMap.lower_bound(x);
  190. if (firstLess == pointMap.begin())
  191. return firstLess->second;
  192. if (firstLess == pointMap.end())
  193. return (pointMap.rbegin())->second;
  194. auto oneBefore = firstLess; oneBefore--;
  195. float lo = oneBefore->first;
  196. float hi = firstLess->first;
  197. RGBColor loCol = oneBefore->second;
  198. RGBColor hiCol = firstLess->second;
  199. return lerpColors(loCol, hiCol, (x - lo) / (hi - lo));
  200. }
  201. RGBColorf Gradient::lerpColors(RGBColorf a, RGBColorf b, float val)
  202. {
  203. return RGBColorf {
  204. b.r * val + a.r * (1 - val),
  205. b.g * val + a.g * (1 - val),
  206. b.b * val + a.b * (1 - val)
  207. };
  208. }
  209. RGBColor Gradient::lerpColors(RGBColor a, RGBColor b, float val)
  210. {
  211. return RGBColor{ lerpColors(RGBColorf{ a }, RGBColorf{ b }, val) };
  212. }
  213. /*std::tuple<RGBColor, RGBColor, float> Gradient::getNeighbors(float x) const
  214. {
  215. for (auto it = colors.begin(); it != colors.end(); ++it) {
  216. if (it->second > x) {
  217. if (it == colors.begin()) {
  218. return { it->first, it->first, 0 };
  219. }
  220. else {
  221. float lerp = (x - (it - 1)->second) / (it->second - (it - 1)->second);
  222. return { (it - 1)->first, it->first, lerp };
  223. }
  224. }
  225. }
  226. return { (colors.end() - 1)->first, (colors.end() - 1)->first, 0 };
  227. }*/