Skip to content

Instantly share code, notes, and snippets.

@Basilakis
Created December 26, 2017 16:23
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 Basilakis/4ccc816237f52acce6d3a54decf12c33 to your computer and use it in GitHub Desktop.
Save Basilakis/4ccc816237f52acce6d3a54decf12c33 to your computer and use it in GitHub Desktop.
Convert HEX value to RGBA
function mk_convert_rgba($colour, $alpha)
{
if (!empty($colour)) {
if ($alpha >= 0.95) {
return $colour; // If alpha is equal 1 no need to convert to RGBA, so we are ok with it. :)
} else {
if ($colour[0] == '#') {
$colour = substr($colour, 1);
}
if (strlen($colour) == 6) {
list($r, $g, $b) = array(
$colour[0] . $colour[1],
$colour[2] . $colour[3],
$colour[4] . $colour[5]
);
} elseif (strlen($colour) == 3) {
list($r, $g, $b) = array(
$colour[0] . $colour[0],
$colour[1] . $colour[1],
$colour[2] . $colour[2]
);
} else {
return false;
}
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
$output = array(
'red' => $r,
'green' => $g,
'blue' => $b
);
return 'rgba(' . implode($output, ',') . ',' . $alpha . ')';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment