String identifiers for colour ids

The transition to JUCE 6 could be a good moment to convert integer based colour ids to an Identifier or other string based solution.

That would be quite expensive for the runtime, since the drawing methods do a lot of lookup for those Colour values. It is probably best not to have a string comparison there.

In my WYSWYG PluginEditor editor PluginGuiMagic I use maps to map from a string to the enum value like this:

I use this lookup table only when the user modifies a value or at startup, when the UI is loaded from an XML. But at runtime it is best to have the mapping using those enum values.

Just my thoughts.

Comparing Identifiers is just comparing their pointer value so no more expensive than integers. The real benefit i see is when creating custom components and not having to book keep magic numbers scattered around in different classes that you often won’t touch anymore for years.

4 Likes

It would be great to have a solution for custom classes!

The cost is moved to creating the Identifier. I don’t know how that will work out in the end, since I read that the underlying StringPool can flush when it is full, which means you cannot rely on it to be only a pointer comparison? To be honest, I don’t know, I found that discussion confusing, but my takeaway was, that Identifiers are not as great than I thought they were.

This is what I got confused:

same request here.
Merging to yours

There’s some horror stories about converting to identifiers under the hood of it anyway, e.g.

Colour Component::findColour (int colourID, bool inheritFromParent) const
{
    if (auto* v = properties.getVarPointer (ComponentHelpers::getColourPropertyID (colourID)))
        return Colour ((uint32) static_cast<int> (*v));

    if (inheritFromParent && parentComponent != nullptr
         && (lookAndFeel == nullptr || ! lookAndFeel->isColourSpecified (colourID)))
        return parentComponent->findColour (colourID, true);

    return getLookAndFeel().findColour (colourID);
}

Where getColourPropertyID is:

    static Identifier getColourPropertyID (int colourID)
{
    char buffer[32];
    auto* end = buffer + numElementsInArray (buffer) - 1;
    auto* t = end;
    *t = 0;

    for (auto v = (uint32) colourID;;)
    {
        *--t = "0123456789abcdef" [v & 15];
        v >>= 4;

        if (v == 0)
            break;
    }

    for (int i = (int) sizeof (colourPropertyPrefix) - 1; --i >= 0;)
        *--t = colourPropertyPrefix[i];

    return t;
}