PHP代码在线运行:http://www.dooccn.com/php7/
复制代码去上述网站运行下,就能得知结果。
<?php
for($i=1;$i<=1500;$i++){
echo get_color_by_scale().",";
}
function get_color_by_scale( ) {
$str='0123456789ABCDEF';
$estr='';
$len=strlen($str);
for($i=1;$i<=6;$i++)
{
$num=rand(0,$len-1);
$estr=$estr.$str[$num];
}
$estr=changeColor($estr,"no");
return "'".$estr."'";
}
//hex颜色加深减淡
function changeColor($hex, $type='no'){
$level = "0.9"; //level为加深的程度,限0-1之间
$diycolor = '#b4e0e1';
if($hex < 0 || hexdec($hex) > hexdec('ffffff'))
{
$hex = $diycolor;
}
$rgb = hexToRgb($hex);
if($type=='-'){ //减淡
for ($i = 0; $i < 3; $i++) {
$re[$i] = floor((255 - $rgb[$i]) * $level + $rgb[$i]);
}
$re = rgbToHex($re);
}elseif($type=='+'){ //加深
for ($i = 0; $i < 3; $i++){
$re[$i] = floor($rgb[$i] * (1 - $level));
}
$re = rgbToHex($re);
}
else{
$re = $hex;
}
return $re;
}
//hex颜色转RGB
function hexToRgb($hex){
$hex = str_replace('#', '', $hex);
$rgb[0] = hexdec($hex[0].$hex[1]);
$rgb[1] = hexdec($hex[2].$hex[3]);
$rgb[2] = hexdec($hex[4].$hex[5]);
return $rgb;
}
?>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
这段PHP代码实现了一个循环,用于生成1500个随机的六字符颜色代码,并通过get_color_by_scale函数对颜色进行加深或减淡处理。get_color_by_scale函数首先生成一个随机的十六进制颜色代码,然后根据$type参数决定是否进行颜色加深或减淡操作。颜色转换由changeColor函数完成,它将十六进制颜色转换为RGB,然后根据$type调整亮度,最后再转换回十六进制。
249

被折叠的 条评论
为什么被折叠?



