如何生成一组随机颜色,其中没有两种颜色几乎相似? [英] How to generate a set of random colors where no two colors are almost similar?

查看:445
本文介绍了如何生成一组随机颜色,其中没有两种颜色几乎相似?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用以下函数生成颜色的随机十六进制表示。

I currently use the following function to generate a random hexadecimal representation of a color.

function getRandomColor($max_r = 192, $max_g = 192, $max_b = 192) {
    if ($max_r > 192) { $max_r = 192; }
    if ($max_g > 192) { $max_g = 192; }
    if ($max_b > 192) { $max_b = 192; }
    if ($max_r < 0) { $max_r = 0; }
    if ($max_g < 0) { $max_g = 0; }
    if ($max_b < 0) { $max_b = 0; }

    return '#' . dechex(rand(0, 192)) . dechex(rand(0, 192)) . dechex(rand(0, 192));
}

请注意,我将最大值设置为192而不是255原因,我避免了非常浅的颜色,为了我将使用随机颜色作为前景在白色背景中。

Notice that I set the max value to be 192 instead of 255 for the sole reason that I am avoiding very light colors, for the purpose that I would be using the random color as foreground in a white background.

我的问题是如何生成无限数量的颜色,其中没有几乎相同的颜色。例如:#D964D9 & #FF3EFF

My question is how do I generate an indefinitely numbered set of colors where there are no colors that are almost the same. e.g.: #D964D9 & #FF3EFF ?

推荐答案

=http://en.wikipedia.org/wiki/HSL_and_HSV =nofollow noreferrer> HSV 坐标。如果不需要白色或黑色,则可以将S和V设置为其最大值,并生成彼此不太接近的H值(模360度)。然后转换为RGB。

It might be better to use HSV coordinates. If you don't need white or black, you can set S and V to their maximum values, and generate H values that are not too close to each other (mod 360 degrees). Then convert to RGB.

这篇关于如何生成一组随机颜色,其中没有两种颜色几乎相似?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆