如何生成一组随机颜色,其中没有两种颜色几乎相似?

编程入门 行业动态 更新时间:2024-10-09 15:18:13
本文介绍了如何生成一组随机颜色,其中没有两种颜色几乎相似?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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 ?

推荐答案

=en.wikipedia/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.

更多推荐

如何生成一组随机颜色,其中没有两种颜色几乎相似?

本文发布于:2023-11-28 21:53:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   两种

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!