# String identifiers for colour ids

**URL:** <https://forum.juce.com/t/string-identifiers-for-colour-ids/39677>\
**Category:** Feature Requests\
**Created:** [May 26, 2020, 10:24am UTC](https://forum.juce.com/t/string-identifiers-for-colour-ids/39677 "2020-05-26T10:24:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luzifer](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/luzifer/32/60_2.png) [@luzifer](https://forum.juce.com/u/luzifer)\
**Post date:** [May 26, 2020, 10:24am UTC](https://forum.juce.com/t/string-identifiers-for-colour-ids/39677/1 "2020-05-26T10:24:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 26, 2020, 10:39am UTC](https://forum.juce.com/t/string-identifiers-for-colour-ids/39677/2 "2020-05-26T10:39:29Z")

</div>

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](https://foleysfinest.com/developer/pluginguimagic/) I use maps to map from a string to the enum value like this:

> <https://github.com/ffAudio/foleys_gui_magic/blob/master/General/foleys_MagicJUCEFactories.cpp#L168>

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.

---

<div class="post-metadata">

**Author:** ![luzifer](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/luzifer/32/60_2.png) [@luzifer](https://forum.juce.com/u/luzifer)\
**Post date:** [May 26, 2020, 10:52am UTC](https://forum.juce.com/t/string-identifiers-for-colour-ids/39677/3 "2020-05-26T10:52:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 26, 2020, 10:59am UTC](https://forum.juce.com/t/string-identifiers-for-colour-ids/39677/4 "2020-05-26T10:59:24Z")

</div>

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:

> [@Std::hash specialisation for Identifier / unordered\_map](https://forum.juce.com/t/std-hash-specialisation-for-identifier-unordered-map/25157/16):
>
> …but the Identifier is already using the [StringPool](https://docs.juce.com/develop/classStringPool.html#aab1fd1101bfe15304bd618b3eeedfc0e) and a comparison for equality is a single operation:

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [May 26, 2020, 11:53am UTC](https://forum.juce.com/t/string-identifiers-for-colour-ids/39677/5 "2020-05-26T11:53:18Z")

</div>

same request here.  
Merging to yours

> [@Handle custom colourId for custom widget](https://forum.juce.com/t/handle-custom-colourid-for-custom-widget/39262):
>
> Hi, Would be great if there was a solution to handle custom colourId for custom widgets. Right now I do it manually enum ColourIds { backgroundColourId = UviLookAndFeel::MultiSliderOffset, sliderColourId, textValueBackgroundColourId, textValueColourId }; but this is quite error prone. Don’t have an out of the box solution to this issue but would like to open the discussion regarding this. Thanks !

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [May 26, 2020, 12:03pm UTC](https://forum.juce.com/t/string-identifiers-for-colour-ids/39677/6 "2020-05-26T12:03:33Z")

</div>

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;
}
```
