I think you could change this block to
```
        color_t color = g_colorManager.GetColor(text.Mid(pos + 5, finish - pos - 5));
        vecColors::const_iterator it = std::find(colors.begin(), colors.end(), color);
        if (it == colors.end())
        { // create new color
          if (colors.size() <= 0xFF)
          {
            newColor = colors.size();
            colors.push_back(color);
          }
          else // passed our max colors, so recycle
            newColor = 0;
        }
        else // reuse existed color
          newColor = it - colors.begin();
        colorStack.push(newColor);
```

That ensures newColor is in the range we want, right?