«00» становится «0» в функции PHP, но для работы RGB должно быть «00». Как? - PullRequest
4 голосов
/ 05 марта 2011

эта функция изменения яркости 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);
}

Ответы [ 2 ]

8 голосов
/ 05 марта 2011
return sprintf("#%02x%02x%02x", $r, $g, $b);
1 голос
/ 05 марта 2011

Это обсуждалось здесь в комментариях:

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

Оберните каждый из r, g, b в функцию zfill / zeropad.

...