Problem with Colour::fromString()

Is there a problem with Colour::fromString() in juce 7?

Used to be able to write lines like this
static const Colour myColour { Colour::fromString(“#3B4252”) };

now it no longer gives expected results

g.setColour(myColour)

compiles but doesn’t register a new colour

any suggestions?

Thanx

I’m not sure what this means.

Is this not the colour you’re seeing when you paint or fill something with it?

If not, it’s entirely possible that JUCE’s hex parser requires you to have the alpha included, meaning your colour string should be "#ff3b4252" (being ARGB).

It has no effect on the paint process. It worked fine, and now it doesn’t. I get the standard greenish background. Will try adding the alpha. thanks

That worked! Thanks

Just in case you didn’t know, if you want hard coded hex colour values in your code, you don’t need the overhead of parsing a string. Directly entering an uint32 hex literal works as well and is a lot more straightforward and efficient as you don’t need to rely on any runtime code that understands your string and then generates the exact same integer value from that string that you could have hard-coded right away:

static const Colour myColour { 0xFF3B4252 };

If you have a look at the implementation of juce_Colours.h you’ll see that this is exactly what they also do for all their colours.

2 Likes

Also note that the fromString() method doesn’t support triplets. If you only supply three values, the first is taken as alpha and blue is unset (zero).

EDIT: Just see it was pointed out before:

Sorry, didn’t check properly

2 Likes

Regardless of the skimming :slight_smile: - I was thinking about this; why doesn’t JUCE’s parser support this anyway? It’s not like it’s not a common UX.

I think it is a simple oversight. In my code I use this:

juce::Colour::fromString (name.length() < 8 ? "ff" + name : name)