Skip to content

Konvertering af hex farvekoder til rgb med PHP

Når jeg laver themes med opsætning i Customizer i WordPress, har jeg af og til brug for at konvertere HEX værdier til RGB.

Hvis jeg vil lave en semi transparent baggrund benytter jeg, RGB som eksempel:

background-color: rgba(255,255,255,0.5);

Men Customizer farve vælgeren gemmer altid en HEX værdi og jeg har derfor brug for at konvertere HEX værdien.

Til det formål benytter jeg en lille funktion, som en bruger på php.net, har været så venligt at publicere. (http://php.net/manual/en/function.hexdec.php#99478)

[code language=”php”] /**
* Convert a hexa decimal color code to its RGB equivalent
*
* @param string $hexStr (hexadecimal color value)
* @param boolean $returnAsString (if set true, returns the value separated by the separator character. Otherwise returns associative array)
* @param string $seperator (to separate RGB values. Applicable only if second parameter is true.)
* @return array or string (depending on second parameter. Returns False if invalid hex color value)
*/
function hex2RGB($hexStr, $returnAsString = false, $seperator = ‘,’) {
$hexStr = preg_replace("/[^0-9A-Fa-f]/", ”, $hexStr); // Gets a proper hex string
$rgbArray = array();
if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead… faster
$colorVal = hexdec($hexStr);
$rgbArray[‘red’] = 0xFF & ($colorVal >> 0x10);
$rgbArray[‘green’] = 0xFF & ($colorVal >> 0x8);
$rgbArray[‘blue’] = 0xFF & $colorVal;
} elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
$rgbArray[‘red’] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
$rgbArray[‘green’] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
$rgbArray[‘blue’] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
} else {
return false; //Invalid hex color code
}
return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array
}
[/code]

This Post Has 0 Comments

Skriv et svar

Back To Top