Skip to content

Instantly share code, notes, and snippets.

@chrdesigner
Last active December 12, 2015 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrdesigner/a312b1301c6aed909cb9 to your computer and use it in GitHub Desktop.
Save chrdesigner/a312b1301c6aed909cb9 to your computer and use it in GitHub Desktop.
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