Unique colors for each user

In my web application, I have a chat. I want each username to be colored differently, not all black. What is the best way to achieve this? Do I have to store RGB / HEX code for each user when they subscribe to my application and put that string as a field as part of their database record?

+1


a source to share


4 answers


I will have a fixed list of colors and select users from this and store the index in the database 1 = red

, 2 = green

etc. This will allow you to change the shade of each color to suit your website design as it evolves.



For example, you might want "red" to match the same red as your logo; if you have stored this RGB value in your database rather than just "red" it will be much more difficult to change.

+6


a source


If your goal is to make a user distinguishable at a glance in a chat room, it depends on how large your user base is. If the average chat size is, say, 10, but your userbase is in the thousands, having a unique color for each won't work. You will end up with slightly different shades of the same color appearing in the same chat room.



In this case, just keep a short list of clearly different colors and assign them to the same chat room.

+2


a source


As an alternative to your schema:

You can do it like irssi (and others) and just assign a hash based color (using your select function) to the screen. Nickname length and color (should / not) be uncorrelated in this system, so a mix of different letters, different screen names, and different colors should be enough to make things distinguishable, but consistent from session to session. In this scheme, the only thing you might need to cache is like a color session for each username, so you don't need to make md5 calls. This does not guarantee a different color for each user (far from it), and perhaps you should think about the goals you are trying to achieve with this specification.

+1


a source


Create your colors from HSV / HSL using hue value in particular. Then convert to RGB.

See my answer here for more information on HSV / HSL

You probably want different colors every time.

You can do this by saving a queue of color ranges.

If the queue is empty, return hue 0 and insert the pair (0, 360) into the queue. The next time you want the color to come out of the queue (0, 360) and return the midpoint between these values: 180. Then push (0, 180) and (180, 360) into the queue.

The next time you want a color to select an item from the queue (0, 180) and return the midpoint between these values: 90. Then push (0, 90) and (90, 180) into the queue.

The next time you want a color to come out of the queue (180, 360) and return the midpoint between these values: 270. Then push (180, 270) and (270, 360) into the queue.

Continue this process ...

You will have the widest possible color for each user.

0


a source







All Articles