Why can't I set inc/dec slider color as with "regular" silders?

I hope I am just overlooking something, because this makes no sense at all.

For buttons I can do this and set each button to a different color;

myButton1.setColour (TextButton::buttonColourId, Colours::green);
myButton2.setColour (TextButton::buttonColourId, Colours::blue);

and so on, 100% logical, easy as minced pie.

For inc/dec sliders this should be possible, seems 100% logical, and it compiles but does nothing?!?;

mySlider1.setColour (TextButton::ColourIds::buttonColourId, Colours::darkblue);
mySlider2.setColour (TextButton::ColourIds::buttonColourId, Colours::darkred);

So I have to do this, but all my inc/dec sliders will be the same color;

getLookAndFeel ().setColour (TextButton::ColourIds::buttonColourId, Colours::darkblue);

So again I hope I have overlooked something, but do I really have to override lookandfeel just to set a few sliders with different colors?

Each component that supports simple color changes has its own set of ColourIds, Slider has the following and you need to access them from the Slider’s namespace with Slider::ColourIds (TextButton::ColourIds applies just to TextButtons) :

enum ColourIds
    {
        backgroundColourId          = 0x1001200,  /**< A colour to use to fill the slider's background. */
        thumbColourId               = 0x1001300,  /**< The colour to draw the thumb with. It's up to the look
                                                       and feel class how this is used. */
        trackColourId               = 0x1001310,  /**< The colour to draw the groove that the thumb moves along. */
        rotarySliderFillColourId    = 0x1001311,  /**< For rotary sliders, this colour fills the outer curve. */
        rotarySliderOutlineColourId = 0x1001312,  /**< For rotary sliders, this colour is used to draw the outer curve's outline. */

        textBoxTextColourId         = 0x1001400,  /**< The colour for the text in the text-editor box used for editing the value. */
        textBoxBackgroundColourId   = 0x1001500,  /**< The background colour for the text-editor box. */
        textBoxHighlightColourId    = 0x1001600,  /**< The text highlight colour for the text-editor box. */
        textBoxOutlineColourId      = 0x1001700   /**< The colour to use for a border around the text-editor box. */
    };

Thank you very much, and now I suddenly remembered why I used “TextButton” because, unless I am using the wrong syntax, none of the color ID’s will change colors of Inc/Dec “sliders”;

mySlider1.setColour (Slider::ColourIds::backgroundColourId, Colours::darkblue);

Only thing I got to work, without having to resort to overriding lookandfeel, was this to set all my Inc/Dec “sliders” same background color.

getLookAndFeel ().setColour (TextButton::ColourIds::buttonColourId, Colours::darkblue);

Yes it seems strange that would work to set background color on Inc/Dec “silders”, but it does.

The Inc/Dec slider style uses internal Button instances. So you would need to figure out how to access those and then set the colours.

A quick test I did, which appears to work :

auto& sliderChildren = keySlider.getChildren();
		for (auto& child : sliderChildren)
		{
			auto but = dynamic_cast<TextButton*>(child);
			if (but)
			{
				but->setColour(TextButton::ColourIds::buttonColourId, Colours::yellow);
			}
		}

So if I understood you correct, and if my inc/dec “slider” is called mySlider1, I do this;

mySlider1.setSliderStyle (Slider::SliderStyle::IncDecButtons);
auto& sliderChildren = mySlider1.getChildren ();
	for (auto& child : sliderChildren)
	{
		auto but = dynamic_cast<TextButton*>(child);
		if (but)
		{
			but->setColour (TextButton::ColourIds::buttonColourId, Colours::yellow);
		}
	}

If that is correct it unfortunately does not change the background color in my end.

setColour is a method of Component, so no need for the dynamic_cast.

Check with a breakpoint, if the setColour is actually hit.

A good explanation, how colours work is in findColour()

Looks for a colour that has been registered with the given colour ID number.

If a colour has been set for this ID number using setColour(), then it is returned. If none has been set, the method will try calling the component's LookAndFeel class's findColour() method. If none has been registered with the look-and-feel either, it will just return black.

The colour IDs for various purposes are stored as enums in the components that they are relevant to - for an example, see Slider::ColourIds, Label::ColourIds, TextEditor::ColourIds, TreeView::ColourIds, etc.

That is the method that is called inside the LookAndFeel to find the colour to use

[quote=“daniel, post:6, topic:35517, full:true”]
setColour is a method of Component, so no need for the dynamic_cast.[/quote]

But you might want to ensure you are calling the method only on TextButton instances. (Maybe Slider will never have any other kind of child components, though…)

…you might, but any other Component shouldn’t react to TextButton::ColourIds…

well, I am abusing ColourIds myself for custom Components, so I shouldn’t be so loud here :wink:

Just had a quick look into the LookAndFeel:

The lookAndFeel is free to return any kind of Button* in Button* LookAndFeel::createSliderButton (Slider&, const bool isIncrement), and in case of LookAndFeel_V1 it is an ArrowButton, so this might be the reason, why it didn’t work with the dynamic_cast:

…and furthermore, the white is hardcoded :frowning:

I forgot to mention I tested my code above with whatever look and feel is default for Juce 5. In that the inc/dec button slider is just 2 text buttons with “+” and “-” on them.