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.

98 lines
2.2KB

  1. #pragma once
  2. #include "util/common.hpp"
  3. #include "nanovg.h"
  4. namespace rack {
  5. namespace color {
  6. static const NVGcolor BLACK_TRANSPARENT = nvgRGBA(0x00, 0x00, 0x00, 0x00);
  7. static const NVGcolor BLACK = nvgRGB(0x00, 0x00, 0x00);
  8. static const NVGcolor WHITE = nvgRGB(0xff, 0xff, 0xff);
  9. static const NVGcolor WHITE_TRANSPARENT = nvgRGB(0xff, 0xff, 0xff);
  10. static const NVGcolor RED = nvgRGB(0xff, 0x00, 0x00);
  11. static const NVGcolor GREEN = nvgRGB(0x00, 0xff, 0x00);
  12. static const NVGcolor BLUE = nvgRGB(0x00, 0x00, 0xff);
  13. static const NVGcolor YELLOW = nvgRGB(0xff, 0xff, 0x00);
  14. static const NVGcolor MAGENTA = nvgRGB(0xff, 0x00, 0xff);
  15. static const NVGcolor CYAN = nvgRGB(0x00, 0xff, 0xff);
  16. inline NVGcolor clip(NVGcolor a) {
  17. for (int i = 0; i < 4; i++)
  18. a.rgba[i] = math::clamp(a.rgba[i], 0.f, 1.f);
  19. return a;
  20. }
  21. inline NVGcolor minus(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 plus(NVGcolor a, NVGcolor b) {
  27. for (int i = 0; i < 3; i++)
  28. a.rgba[i] += b.rgba[i];
  29. return a;
  30. }
  31. inline NVGcolor mult(NVGcolor a, NVGcolor b) {
  32. for (int i = 0; i < 3; i++)
  33. a.rgba[i] *= b.rgba[i];
  34. return a;
  35. }
  36. inline NVGcolor mult(NVGcolor a, float x) {
  37. for (int i = 0; i < 3; i++)
  38. a.rgba[i] *= x;
  39. return a;
  40. }
  41. /** Screen blending with alpha compositing */
  42. inline NVGcolor screen(NVGcolor a, NVGcolor b) {
  43. if (a.a == 0.0)
  44. return b;
  45. if (b.a == 0.0)
  46. return a;
  47. a = mult(a, a.a);
  48. b = mult(b, b.a);
  49. NVGcolor c = minus(plus(a, b), mult(a, b));
  50. c.a = a.a + b.a - a.a * b.a;
  51. c = mult(c, 1.f / c.a);
  52. c = clip(c);
  53. return c;
  54. }
  55. inline NVGcolor alpha(NVGcolor a, float alpha) {
  56. a.a *= alpha;
  57. return a;
  58. }
  59. inline NVGcolor fromHexString(std::string s) {
  60. uint8_t r = 0;
  61. uint8_t g = 0;
  62. uint8_t b = 0;
  63. uint8_t a = 255;
  64. sscanf(s.c_str(), "#%2hhx%2hhx%2hhx%2hhx", &r, &g, &b, &a);
  65. return nvgRGBA(r, g, b, a);
  66. }
  67. inline std::string toHexString(NVGcolor c) {
  68. uint8_t r = std::round(c.r * 255);
  69. uint8_t g = std::round(c.g * 255);
  70. uint8_t b = std::round(c.b * 255);
  71. uint8_t a = std::round(c.a * 255);
  72. if (a == 255)
  73. return stringf("#%02x%02x%02x", r, g, b);
  74. else
  75. return stringf("#%02x%02x%02x%02x", r, g, b, a);
  76. }
  77. } // namespace color
  78. } // namespace rack