Lua Code:
local function hexColorNames(chatMessage, modChatColor)
local words,coloredMessage,s,p,class = {strsplit(' ', chatMessage)},''
local combinedName = UnitName('player')..'-'..GetRealmName()
local _, playerClass = UnitClass('player')
for k, w in ipairs(words) do
s,p = false,false
if w ~= 'player' and w ~= 'target' and UnitIsPlayer(w) then
_, class = UnitClass(w)
s = true
elseif combinedName:lower() == w:lower() then
p = true
end
w = (s or p) and '|c'..RAID_CLASS_COLORS[p and playerClass or class].colorStr..w..modChatColor or w
coloredMessage = coloredMessage == '' and w or coloredMessage..' '..w
end
return coloredMessage
end
Since you're iterating through the message word-for-word, just rebuild it word-for-word instead of using gsub. I also went with boolean switches and dropped some other stuff like UnitName being redundant since UnitIsPlayer is fine by itself.
I did not test this.