A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

drawutil.cc 1005B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "drawutil.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. namespace Swan {
  5. namespace Draw {
  6. static float linearLine(float from, float to, float frac) {
  7. return std::clamp(to * frac + from * (1 - frac), 0.0f, 255.0f);
  8. }
  9. static Cygnet::Color linearColor(Cygnet::Color from, Cygnet::Color to, float frac) {
  10. return {
  11. .r = linearLine(from.r, to.r, frac),
  12. .g = linearLine(from.g, to.g, frac),
  13. .b = linearLine(from.b, to.b, frac),
  14. .a = linearLine(from.a, to.a, frac),
  15. };
  16. }
  17. Cygnet::Color linearGradient(
  18. float val,
  19. std::initializer_list<std::pair<float, Cygnet::Color>> colors) {
  20. const std::pair<float, Cygnet::Color> *arr = colors.begin();
  21. size_t size = colors.size();
  22. if (val < arr[0].first)
  23. return arr[0].second;
  24. for (size_t i = 1; i < size; ++i) {
  25. if (arr[i].first < val)
  26. continue;
  27. auto [fromv, fromc] = arr[i - 1];
  28. auto [tov, toc] = arr[i];
  29. float frac = (val - fromv) / (tov - fromv);
  30. return linearColor(fromc, toc, frac);
  31. }
  32. return arr[size - 1].second;
  33. }
  34. }
  35. }