4

这个 PHP RGB 亮度改变功能部分工作:

在此处输入图像描述

最后少了一个零“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);
}
4

2 回答 2

8
return sprintf("#%02x%02x%02x", $r, $g, $b);
于 2011-03-05T00:30:40.130 回答
1

这已在评论中讨论过:

http://php.net/manual/en/function.dechex.php

将每个 r,g,b 包装在 zfill/zeropad 函数中。

于 2011-03-05T00:34:25.060 回答