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.

161 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. A component that lets the user choose a colour.
  18. This shows RGB sliders and a colourspace that the user can pick colours from.
  19. This class is also a ChangeBroadcaster, so listeners can register to be told
  20. when the colour changes.
  21. @tags{GUI}
  22. */
  23. class JUCE_API ColourSelector : public Component,
  24. public ChangeBroadcaster
  25. {
  26. public:
  27. //==============================================================================
  28. /** Options for the type of selector to show. These are passed into the constructor. */
  29. enum ColourSelectorOptions
  30. {
  31. showAlphaChannel = 1 << 0, /**< if set, the colour's alpha channel can be changed as well as its RGB. */
  32. showColourAtTop = 1 << 1, /**< if set, a swatch of the colour is shown at the top of the component. */
  33. editableColour = 1 << 2, /**< if set, the colour shows at the top of the component is editable. */
  34. showSliders = 1 << 3, /**< if set, RGB sliders are shown at the bottom of the component. */
  35. showColourspace = 1 << 4 /**< if set, a big HSV selector is shown. */
  36. };
  37. //==============================================================================
  38. /** Creates a ColourSelector object.
  39. The flags are a combination of values from the ColourSelectorOptions enum, specifying
  40. which of the selector's features should be visible.
  41. The edgeGap value specifies the amount of space to leave around the edge.
  42. gapAroundColourSpaceComponent indicates how much of a gap to put around the
  43. colourspace and hue selector components.
  44. */
  45. ColourSelector (int flags = (showAlphaChannel | showColourAtTop | showSliders | showColourspace),
  46. int edgeGap = 4,
  47. int gapAroundColourSpaceComponent = 7);
  48. /** Destructor. */
  49. ~ColourSelector() override;
  50. //==============================================================================
  51. /** Returns the colour that the user has currently selected.
  52. The ColourSelector class is also a ChangeBroadcaster, so listeners can
  53. register to be told when the colour changes.
  54. @see setCurrentColour
  55. */
  56. Colour getCurrentColour() const;
  57. /** Changes the colour that is currently being shown.
  58. @param newColour the new colour to show
  59. @param notificationType whether to send a notification of the change to listeners.
  60. A notification will only be sent if the colour has changed.
  61. */
  62. void setCurrentColour (Colour newColour, NotificationType notificationType = sendNotification);
  63. //==============================================================================
  64. /** Tells the selector how many preset colour swatches you want to have on the component.
  65. To enable swatches, you'll need to override getNumSwatches(), getSwatchColour(), and
  66. setSwatchColour(), to return the number of colours you want, and to set and retrieve
  67. their values.
  68. */
  69. virtual int getNumSwatches() const;
  70. /** Called by the selector to find out the colour of one of the swatches.
  71. Your subclass should return the colour of the swatch with the given index.
  72. To enable swatches, you'll need to override getNumSwatches(), getSwatchColour(), and
  73. setSwatchColour(), to return the number of colours you want, and to set and retrieve
  74. their values.
  75. */
  76. virtual Colour getSwatchColour (int index) const;
  77. /** Called by the selector when the user puts a new colour into one of the swatches.
  78. Your subclass should change the colour of the swatch with the given index.
  79. To enable swatches, you'll need to override getNumSwatches(), getSwatchColour(), and
  80. setSwatchColour(), to return the number of colours you want, and to set and retrieve
  81. their values.
  82. */
  83. virtual void setSwatchColour (int index, const Colour& newColour);
  84. //==============================================================================
  85. /** A set of colour IDs to use to change the colour of various aspects of the keyboard.
  86. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  87. methods.
  88. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  89. */
  90. enum ColourIds
  91. {
  92. backgroundColourId = 0x1007000, /**< the colour used to fill the component's background. */
  93. labelTextColourId = 0x1007001 /**< the colour used for the labels next to the sliders. */
  94. };
  95. //==============================================================================
  96. // These need to be public otherwise the Projucer's live-build engine will complain
  97. class ColourSpaceView;
  98. class HueSelectorComp;
  99. class ColourPreviewComp;
  100. private:
  101. //==============================================================================
  102. class SwatchComponent;
  103. Colour colour;
  104. float h, s, v;
  105. std::unique_ptr<Slider> sliders[4];
  106. std::unique_ptr<ColourSpaceView> colourSpace;
  107. std::unique_ptr<HueSelectorComp> hueSelector;
  108. std::unique_ptr<ColourPreviewComp> previewComponent;
  109. OwnedArray<SwatchComponent> swatchComponents;
  110. const int flags;
  111. int edgeGap;
  112. void setHue (float newH);
  113. void setSV (float newS, float newV);
  114. void updateHSV();
  115. void update (NotificationType);
  116. void changeColour();
  117. void paint (Graphics&) override;
  118. void resized() override;
  119. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourSelector)
  120. };
  121. } // namespace juce