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.

182 lines
7.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_COLOURGRADIENT_H_INCLUDED
  18. #define JUCE_COLOURGRADIENT_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Describes the layout and colours that should be used to paint a colour gradient.
  22. @see Graphics::setGradientFill
  23. */
  24. class JUCE_API ColourGradient
  25. {
  26. public:
  27. //==============================================================================
  28. /** Creates a gradient object.
  29. (x1, y1) is the location to draw with colour1. Likewise (x2, y2) is where
  30. colour2 should be. In between them there's a gradient.
  31. If isRadial is true, the colours form a circular gradient with (x1, y1) at
  32. its centre.
  33. The alpha transparencies of the colours are used, so note that
  34. if you blend from transparent to a solid colour, the RGB of the transparent
  35. colour will become visible in parts of the gradient. e.g. blending
  36. from Colour::transparentBlack to Colours::white will produce a
  37. muddy grey colour midway, but Colour::transparentWhite to Colours::white
  38. will be white all the way across.
  39. @see ColourGradient
  40. */
  41. ColourGradient (Colour colour1, float x1, float y1,
  42. Colour colour2, float x2, float y2,
  43. bool isRadial);
  44. /** Creates an uninitialised gradient.
  45. If you use this constructor instead of the other one, be sure to set all the
  46. object's public member variables before using it!
  47. */
  48. ColourGradient() noexcept;
  49. /** Destructor */
  50. ~ColourGradient();
  51. //==============================================================================
  52. /** Removes any colours that have been added.
  53. This will also remove any start and end colours, so the gradient won't work. You'll
  54. need to add more colours with addColour().
  55. */
  56. void clearColours();
  57. /** Adds a colour at a point along the length of the gradient.
  58. This allows the gradient to go through a spectrum of colours, instead of just a
  59. start and end colour.
  60. @param proportionAlongGradient a value between 0 and 1.0, which is the proportion
  61. of the distance along the line between the two points
  62. at which the colour should occur.
  63. @param colour the colour that should be used at this point
  64. @returns the index at which the new point was added
  65. */
  66. int addColour (double proportionAlongGradient,
  67. Colour colour);
  68. /** Removes one of the colours from the gradient. */
  69. void removeColour (int index);
  70. /** Multiplies the alpha value of all the colours by the given scale factor */
  71. void multiplyOpacity (float multiplier) noexcept;
  72. //==============================================================================
  73. /** Returns the number of colour-stops that have been added. */
  74. int getNumColours() const noexcept;
  75. /** Returns the position along the length of the gradient of the colour with this index.
  76. The index is from 0 to getNumColours() - 1. The return value will be between 0.0 and 1.0
  77. */
  78. double getColourPosition (int index) const noexcept;
  79. /** Returns the colour that was added with a given index.
  80. The index is from 0 to getNumColours() - 1.
  81. */
  82. Colour getColour (int index) const noexcept;
  83. /** Changes the colour at a given index.
  84. The index is from 0 to getNumColours() - 1.
  85. */
  86. void setColour (int index, Colour newColour) noexcept;
  87. /** Returns the an interpolated colour at any position along the gradient.
  88. @param position the position along the gradient, between 0 and 1
  89. */
  90. Colour getColourAtPosition (double position) const noexcept;
  91. //==============================================================================
  92. /** Creates a set of interpolated premultiplied ARGB values.
  93. This will resize the HeapBlock, fill it with the colours, and will return the number of
  94. colours that it added.
  95. When calling this, the ColourGradient must have at least 2 colour stops specified.
  96. */
  97. int createLookupTable (const AffineTransform& transform, HeapBlock <PixelARGB>& resultLookupTable) const;
  98. /** Creates a set of interpolated premultiplied ARGB values.
  99. This will fill an array of a user-specified size with the gradient, interpolating to fit.
  100. The numEntries argument specifies the size of the array, and this size must be greater than zero.
  101. When calling this, the ColourGradient must have at least 2 colour stops specified.
  102. */
  103. void createLookupTable (PixelARGB* resultLookupTable, int numEntries) const noexcept;
  104. /** Returns true if all colours are opaque. */
  105. bool isOpaque() const noexcept;
  106. /** Returns true if all colours are completely transparent. */
  107. bool isInvisible() const noexcept;
  108. //==============================================================================
  109. Point<float> point1, point2;
  110. /** If true, the gradient should be filled circularly, centred around
  111. point1, with point2 defining a point on the circumference.
  112. If false, the gradient is linear between the two points.
  113. */
  114. bool isRadial;
  115. bool operator== (const ColourGradient& other) const noexcept;
  116. bool operator!= (const ColourGradient& other) const noexcept;
  117. private:
  118. //==============================================================================
  119. struct ColourPoint
  120. {
  121. ColourPoint() noexcept {}
  122. ColourPoint (const double pos, Colour col) noexcept
  123. : position (pos), colour (col)
  124. {}
  125. bool operator== (const ColourPoint& other) const noexcept;
  126. bool operator!= (const ColourPoint& other) const noexcept;
  127. double position;
  128. Colour colour;
  129. };
  130. Array <ColourPoint> colours;
  131. JUCE_LEAK_DETECTOR (ColourGradient)
  132. };
  133. #endif // JUCE_COLOURGRADIENT_H_INCLUDED