Skip to content

Instantly share code, notes, and snippets.

@paulruescher
Created February 25, 2013 22:54
Show Gist options
  • Save paulruescher/5034090 to your computer and use it in GitHub Desktop.
Save paulruescher/5034090 to your computer and use it in GitHub Desktop.
Convert hexadecimal color to rgba
/**
* Converts hexidecimal to rgb format
*/
function lab_hex_to_rgb( $hex, $opacity = 1 ) {
$hex = str_replace( '#', '', $hex );
// if in 3 digit format
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 ) );
}
return "rgba( $r, $g, $b, $opacity )";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment