Colour swatches question

I’m trying to get a colour selector to show some swatches, at least I think that’s what they are called? A palette of set colours, like the one that appears in the colour selector in the introjucer. Anyhow, I’m having some problems. My class looks like this:

class colourPallete : public ColourSelector
		{
			Array <Colour> swatchColours;
		
		    public:			
			colourPallete(){
				swatchColours.add(Colours::lime);
				swatchColours.add(Colours::white);
				swatchColours.add(Colours::red);
				swatchColours.add(Colours::pink);
				swatchColours.add(Colours::yellow);};

			~colourPallete(){};

			int getNumSwatches(){
				return swatchColours.size();
			}

			Colour getSwatchColour(int index){
				return swatchColours[index];
			}

			void setSwatchColour (int index, const Colour &newColour){
				swatchColours.insert(index, newColour);
			}
		};

I then call the colour selector like this:

colourPallete colourSelector; colourSelector.setBounds(xPos, yPos, 400, 400); colourSelector.addChangeListener (this); CallOutBox callOut (colourSelector, *this, nullptr); callOut.setTopLeftPosition(xPos+20, yPos); callOut.runModalLoop();

Surely I’m missing something here. Or perhaps it’s not as easy as I thought. I don’t have to go messing with colour selector look and feel methods do I?

What exactly are you expecting and what happens instead? You say you have problems with it, but you never tell what these problems actually are.

Sorry, the problem is it’s not showing any swatches?

Your constructor is not creating the ColourSelector!

ColourPallete : public ColourSelector
{
public:

  ColourPallete()
  :
    ColorSelector() // Without this the selector is not being initialized.  
  {
    ...
  }

...

Note also, from the documentation:

ColourSelector (int sectionsToShow=(showAlphaChannel|showColourAtTop|showSliders|showColourspace), int edgeGap=4, int gapAroundColourSpaceComponent=7)

You can pass a value other than the default 15 to adjust the behaviour of the selector.

Thanks Valley. I tried that but still no joy. Perhaps colour swatches is the wrong term? I’m hoping to show a palette of selected colours below the circular gradient selector widget. Here’s a screenshot of what I have:

[attachment=1]Untitled.png[/attachment]

And this is what I would like to have:

[attachment=0]Untitled1.png[/attachment]

I don’t see anything to tell the colour selector to show swatches?

Ok another thing wrong I see in your original code is that your implementation of getNumSwatches() etc are hiding not overriding their parent class implementations.

class MyColourSelector :	public ColourSelector
{
public:
	MyColourSelector();
	
	virtual ~MyColourSelector();  // Note virtual !!!

	// -----------------------------------------------------------------------

	virtual int getNumSwatches() const; // note virtual
	
	virtual Colour getSwatchColour(const int index) const; // note virtual 

	virtual void setSwatchColour(const int index, const Colour& newColour) const; // note virtual

private:
  ...
};

Google method hiding vs overriding for the details.

Ok, I need to learn about hiding versus overriding, method hiding is a new to me even though I’ve probably being using it without knowing for a while. However, in Jule’s introjucer code he does this:

    class ColourSelectorWithSwatches    : public ColourSelector
    {
    public:
        ColourSelectorWithSwatches() {}

        int getNumSwatches() const;
        Colour getSwatchColour (int index) const;
        void setSwatchColour (int index, const Colour& newColour) const;
    };

And it definitely works there? I’ve looked over his code and I can’t see what I’m missing. I’m sure it’s something stupid I’ve overlooked!?

OK. Can someone chime in here, because both things Valley said are wrong, IMO.

First, you don’t have to specify the constructor as you show, C++ will run the default constructor for you.

Second, just adding a ‘virtual’ doesn’t mean that you are hiding the function. It’s just redundant. The signatures look the same, so they should be overriding. Most compilers will warn if you’re hiding, BTW.

Am I wrong?

(Sorry I’m not answering the original question).

Bruce

Put your debugger at getNumSwatches() and see what happens. I wonder if your creation of standard colors is too late - it was 0 when you constructed the selector, so the view just doesn’t contain them.

Try a constant number of swatches, maybe - after all, you don’t want an infinite set of swatches anyway.

Bruce

No, you’re not. I spend too much of my time jumping between languages, and here, I’m getting C++ mixed up with java/C#. :frowning:

If the method is declared virtual, it remains virtual. My bad, and apologies for the confusion.

Well, the reason that no swatches are being created is because none of these functions ever get called. I’m confused!

Since I’m still feeling kind of bad for derailing the thread with a brain fart:

/*
  ==============================================================================

    This file was auto-generated by the Introjucer!

    It contains the basic startup code for a Juce application.

  ==============================================================================
*/

#include "../JuceLibraryCode/JuceHeader.h"


//==============================================================================
class MySelector : public ColourSelector
{
public:

  MySelector()
  {
    colours.add(new Colour(0xFF000000));
    colours.add(new Colour(0xFFFFFFFF));
    colours.add(new Colour(0xFFFF0000));
    colours.add(new Colour(0xFF00FF00));
    colours.add(new Colour(0xFF0000FF));
    colours.add(new Colour(0xFFFFFF00));
    colours.add(new Colour(0xFFFF00FF));
    colours.add(new Colour(0xFF00FFFF));
    colours.add(new Colour(0x80000000));
    colours.add(new Colour(0x80FFFFFF));
    colours.add(new Colour(0x80FF0000));
    colours.add(new Colour(0x8000FF00));
    colours.add(new Colour(0x800000FF));
    colours.add(new Colour(0x80FFFF00));
    colours.add(new Colour(0x80FF00FF));
    colours.add(new Colour(0x8000FFFF));
  }
  
  ~MySelector() {}
  
  // ---------------------------------------------------------------------------
  
  
  int getNumSwatches() const { return 16; }
  
  Colour getSwatchColour(int index) const { return *colours[index]; }
  
  void setSwatchColour (int index, const Colour &newColour) const
  {
    *(colours[index]) = newColour;
  }
       
  
private:
  
  OwnedArray<Colour> colours;
    
};


//==============================================================================
class MainWindow : public DocumentWindow
{
public:

  MainWindow() 
  : 
    DocumentWindow("Test Window", Colours::silver, DocumentWindow::closeButton)
  {
    mySelector.setBounds(0, 0, 400, 600);
    setContentNonOwned(&mySelector, true);
    setTopLeftPosition(200, 200);
  }

  
  ~MainWindow() {}
  
  
  // -------------------------------------------------------------------------
  
  void closeButtonPressed()
  {
    removeFromDesktop();
    JUCEApplication::quit();
  }
  
  
private:

  MySelector mySelector;
};



//==============================================================================
class TestApplication  : public JUCEApplication
{
public:
    //==============================================================================
    TestApplication() {}

    const String getApplicationName()       { return ProjectInfo::projectName; }
    const String getApplicationVersion()    { return ProjectInfo::versionString; }
    bool moreThanOneInstanceAllowed()       { return true; }

    //==============================================================================
    void initialise (const String& commandLine)
    {
      mainWindow.addToDesktop();
      mainWindow.setVisible(true);
    }

    void shutdown()
    {
    }

    //==============================================================================
    void systemRequestedQuit()
    {
        quit();
    }

    void anotherInstanceStarted (const String& commandLine)
    {
    }
    
private:

  MainWindow mainWindow;
  
};


//==============================================================================
// This macro generates the main() routine that launches the app.
START_JUCE_APPLICATION (TestApplication)

The above was knocked together in under 10 mins, so its bad practices all around, but it works, and it does show a colour selector with 16 colour swatches below the colour field. Maybe make a quick test project from it and test it?

Thanks everyone. Turns out there was a good reason the debugger wasn’t hitting those functions!Because I wasn’t including the const qualifier at the end of the function declaration thus I wasn’t actually overriding the functions. Serious embarrassment here…