Created
February 12, 2016 14:04
-
-
Save Thomas-A-Reinert/7eb5cfdfd64d64e5714e to your computer and use it in GitHub Desktop.
Function to invertRGBa values via PHP
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 | |
/* | |
* Invert incoming RGB/RGBa CSS-String OR just values to RGBa values | |
* Expects a string in one of the following formats: | |
* 255, 255, 255, 0 // Just parameters with/without Alpha value | |
* rgba(255,255,0) // Full CSS value with/without Alpha value | |
* | |
* Thomas A. Reinert // www.tarthemems.com 2015 | |
*/ | |
function invertRGBa($rgba) { | |
$rgba = trim(str_replace(' ', '', $rgba)); | |
if (stripos($rgba, 'rgba') !== false) { | |
$res = sscanf($rgba, "rgba(%d, %d, %d, %f)"); | |
} | |
else { | |
$res = sscanf($rgba, "rgb(%d, %d, %d)"); | |
$res[] = 1; | |
} | |
$r = 255-$res[0]; | |
$g = 255-$res[1]; | |
$b = 255-$res[2]; | |
$output = "$r, $g, $b"; | |
if (isset($res[3] ,$res)) { | |
$output .= ", " . $res[3]; | |
} else { | |
$output .= ", 1"; | |
} | |
return $output; | |
} | |
/* | |
* Example Usages: | |
*/ | |
// echo invertRGBa('255, 255, 255, 0'); // Returns: 255, 255, 255, 1 | |
// echo invertRGBa('rgba(255,255,0)'); // Returns: 0, 0, 255, 1 | |
// echo 'rgba(' . invertRGBa("rgba(0,255,0);") . ');'; // Returns: rgba(255, 0, 255, 1); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
May be overcomplicated but works like a charm ;)