Fonction pour assombrir une couleur hexa en PHP
Posté dans Codes
Cette fonction vous permettra, à partir d’une couleur en hexadécimal, d’obtenir une variation plus sombre d’un facteur X :
function darken_color($rgb, $darker=2) {
$hash = (strpos($rgb, '#') !== false) ? '#' : '';
$rgb = (strlen($rgb) == 7) ? str_replace('#', '', $rgb) : ((strlen($rgb) == 6) ? $rgb : false);
if(strlen($rgb) != 6) return $hash.'000000';
list($R16,$G16,$B16) = str_split($rgb,2);
$R = sprintf("%02X", min(255, floor(hexdec($R16)/$darker)));
$G = sprintf("%02X", min(255, floor(hexdec($G16)/$darker)));
$B = sprintf("%02X", min(255, floor(hexdec($B16)/$darker)));
return $hash.$R.$G.$B;
}