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.

227 lines
7.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. ColourGradient::ColourGradient() noexcept
  18. {
  19. #if JUCE_DEBUG
  20. point1.setX (987654.0f);
  21. #define JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED jassert (point1.x != 987654.0f);
  22. #else
  23. #define JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED
  24. #endif
  25. }
  26. ColourGradient::ColourGradient (Colour colour1, const float x1, const float y1,
  27. Colour colour2, const float x2, const float y2,
  28. const bool radial)
  29. : point1 (x1, y1),
  30. point2 (x2, y2),
  31. isRadial (radial)
  32. {
  33. colours.add (ColourPoint (0.0, colour1));
  34. colours.add (ColourPoint (1.0, colour2));
  35. }
  36. ColourGradient::~ColourGradient()
  37. {
  38. }
  39. bool ColourGradient::operator== (const ColourGradient& other) const noexcept
  40. {
  41. return point1 == other.point1 && point2 == other.point2
  42. && isRadial == other.isRadial
  43. && colours == other.colours;
  44. }
  45. bool ColourGradient::operator!= (const ColourGradient& other) const noexcept
  46. {
  47. return ! operator== (other);
  48. }
  49. //==============================================================================
  50. void ColourGradient::clearColours()
  51. {
  52. colours.clear();
  53. }
  54. int ColourGradient::addColour (const double proportionAlongGradient, Colour colour)
  55. {
  56. // must be within the two end-points
  57. jassert (proportionAlongGradient >= 0 && proportionAlongGradient <= 1.0);
  58. if (proportionAlongGradient <= 0)
  59. {
  60. colours.set (0, ColourPoint (0.0, colour));
  61. return 0;
  62. }
  63. const double pos = jmin (1.0, proportionAlongGradient);
  64. int i;
  65. for (i = 0; i < colours.size(); ++i)
  66. if (colours.getReference(i).position > pos)
  67. break;
  68. colours.insert (i, ColourPoint (pos, colour));
  69. return i;
  70. }
  71. void ColourGradient::removeColour (int index)
  72. {
  73. jassert (index > 0 && index < colours.size() - 1);
  74. colours.remove (index);
  75. }
  76. void ColourGradient::multiplyOpacity (const float multiplier) noexcept
  77. {
  78. for (int i = 0; i < colours.size(); ++i)
  79. {
  80. Colour& c = colours.getReference(i).colour;
  81. c = c.withMultipliedAlpha (multiplier);
  82. }
  83. }
  84. //==============================================================================
  85. int ColourGradient::getNumColours() const noexcept
  86. {
  87. return colours.size();
  88. }
  89. double ColourGradient::getColourPosition (const int index) const noexcept
  90. {
  91. if (isPositiveAndBelow (index, colours.size()))
  92. return colours.getReference (index).position;
  93. return 0;
  94. }
  95. Colour ColourGradient::getColour (const int index) const noexcept
  96. {
  97. if (isPositiveAndBelow (index, colours.size()))
  98. return colours.getReference (index).colour;
  99. return Colour();
  100. }
  101. void ColourGradient::setColour (int index, Colour newColour) noexcept
  102. {
  103. if (isPositiveAndBelow (index, colours.size()))
  104. colours.getReference (index).colour = newColour;
  105. }
  106. Colour ColourGradient::getColourAtPosition (const double position) const noexcept
  107. {
  108. jassert (colours.getReference(0).position == 0); // the first colour specified has to go at position 0
  109. if (position <= 0 || colours.size() <= 1)
  110. return colours.getReference(0).colour;
  111. int i = colours.size() - 1;
  112. while (position < colours.getReference(i).position)
  113. --i;
  114. const ColourPoint& p1 = colours.getReference (i);
  115. if (i >= colours.size() - 1)
  116. return p1.colour;
  117. const ColourPoint& p2 = colours.getReference (i + 1);
  118. return p1.colour.interpolatedWith (p2.colour, (float) ((position - p1.position) / (p2.position - p1.position)));
  119. }
  120. //==============================================================================
  121. void ColourGradient::createLookupTable (PixelARGB* const lookupTable, const int numEntries) const noexcept
  122. {
  123. JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED // Trying to use this object without setting its coordinates?
  124. jassert (colours.size() >= 2);
  125. jassert (numEntries > 0);
  126. jassert (colours.getReference(0).position == 0); // The first colour specified has to go at position 0
  127. PixelARGB pix1 (colours.getReference (0).colour.getPixelARGB());
  128. int index = 0;
  129. for (int j = 1; j < colours.size(); ++j)
  130. {
  131. const ColourPoint& p = colours.getReference (j);
  132. const int numToDo = roundToInt (p.position * (numEntries - 1)) - index;
  133. const PixelARGB pix2 (p.colour.getPixelARGB());
  134. for (int i = 0; i < numToDo; ++i)
  135. {
  136. jassert (index >= 0 && index < numEntries);
  137. lookupTable[index] = pix1;
  138. lookupTable[index].tween (pix2, (uint32) ((i << 8) / numToDo));
  139. ++index;
  140. }
  141. pix1 = pix2;
  142. }
  143. while (index < numEntries)
  144. lookupTable [index++] = pix1;
  145. }
  146. int ColourGradient::createLookupTable (const AffineTransform& transform, HeapBlock<PixelARGB>& lookupTable) const
  147. {
  148. JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED // Trying to use this object without setting its coordinates?
  149. jassert (colours.size() >= 2);
  150. const int numEntries = jlimit (1, jmax (1, (colours.size() - 1) << 8),
  151. 3 * (int) point1.transformedBy (transform)
  152. .getDistanceFrom (point2.transformedBy (transform)));
  153. lookupTable.malloc ((size_t) numEntries);
  154. createLookupTable (lookupTable, numEntries);
  155. return numEntries;
  156. }
  157. bool ColourGradient::isOpaque() const noexcept
  158. {
  159. for (int i = 0; i < colours.size(); ++i)
  160. if (! colours.getReference(i).colour.isOpaque())
  161. return false;
  162. return true;
  163. }
  164. bool ColourGradient::isInvisible() const noexcept
  165. {
  166. for (int i = 0; i < colours.size(); ++i)
  167. if (! colours.getReference(i).colour.isTransparent())
  168. return false;
  169. return true;
  170. }
  171. bool ColourGradient::ColourPoint::operator== (const ColourPoint& other) const noexcept
  172. {
  173. return position == other.position && colour == other.colour;
  174. }
  175. bool ColourGradient::ColourPoint::operator!= (const ColourPoint& other) const noexcept
  176. {
  177. return position != other.position || colour != other.colour;
  178. }