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.

70 lines
1.2KB

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