TextEditor producing mixed color text

Hi everyone,

I have observed this weird behavior when trying to set the background color of a TextEditor to what is written in it and the text color to either white or black to be readable (example: if you type “#00F” the background should change to blue and the text color should change to white, whereas if you type “#FFFFFF”, the background should change to white and the text color should change to black). It appears as if any change to the text color made with a call to “editor.setColour(TextEditor::textColourId, …);” affects only the latest character.

I use this rule for the text color:
if (c.getLightness()>0.7) {
editor.setColour(TextEditor::textColourId, Colours::black);
} else {
editor.setColour(TextEditor::textColourId, Colours::white);
}

When I type “#FFFFFF”, I get a black #, white FFF, black FF, white F. I wasn’t aware that multi-colored text is even possible…

There’s a workaround, but it’s kind of ugly:
editor.setText(“”,dontSendNotification);
editor.setText(text,dontSendNotification);

Is there a better way to do this?

Here are some screenshots to illustrate the issue:



(I’ve written a 7th “F” so my color interpreter will put a default grey color in the background, showing both white and black characters). Interestingly, sometimes the # is white and sometimes it’s black.

Seems like the setColour doesn’t trigger a repaint(). Maybe just try to add a repaint() call after the setColour call?

https://docs.juce.com/master/classTextEditor.html

applyColourToAllText

2 Likes

Ta! I missed that they use setColour also for the rich text… sorry for my shot in the dark, went totally bonkers.

@reFX Thanks, that is much more elegant than resetting the text every time!
@daniel repainting didn’t help. That was the first thing I tried.