Created
December 26, 2017 16:23
-
-
Save Basilakis/4ccc816237f52acce6d3a54decf12c33 to your computer and use it in GitHub Desktop.
Convert HEX value to RGBA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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