Skip to content

Instantly share code, notes, and snippets.

@oropesa
Last active August 29, 2015 14:06
Show Gist options
  • Save oropesa/6face07226a0503f4aed to your computer and use it in GitHub Desktop.
Save oropesa/6face07226a0503f4aed to your computer and use it in GitHub Desktop.
Cast Hexadecimal Color to RGBA Color.
<?php
function hex2rgba($hex, $a = '1') {
$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) );
}
$rgba = 'rgba('.$r.', '.$g.', '.$b.', '.$a.')';
return $rgba;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment