gpt4 book ai didi

php - "00"在 PHP 函数中变为 "0",但它必须是 "00"RGB 才能工作。如何?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:06:55 27 4
gpt4 key购买 nike

此 PHP RGB 亮度改变功能部分起作用:

enter image description here

它在末尾漏了一个零“0”:所以它应该是“00” 如何解决这个问题?

$color = "#a7a709";  // constant 
$color1 = brightness($color,+25); // brighter, echoes #c0c022, correct RGB value
$color2 = brightness($color,-25); // darker echoes #8e8e0, incorrect RGB value!!

如何解决这个问题?非常感谢!

亮度函数();

### CREDITS go to Cusimar9 who wrote this
function 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);
}

最佳答案

return sprintf("#%02x%02x%02x", $r, $g, $b);

关于php - "00"在 PHP 函数中变为 "0",但它必须是 "00"RGB 才能工作。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5200741/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com