根据 IP 地址选择颜色
在我的 PHP (v5.2.17) 脚本中,我想根据当前用户的 IP 地址为当前用户的条目选择唯一的颜色。我不想从十六进制代码映射颜色值,因为我还想随着时间的推移淡化每个条目的颜色。颜色必须始终将其中一个 RGB 值设置为零(就像一组明亮的原色)。
有没有一个聪明的数学解决方案来做到这一点?
如果任何数学天才阅读本文能分享一些见解,我将不胜感激。 :-)
In my PHP (v5.2.17) script, I want to select a unique colour for the current user's entries, based on their IP address. I don't want to map the colour values from the hex codes, because I also want to fade the colours of each entry over time. The colour must always have one of the RGB values set to zero (it's like a set of bright, primary colours).
Is there a clever mathematical solution to do this?
I'd greatly appreciate if any math genuises reading this would share some insights. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您真的将自己限制为只有六种“基本”颜色吗?
我想您将对这些颜色应用线性函数来尝试使它们淡出。这看起来不一定像您想象的那么好——RGB 作为一种表示并不是非常线性的。您可以使用 HSV 或 HSL 廉价地近似更好的“线性”表示a> 表示代替。它们肯定并不完美,但感觉比 RGB 更自然一些。
至于将 IP 地址映射到颜色,您可以将这些颜色组合存储在数组中,并使用简单的 哈希函数。像这样的东西可能就足够了:(
我刚刚随意选择了乘数
17
——它的二进制表示是10001
,这意味着地址中每个字节的位一旦您选择了几种颜色和一些 IP 地址,您可以尝试将乘数更改为21
或53。
看看是什么让最有道理。)Are you really limiting yourself to just six "base" colors?
I presume you're going to apply a linear function to these colors to try to fade them out. This won't necessarily look as good as you think it might -- RGB as a representation isn't very linear. You can cheaply approximate a better "linear" representation by using an HSV or HSL representation instead. They surely aren't perfect but it will feel a little more natural than RGB.
As for mapping the IP address to a color, you could store these color combinations in an array and pick among the six elements by using a simple hash function. Something like this might be sufficient:
(I just picked the multiplier
17
out of the air -- its binary representation is10001
, which means the bits of each byte in the address get "smeared" over each other. There might be better values. Once you've gotten a few colors selected and a handful of IP addresses you can try changing the multiplier to e.g.21
or53
and see what makes most sense.)虽然这不会给你一个
{R,G,B}
之一始终为 0 的结果,但 HSL 表示可能看起来不错。例如,假设色调是从 0 到 1 的十进制值,由 定义,其中每个 octet[i] 是一个无符号字节,而 ^ 是指数。然后,也许可以根据您的喜好手动设置亮度和饱和度。只是一个想法!
作为一个额外的好处,这使得褪色变得容易(只需从饱和度/亮度中减去“时间”的一部分)。
Although this won't give you a result where one of
{R,G,B}
is always 0, a HSL representation might look good. As an example, let hue be a decimal value from 0 to 1, defined by, where each
octet[i]
is an unsigned byte, and^
is exponentiation). And then perhaps set lightness and saturation by hand, as per your preference. Just an idea!As an added bonus, this makes fading the colours easy (just subtract some portion of "time" from saturation/lightness).
您使用数据库来存储关系吗?您始终可以获取用户的 IP 地址,
然后使用该函数设置 IP 的可用变量:
一旦获得该变量,您就可以分配未使用的颜色并保存关联以供将来参考。
are you using a database to store the relations? you could always grab the user's IP Address
Then use the function to set a usable variable of the IP:
once you have that, you can assign a color that isn't in use and save the association for future reference.