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.

237 lines
7.0KB

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