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

想象一下十六进制的有效#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   */

什么PHP代码应该在brightness();功能?为了我的梦想成真?
任何建议和/或代码都非常感谢!

从以下答案更新:

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>

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

解决方法:

沿着这些方向……

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);
}

像$color = alter_brightness(‘#2233FF’,5)一样调用;

上一篇:Android 2.2:调整屏幕亮度


下一篇:【Ubuntu日常技巧】Ubuntu开机自动设置笔记本屏幕亮度