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.

218 lines
8.6KB

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