每个用户都有独特的颜色

发布于 2024-07-22 12:56:05 字数 130 浏览 10 评论 0原文

在我的网络应用程序中,我有一个聊天室。 我希望每个用户的名字都有不同的颜色,而不是全黑。 实现这一目标的最佳方法是什么? 我是否应该在每个用户注册我的应用程序时存储他们的 RGB/HEX 代码,并将该字符串作为字段作为其数据库记录的一部分?

In my web app I have a chatroom. I wish to have each user's name to be colored differently rather than having them all black. What is the best way to achieve this? Should I store the RGB/HEX code for each user as they sign up to my app and put that string as a field as part of their record in the database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谢绝鈎搭 2024-07-29 12:56:05

我有一个固定的颜色列表,让用户从中选择并将索引存储在数据库中,1 = red2 = green 等。这将允许您更改每种颜色的深浅,以适应网站设计的发展。

例如,您可能希望“红色”与您的徽标匹配相同的红色; 如果您将其作为 RGB 值存储在数据库中而不仅仅是“红色”,则更改起来会困难得多。

I'd have a fixed list of colours and get the users to pick from that and store an index in the database, 1 = red, 2 = green etc. This would allow you to change the shade of each colour to match the design of your site as it evolves.

For example, you might want the "red" to match the same red as your logo; if you've stored this as an RGB value in your database rather than just "red" it will be much harder to change.

千秋岁 2024-07-29 12:56:05

如果您的目标是让用户在聊天室中一目了然,那么这取决于您的用户群有多大。 如果聊天室的平均大小为 10 个,但您的用户群有数千个,那么为每个房间设置唯一的颜色是行不通的。 最终,您会在同一个聊天室中看到相同颜色略有不同的色调。

在这种情况下,只需保留一个明显不同颜色的简短列表,并将它们分配到单个聊天室中即可。

If your goal is to make the user distinguishable at a glance within the chat room, it depends on how large your user base is. If the average size of a chat room is, say, 10, but your user base is in the thousands, having a unique color for each of them is not going to work. Eventually you would end up with slightly different shades of the same color showing up in the same chat room.

In that case just keep a short list of blatantly different colors and assign them within the single chat room.

冬天的雪花 2024-07-29 12:56:05

作为您的方案的替代方案:

您可以像 irssi (和其他人一样)那样做,只需根据屏幕昵称的哈希值(使用您选择的函数)分配颜色。 在这个系统下,昵称的长度和颜色(应该/将会)是不相关的,因此不同字母、不同屏幕名称和不同颜色的组合应该足以使事物既可区分,又在会话之间保持一致。 在此方案中,您可能需要缓存的唯一内容是在会话中每个用户名的颜色,因此您不必执行无数次 md5 调用。 这并不能保证每个用户都有不同的颜色(远非如此),也许您应该考虑一下您试图通过该规范实现的目的。

As an alternative to your scheme:

You can do it like irssi (and others do), and just assign color based on the hash (using your function of choice) of the screen nick. Length of nick and color (should / will) be uncorrelated under this system, so the combination of different letters, different screen names, and different colors should be enough to make things both distinguishable, but consistent from session to session. In this scheme, this only thing you might have to cache is within as session the color for each user name, so you don't have to do zillions of md5 calls. This doesn't guarantee a different color for each user (far from it), and maybe you should think about the purposes you're trying to achieve with that specification.

メ斷腸人バ 2024-07-29 12:56:05

特别是使用色调值从 HSV/HSL 生成颜色。 然后转换为RGB。

请参阅我的答案,了解有关 HSV/HSL 的更多信息

您可能希望尽可能不同每次都有颜色。

您可以通过保留颜色范围队列来做到这一点。

如果队列为空,则返回色调 0 并将 (0, 360) 对插入队列中。
下次您想要颜色时,从队列中弹出一个元素 (0, 360),并返回这些值之间的中点:180。然后将 (0, 180) 和 (180, 360) 推入队列。

下次您想要颜色时,从队列中弹出一个元素 (0, 180),并返回这些值之间的中点:90。然后将 (0, 90) 和 (90, 180) 推入队列。

下次您想要颜色时,从队列中弹出一个元素 (180, 360),并返回这些值之间的中点:270。然后将 (180, 270) 和 (270, 360) 推入队列。

继续此过程...

您将自动为每个用户提供最广泛的可区分颜色。

Generate your colors from HSV/HSL using the hue value in particular. Then convert to RGB.

See my answer here for more information on HSV/HSL

You probably want as different as possible colors each time.

You could do this by keeping a queue of color ranges.

If the queue is empty return hue 0 and insert the pair (0, 360) into the queue.
Next time you want a color pop an element from the queue (0, 360), and return the mid point between those values: 180. Then push (0, 180) and (180, 360) into the queue.

Next time you want a color pop an element from the queue (0, 180), and return the mid point between those values: 90. Then push (0, 90) and (90, 180) into the queue.

Next time you want a color pop an element from the queue (180, 360), and return the mid point between those values: 270. Then push (180, 270) and (270, 360) into the queue.

Continue this process...

You will have the widest possible distinguishable colors for each user automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文