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.

64 lines
1.1KB

  1. #pragma once
  2. #include "util/math.hpp"
  3. #include "nanovg.h"
  4. namespace rack {
  5. inline NVGcolor colorClip(NVGcolor a) {
  6. for (int i = 0; i < 4; i++)
  7. a.rgba[i] = clamp(a.rgba[i], 0.f, 1.f);
  8. return a;
  9. }
  10. inline NVGcolor colorMinus(NVGcolor a, NVGcolor b) {
  11. for (int i = 0; i < 3; i++)
  12. a.rgba[i] -= b.rgba[i];
  13. return a;
  14. }
  15. inline NVGcolor colorPlus(NVGcolor a, NVGcolor b) {
  16. for (int i = 0; i < 3; i++)
  17. a.rgba[i] += b.rgba[i];
  18. return a;
  19. }
  20. inline NVGcolor colorMult(NVGcolor a, NVGcolor b) {
  21. for (int i = 0; i < 3; i++)
  22. a.rgba[i] *= b.rgba[i];
  23. return a;
  24. }
  25. inline NVGcolor colorMult(NVGcolor a, float x) {
  26. for (int i = 0; i < 3; i++)
  27. a.rgba[i] *= x;
  28. return a;
  29. }
  30. /** Screen blending with alpha compositing */
  31. inline NVGcolor colorScreen(NVGcolor a, NVGcolor b) {
  32. if (a.a == 0.0)
  33. return b;
  34. if (b.a == 0.0)
  35. return a;
  36. a = colorMult(a, a.a);
  37. b = colorMult(b, b.a);
  38. NVGcolor c = colorMinus(colorPlus(a, b), colorMult(a, b));
  39. c.a = a.a + b.a - a.a * b.a;
  40. c = colorMult(c, 1.f / c.a);
  41. c = colorClip(c);
  42. return c;
  43. }
  44. inline NVGcolor colorAlpha(NVGcolor a, float alpha) {
  45. a.a *= alpha;
  46. return a;
  47. }
  48. } // namespace rack