views:

212

answers:

3

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?

+6  A: 

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).

Chris Upchurch
30 seconds later than your answer!
Brian Campbell
A good starting point could be the label colors in gmail - they're easily distinguishable and designed to work on a light background. There may not be enough of them though.
ozan
here is a good list of web colors to choose from: http://www.december.com/html/spec/colorhslhex10.html
Calvin
+1  A: 

Create a list of colors (by hand) that are dark enough and easily distinguishable, and then choose one randomly.

Brian Campbell
+1  A: 

You don't want to go too random with colors if you also want them to contrast with each other and be on the dark side of white.

It may be better to predefine an array of colors, and cycle through them from a random starting position, so none will be repeated until the whole set has been assigned.

kennebec