想象一个有效的十六进制#RGB 颜色,定义为$color = "#f7b9a0";
现在我想让 php 从这个 $color 派生另外两种颜色,它们稍微更亮/更暗(相同的色调/颜色,但只是改变了亮度)。我可以通过哪些方式实现这一目标?什么代码会生成这个?我觉得我需要一些简单的东西,比如:
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>
较亮的颜色有效,但较暗的无效。哦,一半的解决方案至少是解决方案的很大一部分,所以非常感谢!