PHP函数需要帮助:brightness();使RGB颜色更暗/更亮
想象一个十六进制的有效#RGB颜色,定义为PHP函数需要帮助:brightness();使RGB颜色更暗/更亮,php,colors,hex,rgb,brightness,Php,Colors,Hex,Rgb,Brightness,想象一个十六进制的有效#RGB颜色,定义为$color=“#f7b9a0” 现在我想让php从这个$color派生出另外两种颜色,它们稍微亮一点/暗一点(相同的色调/颜色,但只是改变了亮度)。我可以通过什么方式实现这一点?什么代码会生成这个?我觉得我需要一些简单的东西,比如: brightness(input rgb color, ± number of steps); // function outputs the new RGB // ?? What php code should go
$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 */
较亮的颜色有效,但较暗的颜色无效。哦,半个解决方案至少是解决方案的一大部分,所以非常感谢 沿着这条路线
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);
}
调用like$color=alter_亮度('2233FF',5)代码>
使用第二个答案。或将以下内容添加到代码中:
如果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;
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;
}
?>
这是该函数的缩小版。我使用str\u pad
为<10的数字添加0。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);
}
函数改变亮度($colorstr,$steps){
//脱掉帽子#
$colourstr=str#u替换('#','$colourstr);
//步长应介于-255和255之间。负=暗,正=亮
$steps=max(-255,min(255,$steps));
//将#fff类型的颜色转换为#ffffff
如果(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);
}
//修改每个构件的刚度
$rgb=数组(substr($colourstr,0,2)、substr($colourstr,2,2)、substr($colourstr,4,2));
对于($i=0;$i
可能有用的信息:/@jnpcl,wwwooops您放置的另一个链接发生了什么,也很有趣,我想我找到了一个似乎更适合您的解决方案的新链接。:)我会把它加回去~你肯定是想用句号而不是加号来组合字符串。+1非常感谢@Cusimar9和@Kevin,当我运行代码时,它说致命错误:调用/var/www/vhosts/aster.nu/httpdocs/color.php第148行的未定义函数hecdec()
我在代码hecdec()和hecdex()中看到这里应该做什么?我非常喜欢你的最后一句话:调用函数的方法是IU所想的。这将是一个很好的解决方案。。。当它工作:)解决了!hexdec/decdex在您的代码中拼错了,但是它可以很好地使颜色更亮,hoewever,我怎样才能使它们更暗?当我使用变量“-5”或任何其他负值时,我会得到错误的RGB数字!查看我的更新当你说“较暗”的颜色不起作用时,它实际上会给你什么结果,例如-25?用这个替换返回行:return sprintf(#%02x%02x%02x“,$r,$g,$b);第二个答案是哪一个?我希望你提到了海报的昵称。正如@Azzoth提到的(),stru pad
需要在左边。
<?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;
}
?>
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);
}