Forked from alexkalh/php-convert-color-code-from-hex-to-rgba
Created
September 12, 2017 03:49
-
-
Save krishna19/5baa245b292f81d00a67072b3b1f9371 to your computer and use it in GitHub Desktop.
php-convert-color-code-from-hex-to-rgba
This file contains hidden or 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
<?php | |
function ak_convert_hex2rgba($color, $opacity = false) { | |
$default = 'rgb(0,0,0)'; | |
if (empty($color)) | |
return $default; | |
if ($color[0] == '#') | |
$color = substr($color, 1); | |
if (strlen($color) == 6) | |
$hex = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]); | |
elseif (strlen($color) == 3) | |
$hex = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]); | |
else | |
return $default; | |
$rgb = array_map('hexdec', $hex); | |
if ($opacity) { | |
if (abs($opacity) > 1) | |
$opacity = 1.0; | |
$output = 'rgba(' . implode(",", $rgb) . ',' . $opacity . ')'; | |
} else { | |
$output = 'rgb(' . implode(",", $rgb) . ')'; | |
} | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment