Skip to content

Commit

Permalink
Create rgba_to_hex.php
Browse files Browse the repository at this point in the history
Reverse conversion
  • Loading branch information
crankygamer authored Dec 15, 2022
1 parent 252a987 commit 0c708dc
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rgba_to_hex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function convert_rgba_to_hex($string) {
$rgba = array();
$hex = '';
$regex = '#\((([^()]+|(?R))*)\)#';
if (preg_match_all($regex, $string ,$matches)) {
$rgba = explode(',', implode(' ', $matches[1]));
} else {
$rgba = explode(',', $string);
}

$rr = dechex($rgba['0']);
$gg = dechex($rgba['1']);
$bb = dechex($rgba['2']);
$aa = '';

if (array_key_exists('3', $rgba)) {
$aa = dechex($rgba['3'] * 255);
}

return strtoupper("#$rr$gg$bb");
}

0 comments on commit 0c708dc

Please sign in to comment.