Conversie cod culori din Hexadecimal in RGB folosind PHP


function hex2rgb($hexCode) {
$hexCode = str_replace("#", "", $hexCode);

if(strlen($hexCode) == 3) {
$r = hexdec(substr($hexCode,0,1).substr($hexCode,0,1));
$g = hexdec(substr($hexCode,1,1).substr($hexCode,1,1));
$b = hexdec(substr($hexCode,2,1).substr($hexCode,2,1));
} else {
$r = hexdec(substr($hexCode,0,2));
$g = hexdec(substr($hexCode,2,2));
$b = hexdec(substr($hexCode,4,2));
}
$rgbCode = array($r, $g, $b);
return $rgbCode; // intoarce un array cu valorile [0]=R, [1]=G, [2]=B
}


$rgbArr = hex2rgb("#ffffff");
$rgb =implode(",", $rgbArr ); // intoarce valorile RGB separate prin virgula

Aplicabilitate
Setare culoare background dinamic cu opacitati diferite:

<style>

.fullOpacity{background:rgb(<?php echo($rgb); ?>)}
.halfOpacity{background:rgba(<?php echo($rgb); ?>, 0.5)}
.minOpacity{background:rgba(<?php echo($rgb); ?>, 0.1)}
.higherOpacity{background:rgba(<?php echo($rgb); ?>, 0.8)}
…..
</style>