2013-08-15 23:12

# 为用户生成不同的颜色（日历中的颜色日期）[关闭]

I have a only one calendar and many users who create events in that calendar. Each user shall have an unique color as background for all the events he creates.

Users are unlimited, but in any case less than ffffff(16777215) users. So i just need an algorithm that creates an unique color for each user that is added to the calendar. The less users the calendar has the more diffrent should the colors be; e.g. red, green, blue, yellow for 4 users, but for 50 users of course the colors become more close to each other.

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 1条回答默认 最新

• douyulv6921 2013-08-15 23:33
已采纳

Technically you can't use all colors, a large range of those colors will clash with your design, and even worse, another large range will make the text on top hard to read, unless you also apply an algorithm to color the text in each cell to ensure higher contrast. Additionally, you need a pretty huge variance between the different colors to make them distinct. Overall it seems like too much effort for something that isn't going to look or work all that well.

I would suggest instead giving each user a randomly generated icon and displaying that icon in small to the left of their events. See the icons that Stack Overflow has? They use Gravatar. Gravatar provides several forms of randomly generated icons based on user email, and users are also free to create a Gravatar account and upload their own icon. I would strongly consider doing this instead. You could have it implemented in less than 5 minutes and be done with it.

That said, if you insist on trying it, I think the best way would be to come up with a formula that takes the auto-incremented user ID and splits up the spectrum automatically distributing the color choices as widely as possible by dividing the allowed spectrum in half repeatedly. This way the first 50 users will have the most variance in color, you will be able to predictably determine each user's color at any time, and it will otherwise be pretty random.

Regarding an algorithm for coloring text to give high contrast, that's a whole other thing. I'm sure Google would yield some good results. A simple method would be to set a threshold where a color is considered light or dark, and just test the color against that - if it's light use black font, if it's dark use white font. Drop shadows could also help with legibility.

点赞 评论