A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearAssigning random colors per session, for chat - page 1
User InfoPosts
Assigning random colors per session, for chat#1
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?

posted date: 2009-04-11 16:45:00

Re: Assigning random colors per session, for chat#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-11 16:45:01

Re: Assigning random colors per session, for chat#3
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).

posted date: 2009-04-11 16:48:00

Re: Assigning random colors per session, for chat#4
Create a list of colors (by hand) that are dark enough and easily distinguishable, and then choose one randomly.

posted date: 2009-04-11 16:49:00

Re: Assigning random colors per session, for chat#5
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.

posted date: 2009-04-11 16:56:00

Re: Assigning random colors per session, for chat#6
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.

posted date: 2009-04-11 17:10:00

Re: Assigning random colors per session, for chat#7
here is a good list of web colors to choose from: december.com/html/spec/colorhslhex10.html

posted date: 2009-04-11 17:20:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.