[solved] Label .setColour not working, always turns out white no matter what color I set ..?

I have a regular label:
juce::Label currentValueLabel;

And I’m setting it to what I believe is bright yellow:
currentValueLabel.setColour(juce::Label::textColourId, juce::Colour::fromFloatRGBA(0.9f, 0.9f, 0.1f, 0.9f));

But its not working. Always turns out white. Why?

I would verify the color first. Put in a known good color, juce::Colours::yellow, and if it works you know to look at why your color is incorrect.

Are you using a custom LookAndFeel which overrides drawLabel() and ignores textColourId?

I tried this currentValueLabel.setColour(juce::Label::textColourId, juce::Colours::blue);
It still turns out white.
@andrewj - i don’t have a custom laf for this.

    currentValueLabel.setText("Current Value", juce::dontSendNotification);
    currentValueLabel.setFont(lcdDigitalFont);
    currentValueLabel.setColour(juce::Label::textColourId, juce::Colours::blue);
    //currentValueLabel.setColour(juce::Label::textColourId, juce::Colour::fromFloatRGBA(0.9f, 0.9f, 0.1f, 0.9f));
    currentValueLabel.setJustificationType(juce::Justification::left);
    addAndMakeVisible(currentValueLabel);

juce::Label currentValueLabel;
As far as I am aware the font does not contain any color information.

It works for me in a new GUI App project.

I guess maybe you should debug into:

//==============================================================================
void Label::paint (Graphics& g)
{
    getLookAndFeel().drawLabel (g, *this);
}

… which, if you indeed have no look and feel applied to the label, should take you into:

void LookAndFeel_V2::drawLabel (Graphics& g, Label& label)
{
    g.fillAll (label.findColour (Label::backgroundColourId));

    if (! label.isBeingEdited())
    {
        auto alpha = label.isEnabled() ? 1.0f : 0.5f;
        const Font font (getLabelFont (label));

        g.setColour (label.findColour (Label::textColourId).withMultipliedAlpha (alpha));
        g.setFont (font);

        auto textArea = getLabelBorderSize (label).subtractedFrom (label.getLocalBounds());

        g.drawFittedText (label.getText(), textArea, label.getJustificationType(),
                          jmax (1, (int) ((float) textArea.getHeight() / font.getHeight())),
                          label.getMinimumHorizontalScale());

        g.setColour (label.findColour (Label::outlineColourId).withMultipliedAlpha (alpha));
    }
    else if (label.isEnabled())
    {
        g.setColour (label.findColour (Label::outlineColourId));
    }

    g.drawRect (label.getLocalBounds());
}

…where perhaps you can see why your colour is not being applied.

1 Like

I think I found the issue. Further down the code, I am decreasing the label’s opacity in a timercallback()
The opacity decrease method is supposed to maintain the RGB but it seems it doesn’t.

void myplugin::decreaseOpacityOfCurrentValueLabel()
{

    juce::Colour currentColour = currentValueLabel.findColour(juce::Label::textColourId);

    float currentOpacity = currentColour.getFloatAlpha();

    currentOpacity = std::max(0.0f, currentOpacity - (currentOpacity > 0.6f ? 0.002f : 0.008f));

    juce::Colour newColour = juce::Colour::fromFloatRGBA(currentColour.getFloatRed(), currentColour.getFloatGreen(), currentColour.getFloatBlue(), currentOpacity);

    currentValueLabel.setColour(juce::Label::textColourId, newColour);
}

Off the top of my head, I think you could use:

   currentValueLabel.setColour(juce::Label::textColourId, currentColour.withAlpha(currentOpacity));

You can also set the opacity of the entire component directly and leave the color as it is:

currentValueLabel.setAlpha(/* some number*/)
1 Like

This is working for me - thank you :+1: