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.

240 lines
7.3KB

  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. ColourGradient::ColourGradient() noexcept
  20. {
  21. #if JUCE_DEBUG
  22. point1.setX (987654.0f);
  23. #define JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED jassert (point1.x != 987654.0f);
  24. #else
  25. #define JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED
  26. #endif
  27. }
  28. ColourGradient::ColourGradient (Colour colour1, const float x1, const float y1,
  29. Colour colour2, const float x2, const float y2,
  30. const bool radial)
  31. : point1 (x1, y1),
  32. point2 (x2, y2),
  33. isRadial (radial)
  34. {
  35. colours.add (ColourPoint (0.0, colour1));
  36. colours.add (ColourPoint (1.0, colour2));
  37. }
  38. ColourGradient::ColourGradient (Colour colour1, Point<float> p1,
  39. Colour colour2, Point<float> p2,
  40. const bool radial)
  41. : point1 (p1),
  42. point2 (p2),
  43. isRadial (radial)
  44. {
  45. colours.add (ColourPoint (0.0, colour1));
  46. colours.add (ColourPoint (1.0, colour2));
  47. }
  48. ColourGradient::~ColourGradient()
  49. {
  50. }
  51. bool ColourGradient::operator== (const ColourGradient& other) const noexcept
  52. {
  53. return point1 == other.point1 && point2 == other.point2
  54. && isRadial == other.isRadial
  55. && colours == other.colours;
  56. }
  57. bool ColourGradient::operator!= (const ColourGradient& other) const noexcept
  58. {
  59. return ! operator== (other);
  60. }
  61. //==============================================================================
  62. void ColourGradient::clearColours()
  63. {
  64. colours.clear();
  65. }
  66. int ColourGradient::addColour (const double proportionAlongGradient, Colour colour)
  67. {
  68. // must be within the two end-points
  69. jassert (proportionAlongGradient >= 0 && proportionAlongGradient <= 1.0);
  70. if (proportionAlongGradient <= 0)
  71. {
  72. colours.set (0, ColourPoint (0.0, colour));
  73. return 0;
  74. }
  75. const double pos = jmin (1.0, proportionAlongGradient);
  76. int i;
  77. for (i = 0; i < colours.size(); ++i)
  78. if (colours.getReference(i).position > pos)
  79. break;
  80. colours.insert (i, ColourPoint (pos, colour));
  81. return i;
  82. }
  83. void ColourGradient::removeColour (int index)
  84. {
  85. jassert (index > 0 && index < colours.size() - 1);
  86. colours.remove (index);
  87. }
  88. void ColourGradient::multiplyOpacity (const float multiplier) noexcept
  89. {
  90. for (int i = 0; i < colours.size(); ++i)
  91. {
  92. Colour& c = colours.getReference(i).colour;
  93. c = c.withMultipliedAlpha (multiplier);
  94. }
  95. }
  96. //==============================================================================
  97. int ColourGradient::getNumColours() const noexcept
  98. {
  99. return colours.size();
  100. }
  101. double ColourGradient::getColourPosition (const int index) const noexcept
  102. {
  103. if (isPositiveAndBelow (index, colours.size()))
  104. return colours.getReference (index).position;
  105. return 0;
  106. }
  107. Colour ColourGradient::getColour (const int index) const noexcept
  108. {
  109. if (isPositiveAndBelow (index, colours.size()))
  110. return colours.getReference (index).colour;
  111. return Colour();
  112. }
  113. void ColourGradient::setColour (int index, Colour newColour) noexcept
  114. {
  115. if (isPositiveAndBelow (index, colours.size()))
  116. colours.getReference (index).colour = newColour;
  117. }
  118. Colour ColourGradient::getColourAtPosition (const double position) const noexcept
  119. {
  120. jassert (colours.getReference(0).position == 0.0); // the first colour specified has to go at position 0
  121. if (position <= 0 || colours.size() <= 1)
  122. return colours.getReference(0).colour;
  123. int i = colours.size() - 1;
  124. while (position < colours.getReference(i).position)
  125. --i;
  126. auto& p1 = colours.getReference (i);
  127. if (i >= colours.size() - 1)
  128. return p1.colour;
  129. auto& p2 = colours.getReference (i + 1);
  130. return p1.colour.interpolatedWith (p2.colour, (float) ((position - p1.position) / (p2.position - p1.position)));
  131. }
  132. //==============================================================================
  133. void ColourGradient::createLookupTable (PixelARGB* const lookupTable, const int numEntries) const noexcept
  134. {
  135. JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED // Trying to use this object without setting its coordinates?
  136. jassert (colours.size() >= 2);
  137. jassert (numEntries > 0);
  138. jassert (colours.getReference(0).position == 0.0); // The first colour specified has to go at position 0
  139. PixelARGB pix1 (colours.getReference (0).colour.getPixelARGB());
  140. int index = 0;
  141. for (int j = 1; j < colours.size(); ++j)
  142. {
  143. const ColourPoint& p = colours.getReference (j);
  144. const int numToDo = roundToInt (p.position * (numEntries - 1)) - index;
  145. const PixelARGB pix2 (p.colour.getPixelARGB());
  146. for (int i = 0; i < numToDo; ++i)
  147. {
  148. jassert (index >= 0 && index < numEntries);
  149. lookupTable[index] = pix1;
  150. lookupTable[index].tween (pix2, (uint32) ((i << 8) / numToDo));
  151. ++index;
  152. }
  153. pix1 = pix2;
  154. }
  155. while (index < numEntries)
  156. lookupTable [index++] = pix1;
  157. }
  158. int ColourGradient::createLookupTable (const AffineTransform& transform, HeapBlock<PixelARGB>& lookupTable) const
  159. {
  160. JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED // Trying to use this object without setting its coordinates?
  161. jassert (colours.size() >= 2);
  162. const int numEntries = jlimit (1, jmax (1, (colours.size() - 1) << 8),
  163. 3 * (int) point1.transformedBy (transform)
  164. .getDistanceFrom (point2.transformedBy (transform)));
  165. lookupTable.malloc ((size_t) numEntries);
  166. createLookupTable (lookupTable, numEntries);
  167. return numEntries;
  168. }
  169. bool ColourGradient::isOpaque() const noexcept
  170. {
  171. for (int i = 0; i < colours.size(); ++i)
  172. if (! colours.getReference(i).colour.isOpaque())
  173. return false;
  174. return true;
  175. }
  176. bool ColourGradient::isInvisible() const noexcept
  177. {
  178. for (int i = 0; i < colours.size(); ++i)
  179. if (! colours.getReference(i).colour.isTransparent())
  180. return false;
  181. return true;
  182. }
  183. bool ColourGradient::ColourPoint::operator== (const ColourPoint& other) const noexcept
  184. {
  185. return position == other.position && colour == other.colour;
  186. }
  187. bool ColourGradient::ColourPoint::operator!= (const ColourPoint& other) const noexcept
  188. {
  189. return position != other.position || colour != other.colour;
  190. }