PHP函数需要帮助:brightness();使RGB颜色更暗/更亮

时间:2011-03-04 21:59:52

标签: php colors hex rgb brightness

想象一下十六进制的有效#RGB颜色,定义为$color = "#f7b9a0";

现在我想让php从这种颜色中衍生出另外两种颜色,这两种颜色稍微更亮/更暗(相同的色调/颜色,但只是改变了亮度)。我有什么方法可以实现这一目标什么代码会生成这个?我觉得我需要一些简单的东西:

brightness(input rgb color, ± number of steps); // function outputs the new RGB
 // ?? What php code should go here??

理想情况下,我希望在我的html中有这样的东西:

.classDefault {color:<?=$color?> }
.classLighter {color:<?=brightness($color,+10)?> } /* 10 steps brighter */
.classDarker  {color:<?=brightness($color,-25)?> } /* 25 steps darker   */

brightness();函数应包含哪些PHP代码?为了我的梦想成真?
任何建议和/或代码都非常感谢!


从以下答案中更新:

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);
  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = max(0,min(255,$r + $steps));
  $g = max(0,min(255,$g + $steps));  
  $b = max(0,min(255,$b + $steps));

  return '#'.dechex($r).dechex($g).dechex($b);
}

### NOW LETS DEFINE MY COLOR
$color = "#2233FF";

### DERIVED BRIGHTER COLORS
$color1 = brightness($color,25);
$color2 = brightness($color,50);
$color3 = brightness($color,75);

### DERIVED DARKER COLORS
$color4 = brightness($color,-25);
$color5 = brightness($color,-50);
$color6 = brightness($color,-75);


<!-- BRIGHTER -->
<div style=" background-color:<?=$color3?>"><?=$color3?></div>
<div style=" background-color:<?=$color2?>"><?=$color2?></div>
<div style=" background-color:<?=$color1?>"><?=$color1?></div>

<!-- DEFINED CONSTANT -->
<div style=" background-color:<?=$color?>"><?=$color?></div>

<!-- DARKER -->
<div style=" background-color:<?=$color4?>"><?=$color4?></div>
<div style=" background-color:<?=$color5?>"><?=$color5?></div>
<div style=" background-color:<?=$color6?>"><?=$color6?></div>

颜色越亮越好,但颜色越深。哦,半解决方案至少是解决方案的重要组成部分所以非常感谢!

5 个答案:

答案 0 :(得分:7)

这些方面的东西......

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);
  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = max(0,min(255,$r + $steps));
  $g = max(0,min(255,$g + $steps));  
  $b = max(0,min(255,$b + $steps));

  return '#'.dechex($r).dechex($g).dechex($b);
}

$colour = alter_brightness('#2233FF',5);

一样打电话

答案 1 :(得分:3)

Cintia几乎是正确的,但是str_pad应该在不在之后添加0:

<?php 

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);

  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = dechex(max(0,min(255,$r + $steps)));
  $g = dechex(max(0,min(255,$g + $steps)));  
  $b = dechex(max(0,min(255,$b + $steps)));

  $r = str_pad($r,2,"0",STR_PAD_LEFT);
  $g = str_pad($g,2,"0",STR_PAD_LEFT);
  $b = str_pad($b,2,"0",STR_PAD_LEFT);

  $cor = '#'.$r.$g.$b;

  return $cor;
}

?>

答案 2 :(得分:1)

<?php 

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);

  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = dechex(max(0,min(255,$r + $steps)));
  $g = dechex(max(0,min(255,$g + $steps)));  
  $b = dechex(max(0,min(255,$b + $steps)));

  $r = str_pad($r,2,"0");
  $g = str_pad($g,2,"0");
  $b = str_pad($b,2,"0");

  $cor = '#'.$r.$g.$b;

  return $cor;
}

?>

答案 3 :(得分:1)

使用第二个答案。或者在代码中添加以下内容:

如果rgb值为10左右,它将返回十六进制的单个字符。它需要以0为前缀才能正确呈现。

  $newhex = '#';
  $newhex .= (strlen(dechex($r)) === 1) ?  '0'.dechex($r) : dechex($r);
  $newhex .= (strlen(dechex($g)) === 1) ?  '0'.dechex($g) : dechex($g);
  $newhex .= (strlen(dechex($b)) === 1) ?  '0'.dechex($b) : dechex($b);

  return $newhex;

答案 4 :(得分:0)

这是该函数的缩小版本。我使用str_pad为数字添加0&lt; 10. cusimar9的版本没有检查。

 function alter_brightness($colourstr, $steps) {
    //Take off the #
    $colourstr    = str_replace( '#', '', $colourstr );
    // Steps should be between -255 and 255. Negative = darker, positive = lighter
    $steps  = max( -255, min( 255, $steps ) );
    // Transform colors of type #fff to #ffffff
    if ( 3 == strlen( $colourstr ) ) {
        $colourstr    = str_repeat( substr( $colourstr, 0, 1 ), 2 ) . str_repeat( substr( $colourstr, 1, 1 ), 2 ) . str_repeat( substr( $colourstr, 2, 1 ), 2 );
    }
    // Modify the brigthness of each component
    $rgb=array(substr($colourstr,0,2),  substr($colourstr,2,2), substr($colourstr,4,2));
    for($i = 0; $i< count($rgb); $i++){
      $rgb[$i] = str_pad(dechex(max(0,min(255, hexdec($rgb[$i]) + $steps))),2,"0",STR_PAD_LEFT) ;
    }
    return '#'.implode('', $rgb);
}