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.

186 lines
7.3KB

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