Fair enough.. examine the code below.. Let's pretend that the $str value is the results of doing a file_get_contents() on some silly nonsense CSS file...
$str = '#div_oopsContent {width: 700px; margin-top: 20px; margin-left: 33px; padding-top: 10px; border-top: 1px dashed #7489D0;}
.urlText {color: #98AEF7;}
.starSystem{width:22px;height:24px;float:left;background:url(/home/images/starSystem2-0.png) no-repeat 0 0;margin:0 0 5px 0}
a.firstStar{margin-left:10px}
a.goldStar{background-position:0 0;margin-right:1px}
a.emptyStar{background-position:-24px 0}
.homeDefList{width:200px;float:left;clear:both;padding-bottom:11px}
dt.dtimg img{float:left}dd{margin:-2px 0 0 11px}
.infoIcon,dd a{float:left}
.infoIcon{margin:-2px 3px 0 0}
.div_dropShadow{float:left;width:181px;height:97px;background-color:#2D3E9E;margin:5px 0 10px 16px}
#recentAdditionImage,#walkthroughImage{float:left;border:1px solid #000;margin:-5px 0 0 -5px}
#recentAdditionImage:hover,#walkthroughImage:hover{border:1px solid #FFF}';
function hexInverse($matches){
$matches[1] = strtoupper($matches[1]);
if(strlen($matches[1]) < 4){ // 3 character shorthand notation detected. Example: C30, which will be converted to CC3300
$trans = array('0' => '00', '1' => '11', '2' => '22', '3' => '33', '4' => '44', '5' => '55', '6' => '66', '7' => '77', '8' => '88', '9' => '99',
'A' => 'AA', 'B' => 'BB', 'C' => 'CC', 'D' => 'DD', 'E' => 'EE', 'F' => 'FF');
$matches[1] = strtr($matches[1], $trans);
}
$invertRGB = array();
$decToHex = NULL;
for($t = 0, $x = 0; $t < 5; $t += 2){
$invertRGB[$x] = 255 - (hexdec(substr($matches[1], $t, 2)));
$decToHex .= (strlen(dechex($invertRGB[$x])) == 1)? '0'.dechex($invertRGB[$x]) : dechex($invertRGB[$x]);
++$x;
}
$decToHex = '#' . strtoupper($decToHex);
return $decToHex;
}
echo $str . '<br /><br />';
$str2 = preg_replace_callback('~(?:#([a-f0-9]{3,6}))~i', "hexInverse", $str);
echo $str2;
Output:
#div_oopsContent {width: 700px; margin-top: 20px; margin-left: 33px; padding-top: 10px; border-top: 1px dashed #7489D0;} .urlText {color: #98AEF7;} .starSystem{width:22px;height:24px;float:left;background:url(/home/images/starSystem2-0.png) no-repeat 0 0;margin:0 0 5px 0} a.firstStar{margin-left:10px} a.goldStar{background-position:0 0;margin-right:1px} a.emptyStar{background-position:-24px 0} .homeDefList{width:200px;float:left;clear:both;padding-bottom:11px} dt.dtimg img{float:left}dd{margin:-2px 0 0 11px} .infoIcon,dd a{float:left} .infoIcon{margin:-2px 3px 0 0} .div_dropShadow{float:left;width:181px;height:97px;background-color:#2D3E9E;margin:5px 0 10px 16px} #recentAdditionImage,#walkthroughImage{float:left;border:1px solid #000;margin:-5px 0 0 -5px} #recentAdditionImage:hover,#walkthroughImage:hover{border:1px solid #FFF}
#div_oopsContent {width: 700px; margin-top: 20px; margin-left: 33px; padding-top: 10px; border-top: 1px dashed #8B762F;} .urlText {color: #675108;} .starSystem{width:22px;height:24px;float:left;background:url(/home/images/starSystem2-0.png) no-repeat 0 0;margin:0 0 5px 0} a.firstStar{margin-left:10px} a.goldStar{background-position:0 0;margin-right:1px} a.emptyStar{background-position:-24px 0} .homeDefList{width:200px;float:left;clear:both;padding-bottom:11px} dt.dtimg img{float:left}dd{margin:-2px 0 0 11px} .infoIcon,dd a{float:left} .infoIcon{margin:-2px 3px 0 0} .div_dropShadow{float:left;width:181px;height:97px;background-color:#D2C161;margin:5px 0 10px 16px} #recentAdditionImage,#walkthroughImage{float:left;border:1px solid #FFFFFF;margin:-5px 0 0 -5px} #recentAdditionImage:hover,#walkthroughImage:hover{border:1px solid #000000}
Here is the breakdown in my thought process:
I obviously plug $str into the preg_replace_callback, which inturn passes it to the hexInverse function.
For every instance that preg pattern finds, I first convert all alpha characters to uppercase (just to keep things uniform).
I then check to see if the match in question is less than 4 characters long (which denotes a shorthand version). And if so, using strtr, translate everything so that we go from say C30 to CC3300.
Then I create a small sub loop that uses substr to access each pair of characters, convert to decimal and subtract that result from 255.
Using $decToHex, I simply append each pair of the previous steps results as hex values once again.
Lastly, I append the # character and once again convert all to uppcase for uniformity and return that value.
So did it work? If you look at the output, the first row is the "before" while the second is the "after".
If we take a look at the very first hex colour value we come across in the "before" row, we see #7489D0. If we follow the above steps:
in RGB terms, 74 = 116, 89 = 137, and D0 = 208.
therefore, to get the inverse:
255 - 116 = 139, 255 - 137 = 118, and 255 - 208 = 47
When I type in 139, 118,47 as the RGB values within the colour palette tool in photoshop, it shows me the hex value: #8B762F. Low and behold, this is the first value in the "after" row in the output.