Default user color in chat

I’m making an app that displays twitch chat, and am trying to make it as similar as possible to the web interface.
I’m using the TWITCHCLIENT command when connecting, and it sends color/emoteset information for some users, but not all. Does that mean that the rest of the users have a default color, and is there an algorithm for finding it?

1 Like

There is an algorithm to pick the default color based on the username:

Modified Javascript used on the site:

default_colors = [
            ["Red", "#FF0000"],
            ["Blue", "#0000FF"],
            ["Green", "#00FF00"],
            ["FireBrick", "#B22222"],
            ["Coral", "#FF7F50"],
            ["YellowGreen", "#9ACD32"],
            ["OrangeRed", "#FF4500"],
            ["SeaGreen", "#2E8B57"],
            ["GoldenRod", "#DAA520"],
            ["Chocolate", "#D2691E"],
            ["CadetBlue", "#5F9EA0"],
            ["DodgerBlue", "#1E90FF"],
            ["HotPink", "#FF69B4"],
            ["BlueViolet", "#8A2BE2"],
            ["SpringGreen", "#00FF7F"];
get_color_for_user = function(name) {
            var color;
            if (user_to_color[name]){ //Cached from USERCOLOR notices
               color = user_to_color[name];
            }else {
                var n = name.charCodeAt(0) + name.charCodeAt(name.length - 1);
                color = default_colors[n % default_colors.length][1]
            return color;
1 Like

Perfect, thanks! Dunno how I didn’t think of looking at JS.
I’ve been doing a simple crc32 but this is much better.

Btw, just the last couple things I’m missing, if I don’t manage to find out myself - how are system messages represented in IRC, like subscriptions or slow mode/r2k? And how are admins/staff identified, by SPECIALUSER or MODE?

Admins, staff, subscribers and turbo users are identified by SPECIALUSER:

> :jtv PRIVMSG nickname :SPECIALUSER staffuser staff
> :jtv PRIVMSG nickname :SPECIALUSER adminuser admin

> :jtv PRIVMSG nickname :SPECIALUSER nickname2 subscriber
> :jtv PRIVMSG nickname :SPECIALUSER nickname2 turbo

Modes are also identified with PRIVMSG but only when it is enabled or disabled:

> :jtv PRIVMSG nickname :This room is now in slow mode. You may send messages every 120 seconds
> :jtv PRIVMSG nickname :This room is now in r9k mode. See

Thanks, so if both use PRIVMSG with your nickname, basically all unknown commands should be treated as messages? I assume notifications such as “user just subscribed” also use the same format? What about chat bans (that replace all messages with “”)?

Sorry for all the questions, the documentation is very brief and doesn’t explain any features (this one:

CLEARCHAT denotes when a user has been timed out or banned:

:jtv PRIVMSG nickname :CLEARCHAT nickname2

CLEARCHAT without a nickname indicates a full chat clear.

New subscriber notifications will come in the form of a channel message from twitchnotify.

I was wondering how to start receiving the SPECIALUSER messages? I was playing around with it and got them to start coming but I could not replicate it. Is it a quarry you send to jtv?

- obiedog

You need to send a client ID command after connecting. If you still want to receive JOIN/PART noticed then send TWITCHCLIENT 1, else send TWITCHCLIENT 2.

Thanks for the quick reply! Just what I couldn’t figure out.

Thanks again,

  • obiedog

The algorithm @george posted doesn’t seem to work anymore, and I haven’t found it in the source. (It’s not the fact that Green is now #008000). Some names return the same color although they are represented different in the original Twitch chat.

The users Swaggsurfnn and BobdeBouwerMC didn’t have a color tag but were represented in the original chat as BlueViolet and Blue respectively. The table shows calculations done on their names the way they are displayed, capital first letter and only lower case.

    Swaggsurfn  |  Swaggsurfnn  |  swaggsurfnn
n      193             193             225
i       13              13               0
    BlueViolet      BlueViolet         Red

  BobdeBouwerMC | Bobdebouwermc | bobdebouwermc
n      133             165             197
i       13               0               2
    BlueViolet         Red            Green

This is my adaptation in Go:

func colorFromName(name string) string {
    n := int(name[0]) + int(name[len(name)-1])
    return default_colors[n%len(default_colors)][1]

My application is supposed to work in conjunction with the original chat, so it would be nice if it had the same colors. Does anyone know the correct algorithm, or a different order of the array?

The javascript is way to advanced for me to understand completly, but I found these bits…

UserStore.COLORS = ["#FF0000", "#0000FF", "#008000", "#B22222", "#FF7F50", "#9ACD32", "#FF4500", "#2E8B57", "#DAA520", "#D2691E", "#5F9EA0", "#1E90FF", "#FF69B4", "#8A2BE2", "#00FF7F"];

util.array = {
    pickRandom: function pickRandom(array) {
        var randomIndex = Math.floor(Math.random() * array.length);
        return array[randomIndex]

SessionManager.prototype.getColor = function(username) {
    var color = this._users.getColor(username);
    if (!color) {
        color = _utilJs2["default"].array.pickRandom(_usersJs2["default"].COLORS);
        this._users.setColor(username, color)
    return color

It would seem that the color is instead chosen at random once, then shown to everyone the same way. If that is the case, I cannot retrieve the color through IRC :pensive:

Edit: For anyone interested, this is the js: (I recommend unobfuscating)

Didn’t get an authority on the matter, but could confirm with a friend that users without a defined color tag receive a random color for the session, for every other user perceiving them. Case closed.