Audio plugin host https://kx.studio/carla
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.

166 lines
4.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DGL_COLOR_HPP_INCLUDED
  17. #define DGL_COLOR_HPP_INCLUDED
  18. #include "Base.hpp"
  19. struct NVGcolor;
  20. START_NAMESPACE_DGL
  21. // --------------------------------------------------------------------------------------------------------------------
  22. /**
  23. A color made from red, green, blue and alpha floating-point values in [0..1] range.
  24. */
  25. struct Color {
  26. /**
  27. Direct access to the color values.
  28. */
  29. union {
  30. float rgba[4];
  31. struct { float red, green, blue, alpha; };
  32. };
  33. /**
  34. Create solid black color.
  35. */
  36. Color() noexcept;
  37. /**
  38. Create a color from red, green, blue and alpha numeric values.
  39. All values except alpha must be in [0..255] range, with alpha in [0..1] range.
  40. */
  41. Color(int red, int green, int blue, float alpha = 1.0f) noexcept;
  42. /**
  43. Create a color from red, green, blue and alpha floating-point values.
  44. All values must in [0..1] range.
  45. */
  46. Color(float red, float green, float blue, float alpha = 1.0f) noexcept;
  47. /**
  48. Create a color by copying another color.
  49. */
  50. Color(const Color& color) noexcept;
  51. Color& operator=(const Color& color) noexcept;
  52. /**
  53. Create a color by linearly interpolating two other colors.
  54. */
  55. Color(const Color& color1, const Color& color2, float u) noexcept;
  56. /**
  57. Create a new color based on this one but with a different alpha value.
  58. */
  59. Color withAlpha(float alpha) const noexcept;
  60. /**
  61. Create a new color based on this one but with subtracted numeric value on all elements.
  62. Value must be in [0..255] range.
  63. */
  64. Color minus(int value) const noexcept;
  65. /**
  66. Create a new color based on this one but with subtracted floating-point value on all elements.
  67. Value must be in [0..1] range.
  68. */
  69. Color minus(float value) const noexcept;
  70. /**
  71. Create a new color based on this one but with added numeric value on all elements.
  72. Value must be in [0..255] range.
  73. */
  74. Color plus(int value) const noexcept;
  75. /**
  76. Create a new color based on this one but with added floating-point value on all elements.
  77. Value must be in [0..1] range.
  78. */
  79. Color plus(float value) const noexcept;
  80. /**
  81. Create a new color based on this one but colors inverted.
  82. */
  83. Color invert() const noexcept;
  84. /**
  85. Create a new color based on this one but in grayscale (using weighted average).
  86. */
  87. Color asGrayscale() const noexcept;
  88. /**
  89. Create a color specified by hue, saturation and lightness.
  90. Values must in [0..1] range.
  91. */
  92. static Color fromHSL(float hue, float saturation, float lightness, float alpha = 1.0f);
  93. /**
  94. Create a color from a HTML string like "#333" or "#112233".
  95. */
  96. static Color fromHTML(const char* rgb, float alpha = 1.0f) noexcept;
  97. /**
  98. Create a color from a RGB unsigned integer.
  99. Basically doing:
  100. ```
  101. uint8_t red = (color >> 24) & 0xff;
  102. uint8_t green = (color >> 16) & 0xff;
  103. uint8_t blue = (color >> 8) & 0xff;
  104. ```
  105. */
  106. static Color fromRGB(uint color, float alpha = 1.0f) noexcept;
  107. /**
  108. Linearly interpolate this color against another.
  109. */
  110. void interpolate(const Color& other, float u) noexcept;
  111. /**
  112. Check if this color matches another.
  113. @note Comparison is done within 8-bit color space.
  114. */
  115. bool isEqual(const Color& color, bool withAlpha = true) noexcept;
  116. bool isNotEqual(const Color& color, bool withAlpha = true) noexcept;
  117. bool operator==(const Color& color) noexcept;
  118. bool operator!=(const Color& color) noexcept;
  119. /**
  120. Fix color bounds if needed.
  121. */
  122. void fixBounds() noexcept;
  123. /**
  124. Set this color for use in the next drawing operation for the provided context.
  125. */
  126. void setFor(const GraphicsContext& context, bool includeAlpha = false);
  127. /**
  128. @internal
  129. Needed for NanoVG compatibility.
  130. */
  131. Color(const NVGcolor&) noexcept;
  132. operator NVGcolor() const noexcept;
  133. };
  134. // --------------------------------------------------------------------------------------------------------------------
  135. END_NAMESPACE_DGL
  136. #endif // DGL_COLOR_HPP_INCLUDED