I have an AJAX chat system that I’m working on and so far I have it working. One thing I want to do is have it so when the user name is displayed on screen, it is a unique color (like in an AIM window). How would I generate a random color to assign to the user’s name for the particular session they are logged in for?
The color would have to be something that is darker as it on white background and as they are generated, they can’t be too similar to the colors that other current users have been assigned.
EDIT Thanks, I guess a predefined list would be the easiest way. If I have this list in a config file and I assign a color when the user logs in, how should I go about making sure that no two logged in users have the same color?
I think you’re going to be better off randomly picking from a list of pregenerated colors that meet your requirements (dark enough, different enough from each other) than trying to generate colors on the fly.
Edited to add:
As far as keeping more than one user from having the same color, one thing I’ve seen systems do is to just assign colors locally on the users machine using Javascript. Usually it’s not that important that different users see the same colors for other people. I don’t care if the colors on my screen are the same as they are on some other user’s screen, as long as all the colors on my screen are unique. In fact it may be desirable for each user’s own text to be distinct (their’s is black, everyone else’s is colored, for instance).