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.

567 lines
17KB

  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. class ColourSelector::ColourComponentSlider : public Slider
  18. {
  19. public:
  20. ColourComponentSlider (const String& name)
  21. : Slider (name)
  22. {
  23. setRange (0.0, 255.0, 1.0);
  24. }
  25. String getTextFromValue (double value)
  26. {
  27. return String::toHexString ((int) value).toUpperCase().paddedLeft ('0', 2);
  28. }
  29. double getValueFromText (const String& text)
  30. {
  31. return (double) text.getHexValue32();
  32. }
  33. private:
  34. JUCE_DECLARE_NON_COPYABLE (ColourComponentSlider)
  35. };
  36. //==============================================================================
  37. class ColourSelector::ColourSpaceMarker : public Component
  38. {
  39. public:
  40. ColourSpaceMarker()
  41. {
  42. setInterceptsMouseClicks (false, false);
  43. }
  44. void paint (Graphics& g) override
  45. {
  46. g.setColour (Colour::greyLevel (0.1f));
  47. g.drawEllipse (1.0f, 1.0f, getWidth() - 2.0f, getHeight() - 2.0f, 1.0f);
  48. g.setColour (Colour::greyLevel (0.9f));
  49. g.drawEllipse (2.0f, 2.0f, getWidth() - 4.0f, getHeight() - 4.0f, 1.0f);
  50. }
  51. private:
  52. JUCE_DECLARE_NON_COPYABLE (ColourSpaceMarker)
  53. };
  54. //==============================================================================
  55. class ColourSelector::ColourSpaceView : public Component
  56. {
  57. public:
  58. ColourSpaceView (ColourSelector& cs, float& hue, float& sat, float& val, const int edgeSize)
  59. : owner (cs), h (hue), s (sat), v (val), lastHue (0.0f), edge (edgeSize)
  60. {
  61. addAndMakeVisible (marker);
  62. setMouseCursor (MouseCursor::CrosshairCursor);
  63. }
  64. void paint (Graphics& g) override
  65. {
  66. if (colours.isNull())
  67. {
  68. const int width = getWidth() / 2;
  69. const int height = getHeight() / 2;
  70. colours = Image (Image::RGB, width, height, false);
  71. Image::BitmapData pixels (colours, Image::BitmapData::writeOnly);
  72. for (int y = 0; y < height; ++y)
  73. {
  74. const float val = 1.0f - y / (float) height;
  75. for (int x = 0; x < width; ++x)
  76. {
  77. const float sat = x / (float) width;
  78. pixels.setPixelColour (x, y, Colour (h, sat, val, 1.0f));
  79. }
  80. }
  81. }
  82. g.setOpacity (1.0f);
  83. g.drawImageTransformed (colours,
  84. RectanglePlacement (RectanglePlacement::stretchToFit)
  85. .getTransformToFit (colours.getBounds().toFloat(),
  86. getLocalBounds().reduced (edge).toFloat()),
  87. false);
  88. }
  89. void mouseDown (const MouseEvent& e) override
  90. {
  91. mouseDrag (e);
  92. }
  93. void mouseDrag (const MouseEvent& e) override
  94. {
  95. const float sat = (e.x - edge) / (float) (getWidth() - edge * 2);
  96. const float val = 1.0f - (e.y - edge) / (float) (getHeight() - edge * 2);
  97. owner.setSV (sat, val);
  98. }
  99. void updateIfNeeded()
  100. {
  101. if (lastHue != h)
  102. {
  103. lastHue = h;
  104. colours = Image::null;
  105. repaint();
  106. }
  107. updateMarker();
  108. }
  109. void resized() override
  110. {
  111. colours = Image::null;
  112. updateMarker();
  113. }
  114. private:
  115. ColourSelector& owner;
  116. float& h;
  117. float& s;
  118. float& v;
  119. float lastHue;
  120. ColourSpaceMarker marker;
  121. const int edge;
  122. Image colours;
  123. void updateMarker()
  124. {
  125. marker.setBounds (roundToInt ((getWidth() - edge * 2) * s),
  126. roundToInt ((getHeight() - edge * 2) * (1.0f - v)),
  127. edge * 2, edge * 2);
  128. }
  129. JUCE_DECLARE_NON_COPYABLE (ColourSpaceView)
  130. };
  131. //==============================================================================
  132. class ColourSelector::HueSelectorMarker : public Component
  133. {
  134. public:
  135. HueSelectorMarker()
  136. {
  137. setInterceptsMouseClicks (false, false);
  138. }
  139. void paint (Graphics& g) override
  140. {
  141. const float cw = (float) getWidth();
  142. const float ch = (float) getHeight();
  143. Path p;
  144. p.addTriangle (1.0f, 1.0f,
  145. cw * 0.3f, ch * 0.5f,
  146. 1.0f, ch - 1.0f);
  147. p.addTriangle (cw - 1.0f, 1.0f,
  148. cw * 0.7f, ch * 0.5f,
  149. cw - 1.0f, ch - 1.0f);
  150. g.setColour (Colours::white.withAlpha (0.75f));
  151. g.fillPath (p);
  152. g.setColour (Colours::black.withAlpha (0.75f));
  153. g.strokePath (p, PathStrokeType (1.2f));
  154. }
  155. private:
  156. JUCE_DECLARE_NON_COPYABLE (HueSelectorMarker)
  157. };
  158. //==============================================================================
  159. class ColourSelector::HueSelectorComp : public Component
  160. {
  161. public:
  162. HueSelectorComp (ColourSelector& cs, float& hue, const int edgeSize)
  163. : owner (cs), h (hue), edge (edgeSize)
  164. {
  165. addAndMakeVisible (marker);
  166. }
  167. void paint (Graphics& g) override
  168. {
  169. ColourGradient cg;
  170. cg.isRadial = false;
  171. cg.point1.setXY (0.0f, (float) edge);
  172. cg.point2.setXY (0.0f, (float) (getHeight() - edge));
  173. for (float i = 0.0f; i <= 1.0f; i += 0.02f)
  174. cg.addColour (i, Colour (i, 1.0f, 1.0f, 1.0f));
  175. g.setGradientFill (cg);
  176. g.fillRect (getLocalBounds().reduced (edge));
  177. }
  178. void resized() override
  179. {
  180. marker.setBounds (0, roundToInt ((getHeight() - edge * 2) * h), getWidth(), edge * 2);
  181. }
  182. void mouseDown (const MouseEvent& e) override
  183. {
  184. mouseDrag (e);
  185. }
  186. void mouseDrag (const MouseEvent& e) override
  187. {
  188. owner.setHue ((e.y - edge) / (float) (getHeight() - edge * 2));
  189. }
  190. void updateIfNeeded()
  191. {
  192. resized();
  193. }
  194. private:
  195. ColourSelector& owner;
  196. float& h;
  197. HueSelectorMarker marker;
  198. const int edge;
  199. JUCE_DECLARE_NON_COPYABLE (HueSelectorComp)
  200. };
  201. //==============================================================================
  202. class ColourSelector::SwatchComponent : public Component
  203. {
  204. public:
  205. SwatchComponent (ColourSelector& cs, int itemIndex)
  206. : owner (cs), index (itemIndex)
  207. {
  208. }
  209. void paint (Graphics& g) override
  210. {
  211. const Colour c (owner.getSwatchColour (index));
  212. g.fillCheckerBoard (getLocalBounds(), 6, 6,
  213. Colour (0xffdddddd).overlaidWith (c),
  214. Colour (0xffffffff).overlaidWith (c));
  215. }
  216. void mouseDown (const MouseEvent&) override
  217. {
  218. PopupMenu m;
  219. m.addItem (1, TRANS("Use this swatch as the current colour"));
  220. m.addSeparator();
  221. m.addItem (2, TRANS("Set this swatch to the current colour"));
  222. m.showMenuAsync (PopupMenu::Options().withTargetComponent (this),
  223. ModalCallbackFunction::forComponent (menuStaticCallback, this));
  224. }
  225. private:
  226. ColourSelector& owner;
  227. const int index;
  228. static void menuStaticCallback (int result, SwatchComponent* comp)
  229. {
  230. if (comp != nullptr)
  231. {
  232. if (result == 1)
  233. comp->setColourFromSwatch();
  234. else if (result == 2)
  235. comp->setSwatchFromColour();
  236. }
  237. }
  238. void setColourFromSwatch()
  239. {
  240. owner.setCurrentColour (owner.getSwatchColour (index));
  241. }
  242. void setSwatchFromColour()
  243. {
  244. if (owner.getSwatchColour (index) != owner.getCurrentColour())
  245. {
  246. owner.setSwatchColour (index, owner.getCurrentColour());
  247. repaint();
  248. }
  249. }
  250. JUCE_DECLARE_NON_COPYABLE (SwatchComponent)
  251. };
  252. //==============================================================================
  253. ColourSelector::ColourSelector (const int sectionsToShow, const int edge, const int gapAroundColourSpaceComponent)
  254. : colour (Colours::white),
  255. flags (sectionsToShow),
  256. edgeGap (edge)
  257. {
  258. // not much point having a selector with no components in it!
  259. jassert ((flags & (showColourAtTop | showSliders | showColourspace)) != 0);
  260. updateHSV();
  261. if ((flags & showSliders) != 0)
  262. {
  263. addAndMakeVisible (sliders[0] = new ColourComponentSlider (TRANS ("red")));
  264. addAndMakeVisible (sliders[1] = new ColourComponentSlider (TRANS ("green")));
  265. addAndMakeVisible (sliders[2] = new ColourComponentSlider (TRANS ("blue")));
  266. addChildComponent (sliders[3] = new ColourComponentSlider (TRANS ("alpha")));
  267. sliders[3]->setVisible ((flags & showAlphaChannel) != 0);
  268. for (int i = 4; --i >= 0;)
  269. sliders[i]->addListener (this);
  270. }
  271. if ((flags & showColourspace) != 0)
  272. {
  273. addAndMakeVisible (colourSpace = new ColourSpaceView (*this, h, s, v, gapAroundColourSpaceComponent));
  274. addAndMakeVisible (hueSelector = new HueSelectorComp (*this, h, gapAroundColourSpaceComponent));
  275. }
  276. update();
  277. }
  278. ColourSelector::~ColourSelector()
  279. {
  280. dispatchPendingMessages();
  281. swatchComponents.clear();
  282. }
  283. //==============================================================================
  284. Colour ColourSelector::getCurrentColour() const
  285. {
  286. return ((flags & showAlphaChannel) != 0) ? colour : colour.withAlpha ((uint8) 0xff);
  287. }
  288. void ColourSelector::setCurrentColour (Colour c)
  289. {
  290. if (c != colour)
  291. {
  292. colour = ((flags & showAlphaChannel) != 0) ? c : c.withAlpha ((uint8) 0xff);
  293. updateHSV();
  294. update();
  295. }
  296. }
  297. void ColourSelector::setHue (float newH)
  298. {
  299. newH = jlimit (0.0f, 1.0f, newH);
  300. if (h != newH)
  301. {
  302. h = newH;
  303. colour = Colour (h, s, v, colour.getFloatAlpha());
  304. update();
  305. }
  306. }
  307. void ColourSelector::setSV (float newS, float newV)
  308. {
  309. newS = jlimit (0.0f, 1.0f, newS);
  310. newV = jlimit (0.0f, 1.0f, newV);
  311. if (s != newS || v != newV)
  312. {
  313. s = newS;
  314. v = newV;
  315. colour = Colour (h, s, v, colour.getFloatAlpha());
  316. update();
  317. }
  318. }
  319. //==============================================================================
  320. void ColourSelector::updateHSV()
  321. {
  322. colour.getHSB (h, s, v);
  323. }
  324. void ColourSelector::update()
  325. {
  326. if (sliders[0] != nullptr)
  327. {
  328. sliders[0]->setValue ((int) colour.getRed());
  329. sliders[1]->setValue ((int) colour.getGreen());
  330. sliders[2]->setValue ((int) colour.getBlue());
  331. sliders[3]->setValue ((int) colour.getAlpha());
  332. }
  333. if (colourSpace != nullptr)
  334. {
  335. colourSpace->updateIfNeeded();
  336. hueSelector->updateIfNeeded();
  337. }
  338. if ((flags & showColourAtTop) != 0)
  339. repaint (previewArea);
  340. sendChangeMessage();
  341. }
  342. //==============================================================================
  343. void ColourSelector::paint (Graphics& g)
  344. {
  345. g.fillAll (findColour (backgroundColourId));
  346. if ((flags & showColourAtTop) != 0)
  347. {
  348. const Colour currentColour (getCurrentColour());
  349. g.fillCheckerBoard (previewArea, 10, 10,
  350. Colour (0xffdddddd).overlaidWith (currentColour),
  351. Colour (0xffffffff).overlaidWith (currentColour));
  352. g.setColour (Colours::white.overlaidWith (currentColour).contrasting());
  353. g.setFont (Font (14.0f, Font::bold));
  354. g.drawText (currentColour.toDisplayString ((flags & showAlphaChannel) != 0),
  355. previewArea, Justification::centred, false);
  356. }
  357. if ((flags & showSliders) != 0)
  358. {
  359. g.setColour (findColour (labelTextColourId));
  360. g.setFont (11.0f);
  361. for (int i = 4; --i >= 0;)
  362. {
  363. if (sliders[i]->isVisible())
  364. g.drawText (sliders[i]->getName() + ":",
  365. 0, sliders[i]->getY(),
  366. sliders[i]->getX() - 8, sliders[i]->getHeight(),
  367. Justification::centredRight, false);
  368. }
  369. }
  370. }
  371. void ColourSelector::resized()
  372. {
  373. const int swatchesPerRow = 8;
  374. const int swatchHeight = 22;
  375. const int numSliders = ((flags & showAlphaChannel) != 0) ? 4 : 3;
  376. const int numSwatches = getNumSwatches();
  377. const int swatchSpace = numSwatches > 0 ? edgeGap + swatchHeight * ((numSwatches + 7) / swatchesPerRow) : 0;
  378. const int sliderSpace = ((flags & showSliders) != 0) ? jmin (22 * numSliders + edgeGap, proportionOfHeight (0.3f)) : 0;
  379. const int topSpace = ((flags & showColourAtTop) != 0) ? jmin (30 + edgeGap * 2, proportionOfHeight (0.2f)) : edgeGap;
  380. previewArea.setBounds (edgeGap, edgeGap, getWidth() - edgeGap * 2, topSpace - edgeGap * 2);
  381. int y = topSpace;
  382. if ((flags & showColourspace) != 0)
  383. {
  384. const int hueWidth = jmin (50, proportionOfWidth (0.15f));
  385. colourSpace->setBounds (edgeGap, y,
  386. getWidth() - hueWidth - edgeGap - 4,
  387. getHeight() - topSpace - sliderSpace - swatchSpace - edgeGap);
  388. hueSelector->setBounds (colourSpace->getRight() + 4, y,
  389. getWidth() - edgeGap - (colourSpace->getRight() + 4),
  390. colourSpace->getHeight());
  391. y = getHeight() - sliderSpace - swatchSpace - edgeGap;
  392. }
  393. if ((flags & showSliders) != 0)
  394. {
  395. const int sliderHeight = jmax (4, sliderSpace / numSliders);
  396. for (int i = 0; i < numSliders; ++i)
  397. {
  398. sliders[i]->setBounds (proportionOfWidth (0.2f), y,
  399. proportionOfWidth (0.72f), sliderHeight - 2);
  400. y += sliderHeight;
  401. }
  402. }
  403. if (numSwatches > 0)
  404. {
  405. const int startX = 8;
  406. const int xGap = 4;
  407. const int yGap = 4;
  408. const int swatchWidth = (getWidth() - startX * 2) / swatchesPerRow;
  409. y += edgeGap;
  410. if (swatchComponents.size() != numSwatches)
  411. {
  412. swatchComponents.clear();
  413. for (int i = 0; i < numSwatches; ++i)
  414. {
  415. SwatchComponent* const sc = new SwatchComponent (*this, i);
  416. swatchComponents.add (sc);
  417. addAndMakeVisible (sc);
  418. }
  419. }
  420. int x = startX;
  421. for (int i = 0; i < swatchComponents.size(); ++i)
  422. {
  423. SwatchComponent* const sc = swatchComponents.getUnchecked(i);
  424. sc->setBounds (x + xGap / 2,
  425. y + yGap / 2,
  426. swatchWidth - xGap,
  427. swatchHeight - yGap);
  428. if (((i + 1) % swatchesPerRow) == 0)
  429. {
  430. x = startX;
  431. y += swatchHeight;
  432. }
  433. else
  434. {
  435. x += swatchWidth;
  436. }
  437. }
  438. }
  439. }
  440. void ColourSelector::sliderValueChanged (Slider*)
  441. {
  442. if (sliders[0] != nullptr)
  443. setCurrentColour (Colour ((uint8) sliders[0]->getValue(),
  444. (uint8) sliders[1]->getValue(),
  445. (uint8) sliders[2]->getValue(),
  446. (uint8) sliders[3]->getValue()));
  447. }
  448. //==============================================================================
  449. int ColourSelector::getNumSwatches() const
  450. {
  451. return 0;
  452. }
  453. Colour ColourSelector::getSwatchColour (const int) const
  454. {
  455. jassertfalse; // if you've overridden getNumSwatches(), you also need to implement this method
  456. return Colours::black;
  457. }
  458. void ColourSelector::setSwatchColour (const int, const Colour&) const
  459. {
  460. jassertfalse; // if you've overridden getNumSwatches(), you also need to implement this method
  461. }