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.

221 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 isRadial_)
  29. : point1 (x1_, y1_),
  30. point2 (x2_, y2_),
  31. isRadial (isRadial_)
  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. const double pos = jlimit (0.0, 1.0, proportionAlongGradient);
  59. int i;
  60. for (i = 0; i < colours.size(); ++i)
  61. if (colours.getReference(i).position > pos)
  62. break;
  63. colours.insert (i, ColourPoint (pos, colour));
  64. return i;
  65. }
  66. void ColourGradient::removeColour (int index)
  67. {
  68. jassert (index > 0 && index < colours.size() - 1);
  69. colours.remove (index);
  70. }
  71. void ColourGradient::multiplyOpacity (const float multiplier) noexcept
  72. {
  73. for (int i = 0; i < colours.size(); ++i)
  74. {
  75. Colour& c = colours.getReference(i).colour;
  76. c = c.withMultipliedAlpha (multiplier);
  77. }
  78. }
  79. //==============================================================================
  80. int ColourGradient::getNumColours() const noexcept
  81. {
  82. return colours.size();
  83. }
  84. double ColourGradient::getColourPosition (const int index) const noexcept
  85. {
  86. if (isPositiveAndBelow (index, colours.size()))
  87. return colours.getReference (index).position;
  88. return 0;
  89. }
  90. Colour ColourGradient::getColour (const int index) const noexcept
  91. {
  92. if (isPositiveAndBelow (index, colours.size()))
  93. return colours.getReference (index).colour;
  94. return Colour();
  95. }
  96. void ColourGradient::setColour (int index, Colour newColour) noexcept
  97. {
  98. if (isPositiveAndBelow (index, colours.size()))
  99. colours.getReference (index).colour = newColour;
  100. }
  101. Colour ColourGradient::getColourAtPosition (const double position) const noexcept
  102. {
  103. jassert (colours.getReference(0).position == 0); // the first colour specified has to go at position 0
  104. if (position <= 0 || colours.size() <= 1)
  105. return colours.getReference(0).colour;
  106. int i = colours.size() - 1;
  107. while (position < colours.getReference(i).position)
  108. --i;
  109. const ColourPoint& p1 = colours.getReference (i);
  110. if (i >= colours.size() - 1)
  111. return p1.colour;
  112. const ColourPoint& p2 = colours.getReference (i + 1);
  113. return p1.colour.interpolatedWith (p2.colour, (float) ((position - p1.position) / (p2.position - p1.position)));
  114. }
  115. //==============================================================================
  116. void ColourGradient::createLookupTable (PixelARGB* const lookupTable, const int numEntries) const noexcept
  117. {
  118. JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED // Trying to use this object without setting its coordinates?
  119. jassert (colours.size() >= 2);
  120. jassert (numEntries > 0);
  121. jassert (colours.getReference(0).position == 0); // The first colour specified has to go at position 0
  122. PixelARGB pix1 (colours.getReference (0).colour.getPixelARGB());
  123. int index = 0;
  124. for (int j = 1; j < colours.size(); ++j)
  125. {
  126. const ColourPoint& p = colours.getReference (j);
  127. const int numToDo = roundToInt (p.position * (numEntries - 1)) - index;
  128. const PixelARGB pix2 (p.colour.getPixelARGB());
  129. for (int i = 0; i < numToDo; ++i)
  130. {
  131. jassert (index >= 0 && index < numEntries);
  132. lookupTable[index] = pix1;
  133. lookupTable[index].tween (pix2, (uint32) ((i << 8) / numToDo));
  134. ++index;
  135. }
  136. pix1 = pix2;
  137. }
  138. while (index < numEntries)
  139. lookupTable [index++] = pix1;
  140. }
  141. int ColourGradient::createLookupTable (const AffineTransform& transform, HeapBlock <PixelARGB>& lookupTable) const
  142. {
  143. JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED // Trying to use this object without setting its coordinates?
  144. jassert (colours.size() >= 2);
  145. const int numEntries = jlimit (1, jmax (1, (colours.size() - 1) << 8),
  146. 3 * (int) point1.transformedBy (transform)
  147. .getDistanceFrom (point2.transformedBy (transform)));
  148. lookupTable.malloc ((size_t) numEntries);
  149. createLookupTable (lookupTable, numEntries);
  150. return numEntries;
  151. }
  152. bool ColourGradient::isOpaque() const noexcept
  153. {
  154. for (int i = 0; i < colours.size(); ++i)
  155. if (! colours.getReference(i).colour.isOpaque())
  156. return false;
  157. return true;
  158. }
  159. bool ColourGradient::isInvisible() const noexcept
  160. {
  161. for (int i = 0; i < colours.size(); ++i)
  162. if (! colours.getReference(i).colour.isTransparent())
  163. return false;
  164. return true;
  165. }
  166. bool ColourGradient::ColourPoint::operator== (const ColourPoint& other) const noexcept
  167. {
  168. return position == other.position && colour == other.colour;
  169. }
  170. bool ColourGradient::ColourPoint::operator!= (const ColourPoint& other) const noexcept
  171. {
  172. return position != other.position || colour != other.colour;
  173. }