English 中文(简体)
生成随机颜色
原标题:Generate random colors
  • 时间:2012-05-22 19:40:58
  •  标签:
  • php

我使用这个代码来产生随机颜色(效果很好):

  {
       $r = rand(128,255); 
       $g = rand(128,255); 
       $b = rand(128,255); 
       $color = dechex($r) . dechex($g) . dechex($b);
       return "#".$color;
  }

我只是想知道,是否有一种方法/组合 来只产生亮色的颜色?

谢谢

最佳回答

您的原始代码不像你预期的那样有效 - 如果生成了一个低数值, 您可能会得到

echo "rgb(".$r.",".$g.",".$b.")";

由于rgb(123,45,67) 是完全有效的颜色规格。

类似线条下,您可以为 hsl 生成随机数字 :

echo "hsl(".rand(0,359).",100%,50%)";

这将产生任何色调的完全饱和、正常光亮颜色。 但是, 请注意, 只有最近的溴素支持 HSL, 这样如果浏览器支持是一个关注问题, 您可能比向 RGB 旋转更好 。

问题回答
function getRandomColor() {
    $rand = array( 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ,  a ,  b ,  c ,  d ,  e ,  f );
    $color =  # .$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
    return $color;
}

我使用此代码来检测背景颜色是否亮或暗, 然后选择正确的字体颜色, 所以字体颜色仍然可以在随机生成或用户输入的背景颜色上读取/ 可见 :

//$hex: #AB12CD
function ColorLuminanceHex($hex=0) {
  $hex = str_replace( # ,   , $hex);
  $luminance = 0.3 * hexdec(substr($hex,0,2)) + 0.59 * hexdec(substr($hex,2,2)) + 0.11 * hexdec(substr($hex,4,2));
  return $luminance;
}


$background_color =  #AB12CD ;
$luminance = ColorLuminanceHex($background_color);
if($luminance < 128) {
  $color =  #FFFFFF ;
}
else {
  $color =  #000000 ;
}

使用 chakroun yeser s < a href="https://stackoverflow.com/a/16101648/5435780" 回答 以上,我创建了这个函数:

function generateRandomColor($count=1){
    if($count > 1){
        $color = array();
        for($i=0; $count > $i; $i++)
            $color[count($color)] = generateRandomColor();
    }else{
        $rand = array_merge(range(0, 9), range( a ,  f ));
        $color =  # .$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
    }
    return $color;
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签