2013-08-15 23:12
浏览 142


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.

图片转代码服务由CSDN问答提供 功能建议

我只有一个日历和许多用户在该日历中创建事件。 每个用户都应具有唯一的颜色作为他创建的所有事件的背景。

用户无限,但无论如何不到ffffff(16777215)用户。 所以我只需要一种算法,为每个添加到日历中的用户创建一种独特的颜色。 日历的用户越少,颜色就越不同; 例如 4个用户的红色,绿色,蓝色,黄色,但对于50个用户来说,颜色当然会变得更加接近。

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

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.

    点赞 评论

相关推荐 更多相似问题