使用 PHP 生成随机十六进制颜色代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5614530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:59:36  来源:igfitidea点击:

Generating a random hex color code with PHP

phphex

提问by joshdcomp

I'm working on a project where I need to generate an undefined number of random, hexadecimal color codes…how would I go about building such a function in PHP?

我正在做一个项目,我需要生成未定义数量的随机十六进制颜色代码……我将如何在 PHP 中构建这样的函数?

回答by outis

An RGB hex string is just a number from 0x0 through 0xFFFFFF, so simply generate a number in that range and convert it to hexadecimal:

RGB 十六进制字符串只是从 0x0 到 0xFFFFFF 的数字,因此只需生成该范围内的数字并将其转换为十六进制:

function rand_color() {
    return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
}

or:

或者:

function rand_color() {
    return sprintf('#%06X', mt_rand(0, 0xFFFFFF));
}

回答by Galen

Get a random number from 0 to 255, then convert it to hex:

获取一个从 0 到 255 的随机数,然后将其转换为十六进制:

function random_color_part() {
    return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
}

function random_color() {
    return random_color_part() . random_color_part() . random_color_part();
}

echo random_color();

回答by Notepad

you can use md5 for that purpose,very short

您可以为此目的使用 md5,非常短

$color = substr(md5(rand()), 0, 6);

回答by Cas Bloem

$rand = str_pad(dechex(rand(0x000000, 0xFFFFFF)), 6, 0, STR_PAD_LEFT);
echo('#' . $rand);

You can change rand()in for mt_rand()if you want, and you can put strtoupper()around the str_pad()to make the random number look nicer (although it's not required).

如果需要,您可以更改rand()for mt_rand(),并且可以将 放在strtoupper()周围str_pad()以使随机数看起来更好(尽管这不是必需的)。

It works perfectly and is way simpler than all the other methods described here :)

它工作得很好,而且比这里描述的所有其他方法更简单:)

回答by Jake

Valid hex colors can contain 0 to 9 and A to F so if we create a string with those characters and then shuffle it, we can grab the first 6 characters to create a random hex color code. An example is below!

有效的十六进制颜色可以包含 0 到 9 和 A 到 F,因此如果我们用这些字符创建一个字符串然后对其进行打乱,我们可以获取前 6 个字符来创建一个随机的十六进制颜色代码。下面是一个例子!

code

代码

echo '#' . substr(str_shuffle('ABCDEF0123456789'), 0, 6);

I tested this in a while loop and generated 10,000 unique colors.

我在 while 循环中对此进行了测试,并生成了 10,000 种独特的颜色。

code I used to generate 10,000 unique colors:

我用来生成 10,000 种独特颜色的代码:

$colors = array();
while (true) {
   $color          = substr(str_shuffle('ABCDEF0123456789'), 0, 6);
   $colors[$color] = '#' . $color;
   if ( count($colors) == 10000 ) {
      echo implode(PHP_EOL, $colors);
      break;
   }
}

Which gave me these random colorsas the result.

结果给了我这些随机颜色



outis pointed out that my first example couldn't generate hexadecimals such as '4488CC' so I created a function which would be able to generate hexadecimals like that.

outis 指出我的第一个示例无法生成诸如“4488CC”之类的十六进制,因此我创建了一个能够生成此类十六进制的函数。

code

代码

function randomHex() {
   $chars = 'ABCDEF0123456789';
   $color = '#';
   for ( $i = 0; $i < 6; $i++ ) {
      $color .= $chars[rand(0, strlen($chars) - 1)];
   }
   return $color;
}

echo randomHex();


The second example would be better to use because it can return a lot more different results than the first example, but if you aren't going to generate a lot of color codes then the first example would work just fine.

使用第二个示例会更好,因为它可以返回比第一个示例更多不同的结果,但是如果您不打算生成大量颜色代码,那么第一个示例就可以正常工作。

回答by Cas Bloem

$color = sprintf("#%06x",rand(0,16777215));

回答by Cyril

Shortest way:

最短路径:

echo substr(uniqid(),-6); // result: 5ebf06

回答by Jeeva

This is how i do it.

这就是我的做法。

<?php echo 'rgba('.rand(0,255).', '.rand(0,255).', '.rand(0,255).', 0.73)'; ?>

回答by danorton

As of PHP 5.3, you can use openssl_random_pseudo_bytes():

从 PHP 5.3 开始,您可以使用openssl_random_pseudo_bytes()

$hex_string = bin2hex(openssl_random_pseudo_bytes(3));

回答by MudithaE

If someone wants to generate light colors

如果有人想产生浅色

sprintf('#%06X', mt_rand(0xFF9999, 0xFFFF00));