Simple PHP Function to convert HEX to RGBA color, opacity value support! Exclusive PHP function to convert HEX to RGBA color, easy to use.
How To Convert HEX To RGBA
Using hexdec() function, but we will use Qassim_HEX_To_RGBA() function to convert HEX to RGBA color.
Qassim HEX To RGBA
function Qassim_HEX_To_RGBA($color, $opacity = 100){ /* Convert HEX Color to RGBA Color, opacity value support. Written By: Qassim Hassan Twitter: @QQQHZ Website: wp-time.com */ $color = trim($color, "#"); $hex = hexdec($color); if( strlen($color) == 6 ){ $r = hexdec( substr($color, 0, 2) ); $g = hexdec( substr($color, 2, 2) ); $b = hexdec( substr($color, 4, 2) ); if( $opacity > 99){ $opacity = 1; }else{ $opacity = "0.$opacity"; } $a = $opacity; } else{ return "Error color code! Please enter correct color code, for example #ffffff"; return false; } return $r.", ".$g.", ".$b.", ".$a; }
Usage:
$color = "#167ac6"; // Color Code $opacity = "50"; // Opacity Value (number inside quotation marks), default is 100 (100 is no opacity). echo Qassim_HEX_To_RGBA($color, $opacity); // Get RGBA Color!
CSS Example:
<style type="text/css"> body{ background-color: rgba(<?php echo Qassim_HEX_To_RGBA("#167ac6", "08"); ?>); } </style>
Note
In opacity value don’t enter direct number like this 03! enter number inside quotation marks like this “03”.