Skip to content

Instantly share code, notes, and snippets.

@chrdesigner chrdesigner/functions.php
Last active Dec 12, 2015

Embed
What would you like to do?
Transform HEX to RGB with PHP
<?php
function hex2RGB($hex) {
$hex = str_replace("#", "", $hex);
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
return $rgb;
}
?>
<?php
$hex_color = '#333333';
$rgb_color = hex2RGB($hex_color);
$result_rgb = implode(", ", $rgb_color);
?>
<style type="text/css">
background-color: rgb(<?php echo $result_rgb;?>);
/*
Example how return value in your style
background-color: rgb('51,51,51');
*/
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.