The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

264 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. ColourGradient::ColourGradient() noexcept : isRadial (false)
  16. {
  17. #if JUCE_DEBUG
  18. point1.setX (987654.0f);
  19. #define JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED jassert (point1.x != 987654.0f);
  20. #else
  21. #define JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED
  22. #endif
  23. }
  24. ColourGradient::ColourGradient (const ColourGradient& other)
  25. : point1 (other.point1), point2 (other.point2), isRadial (other.isRadial), colours (other.colours)
  26. {}
  27. ColourGradient::ColourGradient (ColourGradient&& other) noexcept
  28. : point1 (other.point1), point2 (other.point2), isRadial (other.isRadial),
  29. colours (std::move (other.colours))
  30. {}
  31. ColourGradient& ColourGradient::operator= (const ColourGradient& other)
  32. {
  33. point1 = other.point1;
  34. point2 = other.point2;
  35. isRadial = other.isRadial;
  36. colours = other.colours;
  37. return *this;
  38. }
  39. ColourGradient& ColourGradient::operator= (ColourGradient&& other) noexcept
  40. {
  41. point1 = other.point1;
  42. point2 = other.point2;
  43. isRadial = other.isRadial;
  44. colours = std::move (other.colours);
  45. return *this;
  46. }
  47. ColourGradient::ColourGradient (Colour colour1, float x1, float y1,
  48. Colour colour2, float x2, float y2, bool radial)
  49. : ColourGradient (colour1, Point<float> (x1, y1),
  50. colour2, Point<float> (x2, y2), radial)
  51. {
  52. }
  53. ColourGradient::ColourGradient (Colour colour1, Point<float> p1,
  54. Colour colour2, Point<float> p2, bool radial)
  55. : point1 (p1),
  56. point2 (p2),
  57. isRadial (radial)
  58. {
  59. colours.add (ColourPoint { 0.0, colour1 },
  60. ColourPoint { 1.0, colour2 });
  61. }
  62. ColourGradient::~ColourGradient() {}
  63. ColourGradient ColourGradient::vertical (Colour c1, float y1, Colour c2, float y2)
  64. {
  65. return { c1, 0, y1, c2, 0, y2, false };
  66. }
  67. ColourGradient ColourGradient::horizontal (Colour c1, float x1, Colour c2, float x2)
  68. {
  69. return { c1, x1, 0, c2, x2, 0, false };
  70. }
  71. bool ColourGradient::operator== (const ColourGradient& other) const noexcept
  72. {
  73. return point1 == other.point1 && point2 == other.point2
  74. && isRadial == other.isRadial
  75. && colours == other.colours;
  76. }
  77. bool ColourGradient::operator!= (const ColourGradient& other) const noexcept
  78. {
  79. return ! operator== (other);
  80. }
  81. //==============================================================================
  82. void ColourGradient::clearColours()
  83. {
  84. colours.clear();
  85. }
  86. int ColourGradient::addColour (const double proportionAlongGradient, Colour colour)
  87. {
  88. // must be within the two end-points
  89. jassert (proportionAlongGradient >= 0 && proportionAlongGradient <= 1.0);
  90. if (proportionAlongGradient <= 0)
  91. {
  92. colours.set (0, { 0.0, colour });
  93. return 0;
  94. }
  95. auto pos = jmin (1.0, proportionAlongGradient);
  96. int i;
  97. for (i = 0; i < colours.size(); ++i)
  98. if (colours.getReference(i).position > pos)
  99. break;
  100. colours.insert (i, { pos, colour });
  101. return i;
  102. }
  103. void ColourGradient::removeColour (int index)
  104. {
  105. jassert (index > 0 && index < colours.size() - 1);
  106. colours.remove (index);
  107. }
  108. void ColourGradient::multiplyOpacity (const float multiplier) noexcept
  109. {
  110. for (auto& c : colours)
  111. c.colour = c.colour.withMultipliedAlpha (multiplier);
  112. }
  113. //==============================================================================
  114. int ColourGradient::getNumColours() const noexcept
  115. {
  116. return colours.size();
  117. }
  118. double ColourGradient::getColourPosition (int index) const noexcept
  119. {
  120. if (isPositiveAndBelow (index, colours.size()))
  121. return colours.getReference (index).position;
  122. return 0;
  123. }
  124. Colour ColourGradient::getColour (int index) const noexcept
  125. {
  126. if (isPositiveAndBelow (index, colours.size()))
  127. return colours.getReference (index).colour;
  128. return {};
  129. }
  130. void ColourGradient::setColour (int index, Colour newColour) noexcept
  131. {
  132. if (isPositiveAndBelow (index, colours.size()))
  133. colours.getReference (index).colour = newColour;
  134. }
  135. Colour ColourGradient::getColourAtPosition (double position) const noexcept
  136. {
  137. jassert (colours.getReference(0).position == 0.0); // the first colour specified has to go at position 0
  138. if (position <= 0 || colours.size() <= 1)
  139. return colours.getReference(0).colour;
  140. int i = colours.size() - 1;
  141. while (position < colours.getReference(i).position)
  142. --i;
  143. auto& p1 = colours.getReference (i);
  144. if (i >= colours.size() - 1)
  145. return p1.colour;
  146. auto& p2 = colours.getReference (i + 1);
  147. return p1.colour.interpolatedWith (p2.colour, (float) ((position - p1.position) / (p2.position - p1.position)));
  148. }
  149. //==============================================================================
  150. void ColourGradient::createLookupTable (PixelARGB* const lookupTable, const int numEntries) const noexcept
  151. {
  152. JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED // Trying to use this object without setting its coordinates?
  153. jassert (colours.size() >= 2);
  154. jassert (numEntries > 0);
  155. jassert (colours.getReference(0).position == 0.0); // The first colour specified has to go at position 0
  156. auto pix1 = colours.getReference (0).colour.getPixelARGB();
  157. int index = 0;
  158. for (int j = 1; j < colours.size(); ++j)
  159. {
  160. auto& p = colours.getReference (j);
  161. auto numToDo = roundToInt (p.position * (numEntries - 1)) - index;
  162. auto pix2 = p.colour.getPixelARGB();
  163. for (int i = 0; i < numToDo; ++i)
  164. {
  165. jassert (index >= 0 && index < numEntries);
  166. lookupTable[index] = pix1;
  167. lookupTable[index].tween (pix2, (uint32) ((i << 8) / numToDo));
  168. ++index;
  169. }
  170. pix1 = pix2;
  171. }
  172. while (index < numEntries)
  173. lookupTable [index++] = pix1;
  174. }
  175. int ColourGradient::createLookupTable (const AffineTransform& transform, HeapBlock<PixelARGB>& lookupTable) const
  176. {
  177. JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED // Trying to use this object without setting its coordinates?
  178. jassert (colours.size() >= 2);
  179. auto numEntries = jlimit (1, jmax (1, (colours.size() - 1) << 8),
  180. 3 * (int) point1.transformedBy (transform)
  181. .getDistanceFrom (point2.transformedBy (transform)));
  182. lookupTable.malloc (numEntries);
  183. createLookupTable (lookupTable, numEntries);
  184. return numEntries;
  185. }
  186. bool ColourGradient::isOpaque() const noexcept
  187. {
  188. for (auto& c : colours)
  189. if (! c.colour.isOpaque())
  190. return false;
  191. return true;
  192. }
  193. bool ColourGradient::isInvisible() const noexcept
  194. {
  195. for (auto& c : colours)
  196. if (! c.colour.isTransparent())
  197. return false;
  198. return true;
  199. }
  200. bool ColourGradient::ColourPoint::operator== (ColourPoint other) const noexcept
  201. {
  202. return position == other.position && colour == other.colour;
  203. }
  204. bool ColourGradient::ColourPoint::operator!= (ColourPoint other) const noexcept
  205. {
  206. return position != other.position || colour != other.colour;
  207. }
  208. } // namespace juce