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.

225 lines
8.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Describes the layout and colours that should be used to paint a colour gradient.
  23. @see Graphics::setGradientFill
  24. @tags{Graphics}
  25. */
  26. class JUCE_API ColourGradient final
  27. {
  28. public:
  29. /** Creates an uninitialised gradient.
  30. If you use this constructor instead of the other one, be sure to set all the
  31. object's public member variables before using it!
  32. */
  33. ColourGradient() noexcept;
  34. ColourGradient (const ColourGradient&);
  35. ColourGradient (ColourGradient&&) noexcept;
  36. ColourGradient& operator= (const ColourGradient&);
  37. ColourGradient& operator= (ColourGradient&&) noexcept;
  38. //==============================================================================
  39. /** Creates a gradient object.
  40. (x1, y1) is the location to draw with colour1. Likewise (x2, y2) is where
  41. colour2 should be. In between them there's a gradient.
  42. If isRadial is true, the colours form a circular gradient with (x1, y1) at
  43. its centre.
  44. The alpha transparencies of the colours are used, so note that
  45. if you blend from transparent to a solid colour, the RGB of the transparent
  46. colour will become visible in parts of the gradient. e.g. blending
  47. from Colour::transparentBlack to Colours::white will produce a
  48. muddy grey colour midway, but Colour::transparentWhite to Colours::white
  49. will be white all the way across.
  50. @see ColourGradient
  51. */
  52. ColourGradient (Colour colour1, float x1, float y1,
  53. Colour colour2, float x2, float y2,
  54. bool isRadial);
  55. /** Creates a gradient object.
  56. point1 is the location to draw with colour1. Likewise point2 is where
  57. colour2 should be. In between them there's a gradient.
  58. If isRadial is true, the colours form a circular gradient with point1 at
  59. its centre.
  60. The alpha transparencies of the colours are used, so note that
  61. if you blend from transparent to a solid colour, the RGB of the transparent
  62. colour will become visible in parts of the gradient. e.g. blending
  63. from Colour::transparentBlack to Colours::white will produce a
  64. muddy grey colour midway, but Colour::transparentWhite to Colours::white
  65. will be white all the way across.
  66. @see ColourGradient
  67. */
  68. ColourGradient (Colour colour1, Point<float> point1,
  69. Colour colour2, Point<float> point2,
  70. bool isRadial);
  71. //==============================================================================
  72. /** Creates a vertical linear gradient between two Y coordinates */
  73. static ColourGradient vertical (Colour colour1, float y1,
  74. Colour colour2, float y2);
  75. /** Creates a horizontal linear gradient between two X coordinates */
  76. static ColourGradient horizontal (Colour colour1, float x1,
  77. Colour colour2, float x2);
  78. /** Creates a vertical linear gradient from top to bottom in a rectangle */
  79. template <typename Type>
  80. static ColourGradient vertical (Colour colourTop, Colour colourBottom, Rectangle<Type> area)
  81. {
  82. return vertical (colourTop, (float) area.getY(), colourBottom, (float) area.getBottom());
  83. }
  84. /** Creates a horizontal linear gradient from right to left in a rectangle */
  85. template <typename Type>
  86. static ColourGradient horizontal (Colour colourLeft, Colour colourRight, Rectangle<Type> area)
  87. {
  88. return horizontal (colourLeft, (float) area.getX(), colourRight, (float) area.getRight());
  89. }
  90. /** Destructor */
  91. ~ColourGradient();
  92. //==============================================================================
  93. /** Removes any colours that have been added.
  94. This will also remove any start and end colours, so the gradient won't work. You'll
  95. need to add more colours with addColour().
  96. */
  97. void clearColours();
  98. /** Adds a colour at a point along the length of the gradient.
  99. This allows the gradient to go through a spectrum of colours, instead of just a
  100. start and end colour.
  101. @param proportionAlongGradient a value between 0 and 1.0, which is the proportion
  102. of the distance along the line between the two points
  103. at which the colour should occur.
  104. @param colour the colour that should be used at this point
  105. @returns the index at which the new point was added
  106. */
  107. int addColour (double proportionAlongGradient, Colour colour);
  108. /** Removes one of the colours from the gradient. */
  109. void removeColour (int index);
  110. /** Multiplies the alpha value of all the colours by the given scale factor */
  111. void multiplyOpacity (float multiplier) noexcept;
  112. //==============================================================================
  113. /** Returns the number of colour-stops that have been added. */
  114. int getNumColours() const noexcept;
  115. /** Returns the position along the length of the gradient of the colour with this index.
  116. The index is from 0 to getNumColours() - 1. The return value will be between 0.0 and 1.0
  117. */
  118. double getColourPosition (int index) const noexcept;
  119. /** Returns the colour that was added with a given index.
  120. The index is from 0 to getNumColours() - 1.
  121. */
  122. Colour getColour (int index) const noexcept;
  123. /** Changes the colour at a given index.
  124. The index is from 0 to getNumColours() - 1.
  125. */
  126. void setColour (int index, Colour newColour) noexcept;
  127. /** Returns the an interpolated colour at any position along the gradient.
  128. @param position the position along the gradient, between 0 and 1
  129. */
  130. Colour getColourAtPosition (double position) const noexcept;
  131. //==============================================================================
  132. /** Creates a set of interpolated premultiplied ARGB values.
  133. This will resize the HeapBlock, fill it with the colours, and will return the number of
  134. colours that it added.
  135. When calling this, the ColourGradient must have at least 2 colour stops specified.
  136. */
  137. int createLookupTable (const AffineTransform& transform, HeapBlock<PixelARGB>& resultLookupTable) const;
  138. /** Creates a set of interpolated premultiplied ARGB values.
  139. This will fill an array of a user-specified size with the gradient, interpolating to fit.
  140. The numEntries argument specifies the size of the array, and this size must be greater than zero.
  141. When calling this, the ColourGradient must have at least 2 colour stops specified.
  142. */
  143. void createLookupTable (PixelARGB* resultLookupTable, int numEntries) const noexcept;
  144. /** Returns true if all colours are opaque. */
  145. bool isOpaque() const noexcept;
  146. /** Returns true if all colours are completely transparent. */
  147. bool isInvisible() const noexcept;
  148. //==============================================================================
  149. Point<float> point1, point2;
  150. /** If true, the gradient should be filled circularly, centred around
  151. point1, with point2 defining a point on the circumference.
  152. If false, the gradient is linear between the two points.
  153. */
  154. bool isRadial;
  155. bool operator== (const ColourGradient&) const noexcept;
  156. bool operator!= (const ColourGradient&) const noexcept;
  157. private:
  158. //==============================================================================
  159. struct ColourPoint
  160. {
  161. bool operator== (ColourPoint) const noexcept;
  162. bool operator!= (ColourPoint) const noexcept;
  163. double position;
  164. Colour colour;
  165. };
  166. Array<ColourPoint> colours;
  167. JUCE_LEAK_DETECTOR (ColourGradient)
  168. };
  169. } // namespace juce